2013-07-27 2 views
1

Je travaille sur un jeu de pong depuis que je suis assez nouveau à XNA, et j'ai rencontré un problème ... J'ai 3 classes , "Game1.cs", "Ball.cs" et "GreenPaddle.cs".XNA - Le nom «boule» n'existe pas dans le contexte actuel

Le GreenPaddle.cs contient Rectangle gpRect, Texture2D gPtexture, Vector2 position. J'ai le mouvement et ainsi de suite, et dans la classe de balle j'ai un booléen d'intersection. Mais quand j'essaye de l'initialiser dans game1.cs j'obtiens des erreurs. Voici les classes:

GreenPaddle.cs:

public class GreenPaddle 
{ 
    Texture2D gPtexture; 
    public Vector2 position; 
    public Rectangle gpRect; 
    public Vector2 origin; 
    public int speed = 2; 

    public GreenPaddle() 
    { 

    } 

    public void LoadContent(ContentManager Content) 
    { 
     gPtexture = Content.Load<Texture2D>("greenpaddle"); 
     position = new Vector2(20, 200); 
     gpRect = new Rectangle((int)position.X, (int)position.Y, 
      gPtexture.Width, gPtexture.Height); 
    } 

    public void Update(GameTime gameTime) 
    { 
     KeyboardState keyState = Keyboard.GetState(); 

     //Movement 
     PaddleMovement(); 

     //Border Collision 
     isCollidingWithBorders(); 
     } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(gPtexture, position, gpRect, Color.LawnGreen); 
    } 
    public Boolean isCollidingWithBorders() 
    { 
     if (position.Y < 83 && gpRect.Y < 83) 
     { 
      position.Y = 83; 
      gpRect.Y = 83; 
      return true; 
     } 

     if (position.Y > 400 && gpRect.Y > 400) 
     { 
      position.Y = 400; 
      gpRect.Y = 400; 
      return true; 
     } 

     else { return false; } 

    } 

    public void PaddleMovement() 
    { 
     KeyboardState keyState = Keyboard.GetState(); 
     if (keyState.IsKeyDown(Keys.W)) 
     { 
      position.Y -= speed; 
      gpRect.Y -= speed; 
     } 
     if (keyState.IsKeyDown(Keys.S)) 
     { 
      position.Y += speed; 
      gpRect.Y += speed; 
     } 
    } 
} 

Ball.cs:

public class Ball 
{ 
    GreenPaddle gPaddle; 


    Texture2D ballTexture; 
    Vector2 ballPosition; 
    Rectangle ballRect; 
    int speed = 2; 
    bool movingUp, movingLeft; 

    public Ball(GreenPaddle paddle) 
    { 
     this.gPaddle = paddle; 
     movingLeft = true; 
     movingUp = true; 
    } 

    public void LoadContent(ContentManager Content) 
    { 
     ballTexture = Content.Load<Texture2D>("ball"); 
     ballPosition = new Vector2(380, 225); 
     ballRect = new Rectangle((int)ballPosition.X, (int)ballPosition.Y, 
      ballTexture.Width, ballTexture.Height); 

    } 

    public void Update(GameTime gameTime) 
    { 
     BallMovement(); 

    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(ballTexture, ballPosition, ballRect, Color.White); 
    } 

    public void BallMovement() 
    { 
     if (movingUp) { ballPosition.Y -= speed; } 
     if (!movingUp) { ballPosition.Y += speed; } 
     if (movingLeft) { ballPosition.X -= speed; } 
     if (!movingLeft) { ballPosition.X += speed; } 

     if (ballRect.Intersects(gPaddle.gpRect)) 
      movingLeft = !movingLeft; 
       movingUp = !movingUp; 

     if (ballPosition.Y < 85) 
     { 
      movingUp = false; 
     } 
    } 
} 

Game1.cs:

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 

    GreenPaddle gPaddle = new GreenPaddle(); 


    Texture2D BackGround; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
     graphics.PreferredBackBufferHeight = 500; 
    } 

    protected override void Initialize() 
    { 

    Ball ball = new Ball(gPaddle); 
     base.Initialize(); 
    } 

    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     BackGround = Content.Load<Texture2D>("pongBG"); 
     gPaddle.LoadContent(Content); 
     //ball.LoadContent(Content); 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// all content. 
    /// </summary> 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     gPaddle.Update(gameTime); 
     ball.Update(gameTime);//Error Line 

     base.Update(gameTime); 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     spriteBatch.Begin(); 
     spriteBatch.Draw(BackGround, new Vector2(0f, 0f), Color.White); 
     gPaddle.Draw(spriteBatch); 
     ball.Draw(spriteBatch);//Error Line 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

Thats it, je ne sais pas que faire avec la partie initialiser:/

Répondre

4

Regardez attentivement comment vous initialisez votre balle, vous le déclarez dans le cadre de la méthode (Entre les accolades). Cela signifie que vous ne pouvez pas y accéder ailleurs, ce que vous essayez de faire dans les méthodes Update et Draw.

protected override void Initialize() 
{ 
    Ball ball = new Ball(gPaddle); 
    base.Initialize(); 
} 

De plus, dès que la fonction se termine votre objet Ball est supprimé.

Cela peut être corrigé en mettant Ball ball dans votre champ de classe afin qu'il soit disponible à tous les membres de la classe, comme ceci:

Ball ball; //In the class scope 

protected override void Initialize() 
{ 
    ball = new Ball(gPaddle); 
    base.Initialize(); 
} 

Si vous n'êtes pas familier avec champs d'application, consultez this article (Nothing vraiment bien sur MSDN)

+0

Merci beaucoup pour me dire et d'expliquer :) je déteste quand les gens ne donne que le code final sans autre explication, l'homme gj: DD –

+0

@UffePuffe Pas de problème :) maintenant, je – Cyral

+0

a une autre erreur ..:/ dans ma partie1 .cs je reçois des erreurs sur 'gPaddle.Update (gameTime);' "NullReferenceExecption n'a pas été gérée". Je suis désolé si je suis ennuyeux ..: /// –

Questions connexes