XNA Creators Club Online
Page 1 of 1 (6 items)
Sort Posts: Previous Next

Problems with "ball" in Turtorial 2D under xna3.0

Last post 23/08/2008 0:09 by CDarklock. 5 replies.
  • 05/08/2008 16:47

    Problems with "ball" in Turtorial 2D under xna3.0

    I have a Problem with the Code from the Turtorial 2D.

    I use the XNA 3.0

    The Code:

     

            public void UpdateCannonBalls()  
            {  
                foreach (GameObject ball in cannonBalls)  
                {  
                    if (ball.alive)  
                    {  
                        ball.position += ball.velocity;  
                        if (!viewportRect.Contains(new Point((int)ball.position.X, (int)ball.position.Y)))  
                        {  
                            ball.alive = false;  
                            continue;  
                        }  
                    }  
                }  
            } 

    This is the same code like the downloaded code.

    if (ball.alive) is an NullReferenceExeption wich was not handled.

    I dont know where i must create the instance with NEW

    Thank you for help

     

  • 05/08/2008 17:01 In reply to

    Re: Problems with "ball" in Turtorial 2D under xna3.0

    You did something wrong somewhere and add a null object to the list.

    This shows you how to create the cannonballs.


    Jim Perry - Microsoft XNA MVP
    If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job.
      Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki.
        Please mark posts as Answers or Good Feedback when appropriate.
  • 05/08/2008 17:11 In reply to

    Re: Problems with "ball" in Turtorial 2D under xna3.0

    Sorry, i find my mistake in the Top of the Code

    i wrote:
    for (int i = 0; i < 0; i++)

    It must be:
    for (int i = 0; i < maxCannonBalls; i++)

    so the loop never initialisize a Objekt...

    .....

  • 09/08/2008 17:04 In reply to

    Re: Problems with "ball" in Turtorial 2D under xna3.0

    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.

  • 21/08/2008 19:44 In reply to

    Re: Problems with "ball" in Turtorial 2D under xna3.0

    There is a pretty obvious mistake in UpdateCannonBalls() which makes the cannonballs disappear before they are out of the viewport:

     

    if(!viewportRect.Contains(new Point((int)ball.position.X,(int)ball.position.Y))) 

    should be

    if(!viewportRect.Contains(new Point((int)ball.position.X,(int)ball.position.Y+ball.sprite.Height)))

    You can easily validate this by reducing the velocity to like 0.5f. Please note and fix in the tutorial.

  • 23/08/2008 0:09 In reply to

    Re: Problems with "ball" in Turtorial 2D under xna3.0

    Telenex:
    I normalize the velocity to get the direction the cannon is pointing in

    That's not what Normalize() does. Normalize() makes the length of your vector equal to 1. It's not the same as a "surface normal".

    BooTes:
    There is a pretty obvious mistake in UpdateCannonBalls() which makes the cannonballs disappear before they are out of the viewport:

    However, given the enemy position boundaries in the sample, there cannot possibly be anything there for it to hit anyway... so calling it a "mistake" seems overly critical.

Page 1 of 1 (6 items) Previous Next