0
\$\begingroup\$

Here's the issue, When I build, everything loads fine, everything works, but then, when you enter the game, the sprite is there. However, The sprite does not move, the sprite RECTANGLE does. Meaning that, if I used my arrow keys to move the "sprite", as I defined them in the update method of my class, the rectangle would move freely, and cut off parts of the sprite. If I moved it the right far enough, the rectangle would leave the sprite, and the sprite will disappear.

Here's a picture of what it looks like:

http://www.flickr.com/photos/79305580@N06/8749802527/in/photostream

and another when I use my arrow keys to move:

http://www.flickr.com/photos/79305580@N06/8749802517/in/photostream/

Here is the code from my sprite class:

class Player : Sprite { const string PLAYER_ASSETNAME = "CharacterLeft"; const int START_POSITION_X = 125; const int START_POSITION_Y = 245; const int WIZARD_SPEED = 160; const int MOVE_UP = -1; const int MOVE_DOWN = 1; const int MOVE_LEFT = -1; const int MOVE_RIGHT = 1; enum State { Walking } State mCurrentState = State.Walking; Vector2 mDirection = Vector2.Zero; Vector2 mSpeed = Vector2.Zero; KeyboardState mPreviousKeyboardState; public void LoadContent(ContentManager theContentManager) { Position = new Vector2(START_POSITION_X, START_POSITION_Y); base.LoadContent(theContentManager, PLAYER_ASSETNAME); } public void Update(GameTime theGameTime) { KeyboardState aCurrentKeyboardState = Keyboard.GetState(); UpdateMovement(aCurrentKeyboardState); mPreviousKeyboardState = aCurrentKeyboardState; base.Update(theGameTime, mSpeed, mDirection); } private void UpdateMovement(KeyboardState aCurrentKeyboardState) { if (mCurrentState == State.Walking) { mSpeed = Vector2.Zero; mDirection = Vector2.Zero; if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true) { mSpeed.X = WIZARD_SPEED; mDirection.X = MOVE_LEFT; } else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true) { mSpeed.X = WIZARD_SPEED; mDirection.X = MOVE_RIGHT; } if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true) { mSpeed.Y = WIZARD_SPEED; mDirection.Y = MOVE_UP; } else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true) { mSpeed.Y = WIZARD_SPEED; mDirection.Y = MOVE_DOWN; } } } } 

}

and here's the code to my main sprite class:

class Sprite { //The asset name for the Sprite's Texture public string AssetName; //The Size of the Sprite (with scale applied) public Rectangle Size; //The amount to increase/decrease the size of the original sprite. private float mScale = 1.0f; //The current position of the Sprite public Vector2 Position = new Vector2(0, 0); //The texture object used when drawing the sprite private Texture2D mSpriteTexture; //The player Rectangle private Rectangle SpriteRect; //Load the texture for the sprite using the Content Pipeline public void LoadContent(ContentManager theContentManager, string theAssetName) { mSpriteTexture = theContentManager.Load<Texture2D>(theAssetName); AssetName = theAssetName; Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * mScale), (int)(mSpriteTexture.Height * mScale)); } //Draw the sprite to the screen public void Draw(SpriteBatch theSpriteBatch) { SpriteRect = new Rectangle((int)Position.X, (int)Position.Y, mSpriteTexture.Width, mSpriteTexture.Height); theSpriteBatch.Draw(mSpriteTexture, Position,SpriteRect, Color.White, 0.0f, Vector2.UnitX, 1f, SpriteEffects.None, 0); } public void Update(GameTime theGameTime, Vector2 theSpeed, Vector2 theDirection) { Position += theDirection * theSpeed * (float)theGameTime.ElapsedGameTime.TotalSeconds; } //When the scale is modified throught he property, the Size of the //sprite is recalculated with the new scale applied. public float Scale { get { return mScale; } set { mScale = value; //Recalculate the Size of the Sprite with the new scale Size = new Rectangle(0, 0, (int)(mSpriteTexture.Width * Scale), (int)(mSpriteTexture.Height * Scale)); } } } 

}

\$\endgroup\$
3
  • \$\begingroup\$ You'd have to include some small code snippets if you want better help with this. How you're creating/drawing/moving. As minimal as possible. \$\endgroup\$ Commented May 18, 2013 at 14:31
  • \$\begingroup\$ Added them, just now. \$\endgroup\$ Commented May 18, 2013 at 14:41
  • \$\begingroup\$ No one? Any help please? \$\endgroup\$ Commented May 18, 2013 at 15:09

1 Answer 1

2
\$\begingroup\$

In the overload of SpriteBatch.Draw that you are using, which you call like so:

SpriteRect = new Rectangle((int)Position.X, (int)Position.Y, mSpriteTexture.Width, mSpriteTexture.Height); theSpriteBatch.Draw(mSpriteTexture, Position,SpriteRect, Color.White, 0.0f, Vector2.UnitX, 1f, SpriteEffects.None, 0); 

You are passing SpriteRect into the method parameter which defines the sprite's source rectangle. Here's the method signature.

public void Draw ( Texture2D texture, Vector2 position, Nullable<Rectangle> sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth ) 

Also from that linked MSDN documentation:

sourceRectangle

Type: Nullable<Rectangle> A rectangle that specifies (in texels) the source texels from a texture. Use null to draw the entire texture.

So when your sprite's position changes, the drawn texture is a different region of your asset. That is the source of your symptoms.

You need to change one or both of those lines to the following:

// (this Rectangle SpriteRect doesn't need to update!) SpriteRect = new Rectangle(0, 0, mSpriteTexture.Width, mSpriteTexture.Height); // (Or even exist!) theSpriteBatch.Draw(mSpriteTexture, Position, null, Color.White, 0.0f, Vector2.UnitX, 1f, SpriteEffects.None, 0); 
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.