Note that there are some explanatory texts on larger screens.

plurals
  1. PONeed some help implementing collisions in my game, with two separate classes
    primarykey
    data
    text
    <p>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. </p> <p>So I have a CharacterSprite class (walkingsprite) all the stuff about the frames is just a walking animation sequence I implemented. </p> <pre><code>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 &gt; 0 &amp;&amp; currentFrame &lt; 4) { currentFrame = 0; } if (currentFrame &gt; 4 &amp;&amp; currentFrame &lt; 8) { currentFrame = 4; } if (currentFrame &gt; 8 &amp;&amp; currentFrame &lt; 12) { currentFrame = 8; } if (currentFrame &gt; 12 &amp;&amp; currentFrame &lt; 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 &lt; 575) { position.Y += spriteSpeed; } } //////////////////////////////////////////////////// if (currentKBState.IsKeyDown(Keys.Up) == true) { AnimateUp(gameTime); if (position.Y &gt; 25) { position.Y -= spriteSpeed; } } ////////////////////////////////////////////////////////// if (currentKBState.IsKeyDown(Keys.Right) == true) { AnimateRight(gameTime); if (position.X &lt; 780) { position.X += spriteSpeed; } } //////////////////////////////////////////////////////////////////// if (currentKBState.IsKeyDown(Keys.Left) == true) { AnimateLeft(gameTime); if (position.X &gt; 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 &gt; interval) { currentFrame++; if(currentFrame &gt; 11) { currentFrame = 8; } timer = 0f; } } //////////////////////////////////////////////////////////////////// public void AnimateUp(GameTime gameTime) { if (currentKBState != previousKBState) { currentFrame = 13; } timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (timer &gt; interval) { currentFrame++; if (currentFrame &gt; 15) { currentFrame = 12; } timer = 0f; } } ////////////////////////////////////////// public void AnimateDown(GameTime gameTime) { if (currentKBState != previousKBState) { currentFrame = 1; } timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (timer &gt; interval) { currentFrame++; if (currentFrame &gt; 3) { currentFrame = 0; } timer = 0f; } } //////////////////////////////////////////////////// public void AnimateLeft(GameTime gameTime) { if (currentKBState != previousKBState) { currentFrame = 5; } timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds; if (timer &gt; interval) { currentFrame++; if (currentFrame &gt; 7) { currentFrame = 4; } timer = 0f; } } } } </code></pre> <p>And I have an obstruction class.</p> <pre><code> 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; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload