I'm writing a game and I would like an explosion animation to run whenever a collision occurs. However the approaches I know how to animate are based on keyboard input. What is the best way to code the method so that when called the animation makes a single pass through all frames then stops?
public void Update(GameTime gametime)
{
timer += gametime.ElapsedGameTime.Milliseconds;
if (timer >= msBetweenFrames)
{
timer = 0;
if (CurrentFrame++ == numberOfFrames - 1)
CurrentFrame = 0;
rect.X = CurrentFrame * width;
rect.Y = 0;
}
}
public void Render(SpriteBatch sb, Vector2 position, Color color)
{
sb.Draw(aniTex, position, rect, color, 0, Vector2.Zero, 2.0f, SpriteEffects.None, 0);
}
If the problem is simply the looping issue, you can just remove the bit of code in your update that causes it to loop (the if (CurrentFrame++ == numberOfFrames - 1) CurrentFrame = 0;) All you would need to do to get the animation to play again, is set the current frame back to 0 when you want it to play. (To stop it drawing, just only do the draw call when you want it.)
If you're looking for a way to structure it (which is how I initially interpreted your question!) why not have a Animated class which you set up with whatever objects you want animated. Something along the lines of
class AnimatedObject
{
Texture2D texture;
Rectangle currentRectangle;
Rectangle frameSize;
int frameCount;
int currentFrame;
bool isRunning;
public AnimatedObject(Texture2D lTexture, Rectangle lFrameSize, int lFrameCount)
{
texture = lTexture;
frameSize = lFrameSize;
currentRectangle = lFrameSize;
currentFrame = 0;
}
public void Pause()
{
isRunning = false;
}
public void Resume()
{
isRunning = true;
}
public void Restart()
{
currentFrame = 0;
isRunning = true;
}
public void Update()
{
if(isRunning)
{
++currentFrame;
if(currentFrame == frameCount)
{
currentFrame = 0;
}
currentRectangle.X = frameSize.Width * currentFrame;
}
}
public void Draw(SpriteBatch spriteBatch)
{
if(isRunning)
{
spriteBatch.Draw(texture, currentRectangle, Color.White);
}
}
}
Obviously you can extend this to be more sophisticated (eg using your timers to choose when to move frames etc.
You can stop the animation, for example, by removing the object with the animation from the game when the animation has finished.
Or otherwise, you could set the currentFrame to an illegal value (e.g. -1) when the animation is done, and test for that.
public void Update(GameTime gametime)
{
if (currentFrame >= 0)
{
timer += gametime.ElapsedGameTime.Milliseconds;
if (timer >= msBetweenFrames)
{
timer = 0;
currentFrame++;
if (currentFramr >= numberOfFrames - 1)
currentFrame = -1;
rect.X = CurrentFrame * width;
rect.Y = 0;
}
}
}
public void Render(SpriteBatch sb, Vector2 position, Color color)
{
if (currentFrame >= 0)
sb.Draw(aniTex, position, rect, color, 0, Vector2.Zero, 2.0f, SpriteEffects.None, 0);
}
Related
I'm making a 2D tower defense game for learning purposes, and can't make the enemies(sprites) to face the correct direction when moving.
Heres how the map is built for clarification:
http://i.imgur.com/ivO8kWe.png
And here is the sprite sheet I'm using to test:
http://i.imgur.com/2h4fSL3.png
The first sprite at the top left is the frame 0, the next on the right is the frame 1 and so on. As you can see the sprite is already looking to the wrong direction. The map have a start point(the first brown tile on the top) and and end point(the last brown tile in the end), and only the brown tiles are walkable, so with the start point and an end point it will calculate the shortest valid path for the sprites to walk in order to reach the end point.
Said that, every sprite that spawn will have a pre determined path to walk, from that i try to find the direction the sprite is facing by checking the last X or Y position and the X or Y current position, with that i choose which line of the sprite sheet i will use for the walk animation. For example, let's say the sprite is moving south, it should use the sprites at the bottom of the sprite sheet(frames 15 to 19), but it's not working.
This is the animation class I'm using for the enemies:
public class AnimatedSprite : Sprite
{
public int Lines { get; set; }
public int Columns { get; set; }
protected int currentFrame;
protected int totalFrames;
protected int timeSinceLastFrame = 0;
protected int milisecondsPerFrame = 50;
public AnimatedSprite(Texture2D texture, int lines, int columns, Vector2 position)
: base ( texture, position)
{
this.texture = texture;
this.position = position;
Lines = lines;
Columns = columns;
totalFrames = Lines * Columns;
}
public override void Update(GameTime gameTime)
{
base.Update(gameTime);
//Here i check if the sprite sheet have more than 1 line because if it have,
//it must use a different update method.
if (Lines > 1)
{
// Down
if (lastPostion.Y < position.Y)
{
AnimateDown(gameTime);
}
// Up
if (position.Y < lastPosition.Y)
{
AnimateUp(gameTime);
}
// Right
if (position.X > lastPosition.X)
{
AnimateRight(gameTime);
}
// Left
if (position.X < lastPosition.X)
{
AnimateLeft(gameTime);
}
}
if (Lines == 1) {
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame == totalFrames)
{
currentFrame = 0;
}
}
}
center = new Vector2(position.X + texture.Width / Columns, position.Y + texture.Height / Lines);
}
public void AnimateUp(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 14)
currentFrame = 10;
}
}
public void AnimateDown(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 19)
currentFrame = 15;
}
}
public void AnimateLeft(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 4)
currentFrame = 0;
}
}
public void AnimateRight(GameTime gameTime)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
if (currentFrame > 9)
currentFrame = 5;
}
}
public override void Draw(SpriteBatch spriteBatch)
{
int width = texture.Width / Columns;
int height = texture.Height / Lines;
int line = (int)((float)currentFrame / (float)Columns);
int column = currentFrame % Columns;
Rectangle originRectangle = new Rectangle(width * column, height * line, width, height);
Rectangle destinationRectangle = new Rectangle((int)position.X, (int)position.Y, width, height);
spriteBatch.Draw(texture, destinationRectangle, originRectangle, Color.White);
}
}
}
Edit: I made a test in a straight line level(start point at left), it starts facing left (frame 0), but when it reaches the third brown tile, it fixes and face the correct direction:
i.imgur.com/3FsGhuY.png
Edit2: I made a test with straight line levels in all four directions(starting down and going up, start right and going left and vice versa), and in all of them, it starts at frame 0 and when it reaches the third tile it fixes and face the correct direction.
You're not checking whether the current frame is below the minimum value for the desired animation loop; you're only checking it against the maximum value. Also, there's some repetition in your code that should probably be factored out to make it easier to read and work with.
I would replace all of your AnimateXXXX methods with a single method:
public void AnimateLoop(GameTime gameTime, int loopFirstFrame, int loopLastFrame)
{
timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
if (timeSinceLastFrame > milisecondsPerFrame)
{
timeSinceLastFrame -= milisecondsPerFrame;
currentFrame++;
}
if (currentFrame > loopLastFrame || currentFrame < loopFirstFrame)
currentFrame = loopFirstFrame;
}
And then call them like this:
// Down
if (lastPostion.Y < position.Y)
AnimateLoop(gameTime, 15, 19);
// Up
if (position.Y < lastPosition.Y)
AnimateLoop(gameTime, 10, 14);
// Right
if (lastPosition.X < position.X)
AnimateLoop(gameTime, 5, 9);
// Left
if (position.X < lastPosition.X)
AnimateLoop(gameTime, 0, 4);
I am working on my game using XNA. I have found a series of tutorials by Oyyou from YouTube and have gone to the collision tutorial so far. I have successfully processed gravity, movement and animation. But I have a problem with collision. The spawn point of my character is up in the air.
The first value is my bool hasJumped. And the second is y velocity. It is accelerating by 0.25.
The character is falling down slowly accelerating. All four platforms are made the same way. All four are in the list and I don't make anything to work with any of them particularly.
When I land on the lowest (ground) I get the parameters I need. hasJumped is false and velocity.Y is 0.
But when I'm on one of three non-ground platforms results are not as I want them. The velocity is 0.25 even though I am making it 0. And hasJumped doesn't become true because my velocity is a non-zero variable. Unluckily I can't post images so I can't show it. But I will post my code:
this is the maingame class where I add players and platforms.
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
player = new Animation(Content.Load<Texture2D>("player"), Content.Load<Texture2D>("rect"), new Vector2(300 + 28, graphics.PreferredBackBufferHeight - 500), 47, 44, graphics.PreferredBackBufferHeight, graphics.PreferredBackBufferWidth);
platformList.Add(new Platform(Content.Load<Texture2D>("Crate"), new Vector2(300,graphics.PreferredBackBufferHeight-100), 80,50));
platformList.Add(new Platform(Content.Load<Texture2D>("Crate"), new Vector2(500, graphics.PreferredBackBufferHeight - 50), 80, 50));
platformList.Add(new Platform(Content.Load<Texture2D>("Crate"), new Vector2(700, graphics.PreferredBackBufferHeight - 60),80,50));
platformList.Add(new Platform(Content.Load<Texture2D>("Crate"), new Vector2(0, graphics.PreferredBackBufferHeight - 10), graphics.PreferredBackBufferWidth, 10));
// TODO: use this.Content to load your game content here
}
This is my Animation class:
class Animation
{
Texture2D texture;
Texture2D rectText;
public Rectangle rectangle;
public Rectangle collisionRect;
public Vector2 position;
Vector2 origin;
public Vector2 velocity;
int currentFrame;
int frameHeight;
int frameWidth;
int screenHeight;
int screenWidth;
float timer;
float interval = 60;
bool movingRight;
public bool hasJumped;
public Animation(Texture2D newTexture, Texture2D newRectText, Vector2 newPosition, int newHeight, int newWidth, int screenH, int screenW)
{
rectText = newRectText;
texture = newTexture;
position = newPosition;
frameHeight = newHeight;
frameWidth = newWidth;
screenHeight = screenH;
screenWidth = screenW;
hasJumped = true;
}
public void Update(GameTime gameTime)
{
rectangle = new Rectangle(currentFrame * frameWidth, 0, frameWidth, frameHeight);
collisionRect = new Rectangle((int)position.X-frameWidth/2, (int)position.Y - frameHeight/2, frameWidth, frameHeight);
origin = new Vector2(rectangle.Width / 2, rectangle.Height / 2);
position = position + velocity;
if (Keyboard.GetState().IsKeyDown(Keys.Right))
{
AnimateRight(gameTime);
velocity.X = 5;
movingRight = true;
}
else if (Keyboard.GetState().IsKeyDown(Keys.Left))
{
AnimateLeft(gameTime);
velocity.X = -5;
movingRight = false;
}
else
{
velocity.X = 0f;
if (movingRight)
currentFrame = 0;
else currentFrame = 4;
}
if(Keyboard.GetState().IsKeyDown(Keys.Space) && !hasJumped)
{
position.Y -= 5f;
velocity.Y = -10;
hasJumped = true;
}
if(hasJumped)
{
velocity.Y += 0.25f;
}
if(position.X<0+frameWidth/2)
{
position.X = 0 + frameWidth / 2;
}
else if (position.X>screenWidth-frameWidth/2)
{
position.X = screenWidth - frameWidth / 2;
}
}
public void AnimateRight(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds/2;
if(timer>interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 3)
{
currentFrame = 0;
}
}
}
public void AnimateLeft(GameTime gameTime)
{
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds / 2;
if (timer > interval)
{
currentFrame++;
timer = 0;
if (currentFrame > 7 || currentFrame < 4)
{
currentFrame = 4;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture,position,rectangle,Color.White, 0f, origin, 1.0f, SpriteEffects.None, 0);
// spriteBatch.Draw(rectText,collisionRect,Color.White);
}
}}
The platform class is pretty simple:
class Platform
{
Texture2D texture;
Vector2 position;
public Rectangle rectangle;
public Platform(Texture2D newTexture, Vector2 newPosition, int platformWidth, int platformHeight)
{
texture = newTexture;
position = newPosition;
rectangle = new Rectangle((int)position.X, (int)position.Y, platformWidth, platformHeight);
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(texture, rectangle, Color.White);
}
}
and the collision method:
public static bool isOnTopOf(this Rectangle r1, Rectangle r2)
{
return (r1.Bottom >= r2.Top - 10 &&
r1.Bottom <= r2.Top + 10 &&
r1.Right >= r2.Left + 10 &&
r1.Left <= r2.Right - 10);
}
The problem is supposed to be here:
foreach (Platform a in platformList)
{
if (player.collisionRect.isOnTopOf(a.rectangle) && player.velocity.Y>=0)
{
player.hasJumped = false;
player.velocity.Y = 0f;
if(player.collisionRect.Bottom > a.rectangle.Top || player.collisionRect.Bottom - a.rectangle.Top <=10)
{
player.position.Y = a.rectangle.Y - 48 / 2;
player.hasJumped = false;
}
}
else
{
player.hasJumped = true;
}
}
But if I exclude the last else it won't fall of the platform.
Thank you for upvoting the question. I have added images.
Your problem is that when the user is on top of a platform that is not the lowest one, during the else when you check if the user is on a platform, then you explicitly set the hasJumped to true.
What you must do is in your foreach where you check your platforms, to order them by INCREASING Y-coordinate (very much depending on your camera settings) and break after you set hasJumped to false. This way you will avoid your issue:
foreach (Platform a in platformList.OrderByY())
{
if (player.collisionRect.isOnTopOf(a.rectangle) && player.velocity.Y>=0)
{
player.hasJumped = false;
player.velocity.Y = 0f;
if(player.collisionRect.Bottom > a.rectangle.Top || player.collisionRect.Bottom - a.rectangle.Top <=10)
{
player.position.Y = a.rectangle.Y - 48 / 2;
player.hasJumped = false;
}
break;
}
else
{
player.hasJumped = true;
}
}
Unfortunately, it is often the case with 3D development that we have to do some hacks to get the application to render correctly. This is especially the case when rendering a complex item with advanced transparencies. In your case however, I would advise to re-imagine the way you work with collisions.
Games development is not an easy task. Superficially, the constraints under which you are working enforce a less than ideal architecture. Everything in an XNA game revolves around the update loop and the ability to control which item gets updated first is so easy and tempting that people tend to use that. It is however generally a much better approach to try and extrapolate functionality in such a way that your models behave in an event-driven way. One way to achieve this is to split the update cycle of your game in the following phases:
Input Phase: Retrieve input from the user and handle them as events (these can be simple method calls on items in a method other than Update or a more complex but better method like using Rx)
Movement Phase: Develop some physical characteristics in your models (acceleration, velocity, weight, etc.) and letting them do the work of positioning, rotating on their own during the movement phase. This is the only thing that they should be doing in Update.
Collision Phase: Test for collision and then fire events on the objects to handle collision.
Cleanup Phase: Cleanup any problems, like moving inside a wall during the collision detection phase. or phasing out particles etc. This gets important when you have requirements to draw on exact pixels to avoid blurring etc.
The above is not necessarily the best way to do it, but it works. If you rely only on the update cycle you will very soon reach a point where the interdependencies between models and the requirement to have some events fired in order will be a nightmare to support. Enforcing an event driven mechanism will save you from a lot of scrapped work with a very small initial overhead.
My suggestion to use Rx may sound a little strange for an XNA game, but trust me, it is an awesome way to work with events. Finally, I would like to mention MonoGame, an excellent framework to develop games for Windows, Mac, Linux, Android, iPhone, Windows Phone, WinRT (and even more) if you do not already know it.
What I'm trying to do is to draw rectangles behind the background and make essentially collision detection. What I'm not sure however is how exactly I can implement that. I thought about making it so that as the sprite approached these rectangles, their speed would get slower and slower till they stop but would that work? Sorry if I sound a bit wet behind the ears, I'm fairly new to C# and am trying to self teach. Any help would be appreciated.
So I have a CharacterSprite class (walkingsprite) all the stuff about the frames is just a walking animation sequence I implemented.
namespace walkingsprite
{
class AnimatedSprite
{
//keyboard
KeyboardState currentKBState;
KeyboardState previousKBState;
Texture2D spriteTexture;
float timer = 0f;
float interval = 200f;
int currentFrame = 0;
int spriteWidth = 32;
int spriteHeight = 48;
int spriteSpeed = 2;
Rectangle sourceRect;
Texture2D obst;
Rectangle obst1;
Obstruction obstruction1;
Vector2 position;
Vector2 origin;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public Vector2 Origin
{
get { return origin; }
set { origin = value; }
}
public Texture2D Texture
{
get { return spriteTexture; }
set { spriteTexture = value; }
}
public Rectangle SourceRect
{
get { return sourceRect; }
set { sourceRect = value; }
}
public int SpriteSpeed
{
get { return spriteSpeed; }
set { spriteSpeed = value; }
}
public AnimatedSprite(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
{
this.spriteTexture = texture;
this.currentFrame = currentFrame;
this.spriteWidth = spriteWidth;
this.spriteHeight = spriteHeight;
}
public void HandleSpriteMovement(GameTime gameTime)
{
previousKBState = currentKBState;
currentKBState = Keyboard.GetState();
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);
////////////////////////////////////////////////////////////////////
if (currentKBState.GetPressedKeys().Length == 0)
{
if (currentFrame > 0 && currentFrame < 4)
{
currentFrame = 0;
}
if (currentFrame > 4 && currentFrame < 8)
{
currentFrame = 4;
}
if (currentFrame > 8 && currentFrame < 12)
{
currentFrame = 8;
}
if (currentFrame > 12 && currentFrame < 16)
{
currentFrame = 12;
}
}
////////////////////////////////////////////////////////////////////
//sprintin
if (currentKBState.IsKeyDown(Keys.Space))
{
spriteSpeed = 2;
interval = 100;
}
else
{
spriteSpeed = 1;
interval = 200;
}
///////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Down) == true)
{
AnimateDown(gameTime);
if (position.Y < 575)
{
position.Y += spriteSpeed;
}
}
////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Up) == true)
{
AnimateUp(gameTime);
if (position.Y > 25)
{
position.Y -= spriteSpeed;
}
}
//////////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Right) == true)
{
AnimateRight(gameTime);
if (position.X < 780)
{
position.X += spriteSpeed;
}
}
////////////////////////////////////////////////////////////////////
if (currentKBState.IsKeyDown(Keys.Left) == true)
{
AnimateLeft(gameTime);
if (position.X > 0)
{
position.X -= spriteSpeed;
}
}
origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
}
////////////////////////////////////////////////////////////////////
public void AnimateRight(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 9;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if(timer > interval)
{
currentFrame++;
if(currentFrame > 11)
{
currentFrame = 8;
}
timer = 0f;
}
}
////////////////////////////////////////////////////////////////////
public void AnimateUp(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 13;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 15)
{
currentFrame = 12;
}
timer = 0f;
}
}
//////////////////////////////////////////
public void AnimateDown(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 1;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 3)
{
currentFrame = 0;
}
timer = 0f;
}
}
////////////////////////////////////////////////////
public void AnimateLeft(GameTime gameTime)
{
if (currentKBState != previousKBState)
{
currentFrame = 5;
}
timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (timer > interval)
{
currentFrame++;
if (currentFrame > 7)
{
currentFrame = 4;
}
timer = 0f;
}
}
}
}
And I have an obstruction class.
namespace walkingsprite
{
class Obstruction
{
Texture2D obst;
Rectangle obstRec1;
public Rectangle ObstRec1
{
get { return obstRec1; }
set { obstRec1 = value; }
}
public Texture2D Obst
{
get { return obst; }
set { obst = value; }
}
public Obstruction(Texture2D texture, Rectangle rec)
{
this.obstRec1 = rec;
this.obst = texture;
}
}
}
XNA already have a Rectangle class with a method to return if a rectangle collide with another rectangle.
An exemple:
Rectangle rec1 = new Rectangle(0, 0, 10, 10);
Rectangle rec2 = new Rectangle(5, 5, 10, 10);
rec1.Intersects(rec2); //Return true if a rectangle intersects another one
What people normally do, is a List of Rectangle and test one by one to know if they collide, or they implements a QuadTree but that is a bit more difficult in the beginning.
An exemple of a List of Rectangle:
List<Rectangle> allObjects = new List<Rectangle>();
Rectangle rec1 = new Rectangle(0, 0, 10, 10);
Rectangle rec2 = new Rectangle(10, 15, 10, 10);
Rectangle mainCharRec = new Rectangle(10, 20, 10, 10);
allObjects.Add(rec1);
allObjects.Add(rec2);
To test if main character rectangle intersects some other rectangles you can do a method with a foreach like this:
foreach (Rectangle rec in allObjects)
if(mainCharRec.Intersects(rec))
return true;
return false;
About getting slower, I think you could have a rectangle to the object and another to the "close region", and when the character enter in the region you slow his speed by the distance you give to the region, for example, if you have a rectangle with 10 pixels larger for each side, when character collides with larger rectangle he loses spriteSpeed/10 for step, until he stops. When the value is too much low, it turn 0:
if (spriteSpeed < 0.5)
spriteSpeed = 0;
else
spriteSpeed -= spriteSpeed/10;
Firstly, I would like to say sorry for making another thread asking the same question (more or less), but I am desperate for an answer and the other thread went dead.
I am currently working on a project for school in which I am attempting to make a simple 2d shooter. Problem is, I don't know how to implement collision detection between my two lists (being bullets and enemies as stated in title). I am very new to programming and have been using various tutorials as a guide, some tutorials say to use a nested for loop and if statement while I have seen others using an if statement, oh and I'm using the rectangle collision method not the pixel by pixel one I think it was called. Thanks in advance! :)
Here is my code:
Main:
namespace Software_Design_Major_Project
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D ship; // Declaring the sprite
Vector2 shipposition = Vector2.Zero; // Creating a vector for the sprite.
List<bullets> bullets = new List<bullets>(); // a list is created here so that there can be multiple copies of this item at once without writing the extra code.
Texture2D texture;
KeyboardState pastkey;
//bullet sound effect.
SoundEffect effect;
List<enemies> Enemies = new List<enemies>();
Random random2 = new Random();
float spawn = 0;
public enum GameState
{
MainMenu,
Playing,
}
GameState CurrentGameState = GameState.MainMenu;
int screenWidth = 600, screenHeight = 480;
cButton play;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
//changes width of screen to 600px.
graphics.PreferredBackBufferWidth = 600;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
// This statement positions the ship.
ship = Content.Load<Texture2D>("ship"); // Loads the ship into the memory.
shipposition = new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - (ship.Width / 2), 420);
// loads bullet sprite
texture = Content.Load<Texture2D>("bullet");
graphics.PreferredBackBufferWidth = screenWidth;
graphics.PreferredBackBufferHeight = screenHeight;
graphics.ApplyChanges();
IsMouseVisible = true;
play = new cButton(Content.Load<Texture2D>("play"), graphics.GraphicsDevice);
play.setPosition(new Vector2(225, 220));
effect = Content.Load<SoundEffect>("laser");
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
spawn += (float)gameTime.ElapsedGameTime.TotalSeconds;
// spawns enemy every second.
foreach (enemies enemy in Enemies)
enemy.update(graphics.GraphicsDevice);
MouseState mouse = Mouse.GetState();
switch (CurrentGameState)
{
case GameState.MainMenu:
if (play.isClicked == true)
CurrentGameState = GameState.Playing;
play.Update(mouse);
break;
case GameState.Playing:
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.E))
{
Exit();
}
break;
}
// Allows the ship to move left and stops the ship going off the screen.
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Left) && shipposition.X >= 0)
{
shipposition.X -= 7;
}// same as above except for the right direction.
if (Keyboard.GetState(PlayerIndex.One).IsKeyDown(Keys.Right) && shipposition.X < ((graphics.GraphicsDevice.Viewport.Width) - (ship.Width)))
{
shipposition.X += 7;
}
// this prevents the player from holding down space to spam bullets.
if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastkey.IsKeyUp(Keys.Space))
{
bullets bullet = new bullets(texture);
// the ships coordinates are gathered from the top left hand corner of the sprites rectangle therefore 'new Vector2(shipposition.X + 32, shipposition.Y)' had to
// be added rather than just = 'shipposition' to avoid having the bullets shoot from the wing.
bullet.position = new Vector2(shipposition.X + 32, shipposition.Y);
bullets.Add(bullet);
effect.Play();
}
pastkey = Keyboard.GetState();
//calls upon the update method from the bullets class.
foreach (bullets bullet in bullets)
bullet.update();
LoadEnemies();
base.Update(gameTime);
}
public void LoadEnemies()
{
int randX = random2.Next(10, 540);
if (spawn <= 1)
{
spawn = 0;
//limits amount of enemies on screen to 5.
if (Enemies.Count() < 5)
Enemies.Add(new enemies(Content.Load<Texture2D>("enemy"), new Vector2(randX, -100)));
}
for (int i = 0; i < Enemies.Count; i++)
{
if (!Enemies[i].enemyVisble)
{
//removes the enemies when they go off screen.
Enemies.RemoveAt(i);
i--;
}
}
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// calls draw method in bullets class
foreach (bullets bullet in bullets)
{
bullet.Draw(spriteBatch);
}
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
spriteBatch.Draw(ship, shipposition, Color.White); // draws ship sprite
switch (CurrentGameState)
{
case GameState.MainMenu:
play.draw(spriteBatch);
spriteBatch.Draw(Content.Load<Texture2D>("menu"), new Rectangle(0, 0, screenWidth, screenHeight), Color.White);
break;
case GameState.Playing:
break;
}
foreach (enemies enemy in Enemies)
{
enemy.draw(spriteBatch);
}
spriteBatch.End();
base.Draw(gameTime);
}
}
}
Bullets Class:
namespace Software_Design_Major_Project
{
class bullets // A new class needs to be created to allow for bullets.
{
public Texture2D texture;
public Vector2 position;
public bool isvisible;
public bullets(Texture2D newtexture)
{
texture = newtexture;
isvisible = false;
}
public void update()
{
position.Y -= 3; // velocity of the bullet
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Begin();
spriteBatch.Draw(texture, position, Color.White);
spriteBatch.End();
}
}
}
Enemies Class:
namespace Software_Design_Major_Project
{
public class enemies
{
public Texture2D enemyTexture;
public Vector2 enemyPosition;
public bool enemyVisble = true;
public float enemyMoveSpeed;
public int Value;
Random random = new Random();
int randY;
public enemies (Texture2D newEnemyTexture, Vector2 newEnemyPosition)
{
enemyTexture = newEnemyTexture;
enemyPosition = newEnemyPosition;
randY = random.Next(1, 4);
enemyMoveSpeed = randY;
enemyVisble = true;
Value = 100;
}
public void update(GraphicsDevice graphics)
{
enemyPosition.Y += enemyMoveSpeed;
if (enemyPosition.Y > 500)
enemyVisble = false;
}
public void draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(enemyTexture, enemyPosition, Color.White);
enemyVisble = true;
}
}
}
You have several ways to do it... one of them maybe add a radio property to enemies and bullets....
for (int bi=0; bi<bullets.count; )
{
bool should_destroy_bullet = false;
Bullet b = bullets[bi];
for (int ei=0; ei<enemies.count; )
{
Enemy e = ememies[ei];
if (b.radio + e.radio < Vector2.Distance(b.Pos, e.Pos)) // Check collision
{
e.Died();
enemies[ei] = enemies[enemies.Count-1];
enemies.RemoveAt(enemies.Count-1);
should_destroy_bullet = true; // This lets a bullet kill more than one enemy
} else ei++;
}
if (should_destroy_bullet) {
b.Died();
bullets[bi] = bullets[bullets.count-1];
bullets.RemoveAt(bullets.count-1);
} else bi++;
}
Or you can build a rectangle for each sprite and check if they intersects....
if (b.Bounds.Intersects(e.Bounds)) ....
The Pythagorean theorem could work for you.(A squared plus B squared = C squared)
Make your players and enemies have a radius.Put this in your constructor.
public float radius = 15;
Put the following in a update void/subclass so that it happens every second.
Make a float the X location of your player and subtract the X location of the bullet. Square it.
float XDist = Math.Pow(player1.location.X - bullet.location.X,2);
Make a float the Y location of your player and subtract the Y location of the bullet.Square it.
float YDist = Math.Pow(player1.location.X - bullet.location.X,2);
Make a float to figure out the square root of the sum of Xdist and Ydist.
float actualDist = Math.Sqrt(Xdist + Ydist);
Now if the radius of your player is less than the distance between the player and the bullet, the bullet has hit the player.
if(actualDist < radius)
bullethit == true;
So heres my problem. I have a box that I want my character to move around. But I want to be able to move around it while holding multiple move commands, for instance..
when moving right (towards the left of the obstacle) I want to be able to hold move right and up or down at the same time without the character sticking to the box. The funny part is, it works fine for the left and right side of the obstacle, yet it sticks when i try it on the top and bottom side of the obstacle.
Heres the Player Class (object im moving)
<!-- language: c# -->
public class Player
{
public Texture2D texture;
public Vector2 position;
public int speed;
public Vector2 offset;
public bool left, right, up, down;
public Rectangle collisionRect
{
get
{
return new Rectangle((int)position.X , (int)position.Y, texture.Width, texture.Height);
}
}
public Vector2 direction;
public Player(Texture2D texture, Vector2 position, int speed)
{
this.texture = texture;
this.position = position;
this.speed = speed;
offset.X = speed;
offset.Y = speed;
left = false;
right = false;
up = false;
down = false;
}
public virtual void Update(GameTime gameTime, Rectangle clientBounds)
{
direction = Vector2.Zero;
if (Keyboard.GetState().IsKeyDown(Keys.A))
{
direction.X -= 1;
left = true;
}
else
left = false;
if (Keyboard.GetState().IsKeyDown(Keys.D))
{
direction.X += 1;
right = true;
}
else
right = false;
if (Keyboard.GetState().IsKeyDown(Keys.W))
{
direction.Y -= 1;
up = true;
}
else
up = false;
if (Keyboard.GetState().IsKeyDown(Keys.S))
{
direction.Y += 1;
down = true;
}
else
down = false;
position += (direction * speed);
}
public virtual void Draw(GameTime gameTime, SpriteBatch spritebatch)
{
spritebatch.Draw(texture, collisionRect, Color.White);
}
}
Heres the Update Method in my maingame
<!-- language: c# -->
public override void Update(GameTime gameTime)
{
// TODO: Add your update code here
player.Update(gameTime, Game.Window.ClientBounds);
if (player.right && HitWall(player))
{
player.position.X -= player.offset.X;
}
else if (player.left && HitWall(player))
{
player.position.X += player.offset.X;
}
if (player.down && HitWall(player))
{
player.position.Y -= player.offset.Y;
}
else if (player.up && HitWall(player))
{
player.position.Y += player.offset.Y;
}
base.Update(gameTime);
}
And the HitWall function
<!-- language: c# -->
public bool HitWall(Player player)
{
for (int i = player.collisionRect.Top; i < player.collisionRect.Bottom; i++)
for (int j = player.collisionRect.Left; j < player.collisionRect.Right; j++)
if (TextureData[i * gameMap.map.Width + j] != Color.White)
return true;
return false;
}
I'm not sure where offset is defined, but I'm assuming it's the movement you've just made that frame.
Your problem is that because you check left & right before up and down, if you're moving diagonally down onto the top edge of the box, then you'll register a hit in the Y direction — HitWall doesn't check which direction you're going, it just checks for a collision. Therefore, the collision in the Y axis stil counts on the line if (player.right && HitWall(player)) and stops your lateral movement.
Best bet is to apply your sideways movement, check for a hit, move back if there is one — then apply your downwards movement, check for a hit, and move back if there is one. Correcting the position like this should mean you slide along the sides as you want.