On a side note, I've created a fix to another problem related to the ball in the 2D tutorial. As written, the ball will be instantiated inside of the cannon sprite, and if the cannon moves or you draw your sprites in the wrong order you'll see the ball poke out in the wrong spot. A small modification to the FireCannonBall method fixes this:
| 1 |
private void FireCannonBall() |
| 2 |
{ |
| 3 |
foreach (CannonBallGameObject ball in CannonBalls) |
| 4 |
{ |
| 5 |
if (ball.Alive) |
| 6 |
{ |
| 7 |
continue; |
| 8 |
} |
| 9 |
|
| 10 |
ball.Alive = true; |
| 11 |
ball.Velocity = new Vector2((float)Math.Cos(Cannon.Rotation), (float)Math.Sin(Cannon.Rotation)) * 5.0f; |
| 12 |
|
| 13 |
ball.Position = ball.Velocity; |
| 14 |
ball.Position.Normalize(); |
| 15 |
ball.Position *= 20; |
| 16 |
ball.Position += Cannon.Position; |
| 17 |
break; |
| 18 |
} |
| 19 |
} |
| 20 |
|
I normalize the velocity to get the direction the cannon is pointing in, then offset the cannon position by that amount to stick the ball right at the cannon muzzle.