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 a bit new to XNA but I wanted to write a simple program that would fire a cannon ball from a cannon at a 45 degree angle. It works fine outside of my keyboard i/o if statement, but when I encapsulate the code around an if statement checking to see if the user hits the space bar, the sprite will draw one frame at a time every time the space bar is hit.

This is the code in question

if (currentKeyboardState.IsKeyUp(Keys.Space) && previousKeyboardState.IsKeyDown(Keys.Space) && !skullBall.Alive)
        {
            //works outside the keyboard input if statement   //{
            skullBall.Position = cannon.Position;
            skullBall.DeltaY = -(float)(Math.Sin(MathHelper.ToRadians(45)) * 50/*39.7577*/ * time + 0.5 * (gravity * (time * time)));
            skullBall.DeltaX = (float)(Math.Cos(MathHelper.ToRadians(45)) * 50/*39.7577*/ * time);
            skullBall.Alive = true;           //}
        }

The skull ball represents the cannon ball and the cannon is just the starting point. DeltaX and DeltaY are the values I’m using to update the cannon balls position per update. I know it's dumb to have the cannon ball start at the cannons position every time the update is called but it’s not really noticeable right now.

I was wondering if after examining my code, if anyone noticed any errors that would cause the sprite to display frame by frame instead of drawing it as a full animation of the cannon ball leaving the cannon and moving from there.

share|improve this question

1 Answer 1

up vote 2 down vote accepted
if (currentKeyboardState.IsKeyUp(Keys.Space) && previousKeyboardState.IsKeyDown(Keys.Space) && !skullBall.Alive)

Every frame the above statement is checked and of course its only true when you've pressed the space bar while it was depressed last frame. So all the code in the statement is indeed only executed once every time that you press space bar.

I think this is what you meant to write

if (currentKeyboardState.IsKeyUp(Keys.Space) && previousKeyboardState.IsKeyDown(Keys.Space) && !skullBall.Alive)
{
    skullBall.Position = cannon.Position;
    skullBal.Alive = true;
}

if(skullBal.Alive)
{
    skullBall.DeltaY = -(float)(Math.Sin(MathHelper.ToRadians(45)) * 50/*39.7577*/ * time + 0.5 * (gravity * (time * time)));
    skullBall.DeltaX = (float)(Math.Cos(MathHelper.ToRadians(45)) * 50/*39.7577*/ * time);
}

Try to think more about why this works and your original code doesn't. You have to think what needs to happen frame-by-frame and programming accordingly. Think of behaviour but don't directly implement but deconstruct into decisions that can be made each frame.

share|improve this answer
    
Thanks a lot, your definitely right and I should have noticed the behavior myself but I was too set on thinking that the whole code block belonged inside of the if statement. Thanks again and I’ll take program behaviors into consideration in future projects. –  user1489599 Dec 7 '12 at 14:35

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.