XNA - Can't change sprite position (move it by keyboard input)

For my first project with XNA, I followed a tutorial which explain how to make a little square face jumping between tiles placed randomly. Yeah, cool! Well, not that much, but it worked and it was fine.

I don't understand how the position update works. I mean, I was expecting to draw every single frame, but the Draw function is called only at the very beginning, when textures are loaded from content, then it just update the position by adding vectorial values.

Next step: I tried to implement (by my self, that's why I'm here) an animated sprite instead of the square of the first project. The sprite is animated (it's a walking-on-the-spot man) but it doesn't move by keyboard inputs anymore...


UPDATE

Ok, I think I found the problem. The class I downloaded has a Draw method more complicated then the one I used before...

batch.Draw(myTexture, Position, sourcerect, Color.White, Rotation, Origin, Scale, SpriteEffects.None, Depth);

... because there's a "position", but even a "origin", which I didn't try to update. I'd like even to delete it if it's not important... I'll make some try!

Edit: I did those tries, still the same...


UPDATE

Question cleaned, here the full code of my app:

Main class: http:// pastebin.com/EUexh9H1

Controller class: http://pastebin.com/ZEcY0DXT

Downloaded class: http://pastebin.com/H2ZDu6bs

(in this one, I just added a "Position" and the Draw call)


UPDATE

I tried to start the whole project from the beginning, to have a better and cleaner code, but it's still the same. I'm just trying to move the animated texture with a Position += Vector2.UnitX;, but it doesn't move at all...

What does it change with frames? I mean, when I call the draw with the Position, how frames affect it? If it does...

Answers


In your batch.Draw method you use screenPos, it is passed as a parameter, but I hope that there's a Vector2 class variable that represents the current position of your sprite on the screen. (I don't know where you call that DrawFrame) If you want to update that position you need to change that class variable in the Update method of your class. You don't have to add other parameters to DrawFrame and UpdateFrame, you already have what you need.

EDIT

In Class2 Update you only need to do:

ActiveSpriteTexture.Position += /* whatever you need */

I don't know what Movement * (float)gameTime.ElapsedGameTime.TotalMilliseconds / 15 does.

And UpdateFrame doesn't need Position parameter, that method should only update the animation of your sprite. The same for DrawFrame, you have to draw the class variable Position, but you should already have done this edit.


Solved.

The problem was that the position was assigned inside the DrawFrame method, and at each update it restore itself, so I just moved this assignment in the costructor. It works.

So...

public void DrawFrame(SpriteBatch batch, int frame, Vector2 screenPos)
{
    Position = screenPos;
    int FrameWidth = myTexture.Width / framecount;
    Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0, FrameWidth, myTexture.Height);
    batch.Draw(myTexture, Position, sourcerect, Color.White, Rotation, Origin, Scale, SpriteEffects.None, Depth);
}

... becomes...

public void DrawFrame(SpriteBatch batch, int frame)
{
    //Position = screenPos;
    int FrameWidth = myTexture.Width / framecount;
    Rectangle sourcerect = new Rectangle(FrameWidth * frame, 0, FrameWidth, myTexture.Height);
    batch.Draw(myTexture, Position, sourcerect, Color.White, Rotation, Origin, Scale, SpriteEffects.None, Depth);
}

Need Your Help

Wordpress category.php Pagination 404 Errors

php wordpress pagination http-status-code-404 categories

I'm trying to display posts for categories in my category.php file with pagination, but when I click the "older posts" button, I'm getting a 404. Here's the code I'm currently using for the query:

Javascript change the souce of all images present inside a string

javascript jquery

I have a message or string which contain both text as well as images as below.