Reputation: 75
So I am developing a simple C# program that allows you to move around a graphic and have it fire bullets that travel from the player to the position of the mouse at the time of the mouse click. I'm still new to C# but have some experience with Java and Python.
I am fully able to move my character with the WASD but unable to get the bullets to be drawn let alone get them to move as they update.
Where might the bug be?
The projectile object's draw method? The Game1 class's update method for the user input? The projectile's object's direction setting?
Here is the full code: http://pastebin.com/j5QVLKU3
I left the player class out, but it has nothing more then a few player variables like health.
Upvotes: 1
Views: 232
Reputation: 4280
For start, could you move
mouseStateCurrent = Mouse.GetState();
if (mouseStateCurrent.LeftButton == ButtonState.Pressed)
{
if (mouseStatePrevious.LeftButton != ButtonState.Pressed)
{
AddProjectile(player.positionPlayer);
mouseStatePrevious = mouseStateCurrent; //<-- This line
}
}
//<-- Here
Once you press your button, you're never gonna enter that if again.
Furthermore, this in your Projectile
Player playerObject = new Player();
Game1 gameObject = new Game1();
has to look like:
Player playerObject;
Game1 gameObject;
public Projectile(Player player, Game1 game)
{
playerObject = player;
gameObject = game;
}
Since you're using those only to get the origin and destination for the bullet, it would be better if you just calculated those outside, and passed them with the initialize. (because it would remove Projectile's need to know what Game1 and Player objects looked like)
You could also completely remove the Initialize method and just use Projectile's constructor, that seems more natural. (if you use the constructor for this, then you know that any time you have an instance, you can use it without having to call additional methods)
I don't think you really need the bulletOrigin in your Projectile class, you could just use the position, and move it from there to the bulletDestination.
Also, Vector2 class is made with operator overloaded methods, so you could use
positionBullet += directionLine*velocity;
instead of
positionBullet = Vector2.Add(positionBullet, directionLine * velocity);
Other than those, your code seems fine! Tell me if it still doesn't work after these changes. Also, use your debugger to step into it, see if you can find anything interesting. :)
Upvotes: 6