XNA Creators Club Online
Page 1 of 1 (3 items)
Sort Posts: Previous Next

Sprite manager method

Last post 09-01-2008 7:22 PM by DigitalGhost123. 2 replies.
  • 08-29-2008 2:32 PM

    Sprite manager method

    Hello boys. I have a problem: I want to create a method that manages the movements of my sprite. The method must move a sprite from an initial position to a final position. how can i do?
  • 08-31-2008 3:20 AM In reply to

    Re: Sprite manager method

    This will do the job. The parameters you send in are the current position of your sprite, the desired position of your sprite, and the speed you want your sprite to be moving. What you get back is the new position of your sprite.

    Vector2 MoveSprite(Vector2 position, Vector2 destination, float speed)
    {
       Vector2 velocity = destination - position;
       if(velocity.Length < speed)
       {
          return destination; 
       }
       velocity.Normalize();
       velocity *= speed;
       return position + velocity;
    }

    You'll want to scale this properly in your Update() function:

    spritePosition = MoveSprite(spritePosition, spriteDestination, spriteSpeed * gameTime.ElapsedGameTime.TotalSeconds);

    Your spriteSpeed should be set to the number of pixels your sprite can move every second.

  • 09-01-2008 7:22 PM In reply to

    Re: Sprite manager method

    tnx :)
Page 1 of 1 (3 items) Previous Next