How to move a object

Tutorials on how to use and create games in XNA.
1 post Page 1 of 1
Contributors
User avatar
reececlarke
VIP - Donator
VIP - Donator
Posts: 245
Joined: Tue Dec 08, 2009 6:47 pm

How to move a object
reececlarke
right i guess this is a tut i should do as you will need to know how to do this as in every game something moves of course.
under public class Game1 : Microsoft.Xna.Framework.Game
{
add
so lets get started
Code: Select all
Texture2D ball;
Rectangle ballrec;
Keyboardstate keyboard;
now what we need to do is load the content in this case the ball

so under protected override void LoadContent()
{
Code: Select all
ball = Content.Load<Texture2D>("ball");
ballrec = new Rectangle(0,0,50,50);

// it goes in x,y on the screen then the width and the hight of the image

// Hint 
// Instead of using (0,0,50,50) you could use(0,0,ball.Hight,ball.Width)
// this gets the Hight and Width of the ball texture instead of you having to enter it manualy


now we have loaded them we are ready to do the logic's of it
so under protected override void Update(GameTime gameTime)
{
Code: Select all
keyboard = Keyboard.GetState();

if (keyboard.IsKeyDown(Keys.Down))
            {
                ballrec.Y += 3;

            };
            if (keyboard.IsKeyDown(Keys.Up))
            {
                ballrec.Y -= 3;

            };
            if (keyboard.IsKeyDown(Keys.Left))
            {
                ballrec.X -= 3;

            };

            if (keyboard.IsKeyDown(Keys.Right))
            {
                ballrec.X += 3; // changeing the number will increase or decrease the speed
            };




now all we need to do is draw this into the screen

so under protected override void Draw(GameTime gameTime)
{
Code: Select all
  
 spriteBatch.Begin();

            spriteBatch.Draw(ball, ballrec, Color.White);

// rem that white has no tint any other colour will have.



            spriteBatch.End();

if this has helped you please give me a like thanks =]
Image
1 post Page 1 of 1
Return to “Tutorials”