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);
}
}
Position += touch_direction * speed_difference * (float)gameTime.ElapsedGameTime.TotalSeconds;note that bothtouch_directionandspeed_differenceare 2D vectors, so they can not be multiplied that easily! – Gajoo Apr 16 '12 at 18:21touch_direction.length()instead. – Gajoo Apr 16 '12 at 18:39