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

Needs Movement

Last post 7/8/2007 9:42 AM by Camp ELM. 2 replies.
  • 7/8/2007 6:12 AM

    Needs Movement

    ok i get the code to create TONS of 1 image but they dont move at all!!!! im on my last nerve right now XD and im VERRY LOST
    and VERY confused...

    can some one PLEASE EXPLAIN!!!!!! how to do this...

    all i want is my bullets to have movemnt like

    Bullet.x = Bullet.x + 5;

    per frame

    also IN THE FUTURE i alos need collisions....

    I NEED TONS OF HELP XD

    and tutorials are CONFUSING XD

    so HERES THE CODE

    #region Using Statements
    using System;
    using System.Collections.Generic;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Storage;
    #endregion

    namespace Spaceattack
    {
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
    GraphicsDeviceManager graphics;
    ContentManager content;
    KeyboardState oldState;
    List<Vector2> bullets = new List<Vector2>();

    public Game1()
    {
    graphics = new GraphicsDeviceManager(this);
    content = new ContentManager(Services);
    oldState = Keyboard.GetState();
    }


    /// <summary>
    /// Allows the game to perform any initialization it needs to before starting to run.
    /// This is where it can query for any required services and load any non-graphic
    /// related content. Calling base.Initialize will enumerate through any components
    /// and initialize them as well.
    /// </summary>
    protected override void Initialize()
    {
    // TODO: Add your initialization logic here

    base.Initialize();
    }


    Texture2D myShip; // you ship Player - 1
    Texture2D Missle; // Player - 1 Missle
    Texture2D Bullet; // Player - 1 Bullet
    // ENEMIES
    Texture2D Enemy1; // enemy1
    //POSITIONS
    Vector2 ShipPosition = Vector2.Zero;
    Vector2 BulletPosition = Vector2.Zero;
    Vector2 MisslePosition = Vector2.Zero;
    Vector2 Enemy1Position = Vector2.Zero;
    SpriteBatch spriteBatch;
    protected override void LoadGraphicsContent(bool loadAllContent)
    {
    if (loadAllContent)
    {
    myShip = content.Load<Texture2D>("Ship");
    Missle = content.Load<Texture2D>("Missle");
    Bullet = content.Load<Texture2D>("Bullet");
    Enemy1 = content.Load<Texture2D>("Enemy1");
    spriteBatch = new SpriteBatch(graphics.GraphicsDevice);
    }
    }



    /// <summary>
    /// Unload your graphics content. If unloadAllContent is true, you should
    /// unload content from both ResourceManagementMode pools. Otherwise, just
    /// unload ResourceManagementMode.Manual content. Manual content will get
    /// Disposed by the GraphicsDevice during a Reset.
    /// </summary>
    /// <param name="unloadAllContent">Which type of content to unload.</param>
    protected override void UnloadGraphicsContent(bool unloadAllContent)
    {
    if (unloadAllContent == true)
    {
    content.Unload();
    }
    }


    /// <summary>
    /// Allows the game to run logic such as updating the world,
    /// checking for collisions, gathering input and playing audio.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Update(GameTime gameTime)
    {
    // Allows the default game to exit on Xbox 360 and Windows
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
    this.Exit();

    // TODO: Add your update logic here

    base.Update(gameTime);
    UpdateBullets();
    UpdateKeyboard(BulletPosition);
    }
    void UpdateBullets()
    {

    }
    void UpdateKeyboard(Vector2 BulletPosition)
    {
    KeyboardState newState = Keyboard.GetState();

    // Check to see if the Spacebar is down
    if (newState.IsKeyDown(Keys.Space))
    {
    bullets.Add(new Vector2(+120, +25) + ShipPosition);
    }
    if (newState.IsKeyDown(Keys.Down))
    {
    ShipPosition.Y++;
    }
    if (newState.IsKeyDown(Keys.Up))
    {
    ShipPosition.Y--;
    }
    if (newState.IsKeyDown(Keys.Left))
    {
    ShipPosition.X--;
    }
    if (newState.IsKeyDown(Keys.Right))
    {
    ShipPosition.X++;
    }
    oldState = newState;
    }

    /// <summary>
    /// This is called when the game should draw itself.
    /// </summary>
    /// <param name="gameTime">Provides a snapshot of timing values.</param>
    protected override void Draw(GameTime gameTime)
    {
    graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

    //draw our sprite

    spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
    spriteBatch.Draw(myShip, ShipPosition, Color.White);
    for (int i = 0; i < bullets.Count; i++)
    {
    spriteBatch.Draw(Bullet, bullets[i], Color.White);
    }
    spriteBatch.End();
    base.Draw(gameTime);
    }

    }
    }

  • 7/8/2007 8:34 AM In reply to

    Re: Needs Movement

    You're not far off!

    You need to put code in your UpdateBullets function.

    for (int i = 0; i < bullets.Count; i++)
    {
    // You'll want some time factor included
    bullets[i].x += 5.0f * gameTime.EllapsedTime.TotalSeconds;
    }


    should do if you just want each bullet to move horizontally.

    The Draw() function you already have should draw the bullets at the right location, I think.  Try that.

    One other thing - you're Update() code will add LOTS of bullets, as the space bar will be down for multiple frames if it's held down for more than a split second.  You probably want to check for if the space bar was pressed last frame (or at least introduce a delay between bullets being generated).
    Muzz http://www.PositronicArts.com/blog
  • 7/8/2007 9:42 AM In reply to

    Re: Needs Movement

    First off I'd make a Bullet class, so that you can hold a Rectangle or bounding sphere as well as an active bool, speed etc.

    Now these code blurbs  is using rotations, but othewise it's exactly what you're trying to do:

        class Bullet
    {
    public Rectangle rectangle;
    public Vector2 position;
    public float speed;
    public bool isActive;
    public float Rotation;
    public BoundingSphere sphere;

    double time;


    public Bullet(Vector2 Position, GameTime gameTime, float rotation,float length)
    {
    rectangle = new Rectangle((int)Position.X, (int)Position.Y, 4, 4);
    Rotation = rotation;
    isActive = true;
    speed = 10+length;
    position = Position;
    time = gameTime.TotalGameTime.TotalSeconds+.5;
    sphere = new BoundingSphere(new Vector3(position.X,position.Y,0),2);

    }
    public void MoveBullet(GameTime gameTime)
    {
    if (time >= gameTime.TotalGameTime.TotalSeconds)
    {
    if (isActive == true)
    {
    position.X += speed * ((float)Math.Cos(Rotation));
    position.Y += speed * ((float)Math.Sin(Rotation));
    rectangle.X = (int)position.X;
    rectangle.Y = (int)position.Y;
    sphere.Center.X = position.X;
    sphere.Center.Y = position.Y;
    }
    }
    else
    {
    isActive = false;
    }
    }
    }
    //The below code is from the main game

    void AddBullet(GameTime gameTime,Vector2 position, float rotation)
    {
    if (gameTime.TotalGameTime.Milliseconds % 200 < 50&&shipobject.isactive)
    {
    if (beendone == false)
    {
    ScreenManager.audio.Fire();
    bulletsInPlay.Add(bullets[bulletcount] = new Bullet(position, gameTime, rotation,length));
    bulletcount++;
    beendone = true;
    }
    }
    else
    {
    beendone = false;
    }

    firebullet = false;
    }

    Here's the full code   But basically here's the rundown of things to do.

    Check to see if it should fire (as said before it's best to check for time since update will most likely be called 60 times or more in 1 second), then create a new bullet object from the bullet class.

    Then each update you need to move it in the desired direction.  If you're doing something like Space invaders, you'll just move Y by negative 5 or something.  If you're doing a game where it's rotated you'll use something similar to my code.

    Then using either a Rectangle or bounding sphere check to see if it hits.

    Then at some point you'll want to remove it.  Whether its by time or position.

    You are really close with what you have, it's just that a more object orriented design would make this easier for you to handle.

    http://www.freewebs.com/campelmxna/ - C# and XNA tutorials
    The only stupid mistake is the one you make twice
Page 1 of 1 (3 items) Previous Next