How to move a object
Tutorials on how to use and create games in XNA.
1 post
Page 1 of 1
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
so under protected override void LoadContent()
{
now we have loaded them we are ready to do the logic's of it
so under protected override void Update(GameTime gameTime)
{
so under protected override void Draw(GameTime gameTime)
{
if this has helped you please give me a like thanks =]
under public class Game1 : Microsoft.Xna.Framework.Game
{
add
so lets get started
Code: Select all
now what we need to do is load the content in this case the ballTexture2D ball;
Rectangle ballrec;
Keyboardstate keyboard;
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
now all we need to do is draw this into the screenkeyboard = 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
};
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 =]
1 post
Page 1 of 1
Copyright Information
Copyright © Codenstuff.com 2020 - 2023