Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm working on a small game, and as my title states, the bullet is not moving properly. When I click, it simply draws the bullet at the set position, and remains stationary. For some reason, the Update function of the bullet doesn't constantly run. So any help would be appreciated, thanks. Currently, I have two classes (not including the Game1.cs), which are: Bullet class and Humvee(player) class.

Here's the code for my Bullet class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Humvee
{
public class Bullet
{
    // Unique id assigned to each bullet
    int bullet_id;

    public bool bullet_active = false;

    // This declares the position of the bullet upon creation (is changed in constructor for debugging purposes)
    public Vector2 Position = new Vector2(400, 240);

    // This declares the direction of the bullet upon creation (not used for now)
    public Vector2 Direction = new Vector2(0, 0);

    // This declares the speed of the bullet upon creation (not used for now)
    public Vector2 Speed = new Vector2(0, 0);

    // Stores the touch location for each bullet
    public Vector2 Destination = new Vector2(0, 0);

    public Bullet()
    {
    }

    // Constructor that assigns each bullet a unique id, starting position, and a boolean to know whether it's active or not
    public Bullet(int id, Vector2 destination)
    {
        bullet_id = id;
        Position.X = 100;
        Position.Y = 240;
        Destination = destination;
    }

    public void Update(GameTime gameTime, Vector2 touch_location, Vector2 touch_direction)
    {
            // Finds the difference between the location of the touch and the bullet
            Vector2 speed_difference = new Vector2(0,0);
            speed_difference.X = Math.Abs(Destination.X - Position.X);
            speed_difference.Y = Math.Abs(Destination.Y - Position.Y);

            //Update the Sprite and change it's position based on the passed in speed, direction and elapsed time.
            Position += touch_direction * speed_difference * (float)gameTime.ElapsedGameTime.TotalSeconds;

    }

    //Draw the sprite to the screen
    public void Draw(SpriteBatch theSpriteBatch, Texture2D Bullet_Texture)
    {
        theSpriteBatch.Draw(Bullet_Texture, Position, Color.White);
    }
}
}

Here's the Humvee class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Humvee
{
class Humvee
{
    // Position of the player
    public Vector2 Position = new Vector2(400,240);

    // Will be used to assign a unique id to each bullet
    public int b_id = 1;

    Bullet testbullet = new Bullet();

    // It creates a new bullet and calls the Update function from Bullet class which causes the bullet to 'shoot'
    public void ShootBullet(GameTime gameTime, Vector2 touch_location, Vector2 touch_direction)
    {
        testbullet = new Bullet(1, touch_location);
        testbullet.bullet_active = true;
        testbullet.Update(gameTime, testbullet.Destination, touch_direction);

    }

    public void Update(GameTime gameTime, Vector2 touch_location, Vector2 touch_direction)
    {
        ShootBullet(gameTime, touch_location, touch_direction);   
    }


    //Draw the sprite to the screen
    public void Draw(SpriteBatch theSpriteBatch, Texture2D humveesprite, Texture2D bulletsprite)
    {
        testbullet.Draw(theSpriteBatch, bulletsprite);
        theSpriteBatch.Draw(humveesprite, new Vector2(400, 240), Color.White);

    }

}
}

And here's part of the Game1 class code:

Humvee testhumvee;

    // Stores the sprites of the objects
    Texture2D background;
    Texture2D bullet;
    Texture2D player;

    Vector2 touch_direction; // Stores the direction of the tap
    Vector2 touch_location; // Stores the location of the tap

    bool shoot_isactive = false;

protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        #region Touch code
        //Touch
        T_Collec = TouchPanel.GetState();
        foreach (TouchLocation tl in T_Collec)
        {
            while (TouchPanel.IsGestureAvailable)
            {
                GestureSample gesture = TouchPanel.ReadGesture();
                if (gesture.GestureType == GestureType.Tap)
                {
                    if (tl.Position.X > testhumvee.Position.X && tl.Position.Y > testhumvee.Position.Y)
                    {
                        touch_direction = new Vector2(1, 1);

                    }
                    else if (tl.Position.X < testhumvee.Position.X && tl.Position.Y > testhumvee.Position.Y)
                    {
                        touch_direction = new Vector2(-1, 1);
                    }
                    else if (tl.Position.X < testhumvee.Position.X && tl.Position.Y < testhumvee.Position.Y)
                    {
                        touch_direction = new Vector2(-1, -1); 
                    }
                    else if (tl.Position.X > testhumvee.Position.X && tl.Position.Y < testhumvee.Position.Y)
                    {
                        touch_direction = new Vector2(1, -1);
                    }

                    touch_location = tl.Position;
                    shoot_isactive = true;
                 }

            }
        #endregion

            // TODO: Add your update logic here
            if (shoot_isactive == true)
            {
                testhumvee.Update(gameTime, touch_location, touch_direction);
            }
            base.Update(gameTime);
        }
    }
share|improve this question
    
what does this line suppose to mean? Position += touch_direction * speed_difference * (float)gameTime.ElapsedGameTime.TotalSeconds; note that both touch_direction and speed_difference are 2D vectors, so they can not be multiplied that easily! –  Ali.S Apr 16 '12 at 18:21
    
well that is supposed to be the formula used to constantly change the position. I have seen it be used before, even with vectors. –  Carlo Rodriguez Apr 16 '12 at 18:34
    
well you compute product of two vectors, it's either cross product (which is only valid for 3d vectors) or dot product(which result in scalar value). none of those meets what you need there. I guess you have to use touch_direction.length() instead. –  Ali.S Apr 16 '12 at 18:39
    
+1 for including enough source code. When finding weird logic issues often there is not enough to see what could be the cause. –  James Apr 16 '12 at 19:17
    
Thanks. I appreciate all of your help. :) –  Carlo Rodriguez Apr 16 '12 at 19:44

3 Answers 3

up vote 1 down vote accepted

There are a couple of issues with the layout that could be causing you confusion.

First up is that your Update() method of the Humvee seems to be a rather bad name as its not Updating so much as Shooting a bullet.

Second up is that you are never calling Update on the bullet object anywhere. I can gather that you may have wanted to do it at the start of the Bullet's Draw() method so that it will calculate its new position and then draw itself there. (While not important right now you may want to consider doing the Update elsewhere as you will generally only want to do Draw() specific code when drawing, but that is another matter all together).

Since you are seeing things I am going to assume that at least draw is being done and so my above comment would suffice to get the bullet moving as long as you also adhere to @Gajet's comments. You have seen

Position += firedDirection + velocity + elapsedTime;

as the way to update bullets, but you need to know that firedDirection is a vector and velocity and elapsedTime should be real values (floats or doubles, whatever you are using). So if you get very unexpected results that would be the next area to look into.

share|improve this answer
    
Actually, I have seen that formula the way you wrote it, but I have also seen it done with vectors. I'll try it as @Gajet had mentioned just in case though. As for your second point though, I don't understand what you mean that I'm not calling update on the bullet object. I call it in the ShootBullet function. –  Carlo Rodriguez Apr 16 '12 at 19:06
    
@CarloRodriguez You call it once when it is fired, so it gets Updated, once.. You never call it after that so it will never update the position. When you tap again its going to fire a New bullet, the previous one will be gone to the wind. It will then update the new bullet as it did the first one. Again this will only happen Once. –  James Apr 16 '12 at 19:10
    
Okay, well since for now I'd like to get at least one bullet moving, I decided to create the new bullet inside the Humvee class but outside of the functions in order to keep it from creating a new bullet every time, and I simply set the Direction to touch_location of the bullet, but now it moves to the location, but only when I hold down the left click. –  Carlo Rodriguez Apr 16 '12 at 20:26
    
@CarloRodriguez Then that means you are still only updating the bullet when you are doing the code that says you are touching the screen. –  James Apr 16 '12 at 21:47
    
But I call the Humvee Update outside of the touching code. –  Carlo Rodriguez Apr 16 '12 at 22:22

What you want to do is to inherit your Humvee and Bullet classes from DrawableGameComponent, then add them to the Game.Components list as you create them.

This way Games base implementation of Update and Draw will call these methods for you, to fire a bullet you would call Humvee.ShootBullet directly and Humvee.Update method should no longer call ShootBullet (or you wind up firing a bullet every frame).

Add the following line to Humvee.ShootBullet:

Game.Components.Add(testbullet);

Since this allows you to fire any number of bullets you need to remove them from the Components collection at some point, like when they hit their target or go out of the game area/screen.

share|improve this answer
    
I was eventually going to use Lists in order to store all the bullets, but for now I only wanted to focus on the bullet movement and making it work properly before continuing. –  Carlo Rodriguez Apr 16 '12 at 22:20
    
@CarloRodriguez This is the builtin functionality, you can do it other ways of course, but in your case this is the direction I would pick to start with, it is well documented and simple way of getting started with XNA. –  Daniel Carlsson Apr 16 '12 at 22:37

Your architecture and you code are flawed.

You forget to update existing bullets. The bullet is updated only through your humvee::update. And each time you update your humvee you shoot a new one.

When you stop updating your humvee you stop updating the last bullet...

You need to update all bullets from somewhere else.

share|improve this answer
    
Your logic is also flawed a bit The Update of the Humvee only calls Draw() on the bullet after it shoots it out.. It also does not call update so your description is a bit off as to what is happening. –  James Apr 16 '12 at 18:45
    
@James you might want to review the code. From class Humvee I don't see any call to Draw in Humvee::Update: public void Update(GameTime gameTime, Vector2 touch_location, Vector2 touch_direction) { ShootBullet(gameTime, touch_location, touch_direction); } –  Coyote Apr 16 '12 at 18:52
    
@James You might want to read my answer as well. –  Coyote Apr 16 '12 at 18:54
    
Apologies, I meant the Draw of the Humvee only calls Draw on the last fired bullet (as firing again will replace any existing bullet).. I typoed there. the Humvee Update does what you say, just shoots a bullet. –  James Apr 16 '12 at 18:57
    
@James Therefore the bullet stays in place because it`s never... Updated; as in its position never changes. I don't think my answer deserved a down vote based on your mistake. –  Coyote Apr 16 '12 at 20:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.