Note that there are some explanatory texts on larger screens.

plurals
  1. POXNA - The name 'ball' does not exist in the current context
    text
    copied!<p>I'm working on a pong game since I'm pretty new to XNA, and I've ran into a problem... i have 3 classes, <code>"Game1.cs"</code>, <code>"Ball.cs"</code>, and <code>"GreenPaddle.cs"</code>.</p> <p>The <code>GreenPaddle.cs</code> contains <code>Rectangle gpRect</code>, <code>Texture2D gPtexture</code>, <code>Vector2 position</code>.</p> <p>I have movement and such, and in the ball class I have an intersect boolean. But when i try to initialize it in <code>game1.cs</code> i get errors. Here are the classes:</p> <p>GreenPaddle.cs:</p> <pre><code>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&lt;Texture2D&gt;("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 &lt; 83 &amp;&amp; gpRect.Y &lt; 83) { position.Y = 83; gpRect.Y = 83; return true; } if (position.Y &gt; 400 &amp;&amp; gpRect.Y &gt; 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; } } } </code></pre> <p>Ball.cs:</p> <pre><code>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&lt;Texture2D&gt;("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 &lt; 85) { movingUp = false; } } } </code></pre> <p>Game1.cs:</p> <pre><code> 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(); } /// &lt;summary&gt; /// LoadContent will be called once per game and is the place to load /// all of your content. /// &lt;/summary&gt; protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); BackGround = Content.Load&lt;Texture2D&gt;("pongBG"); gPaddle.LoadContent(Content); //ball.LoadContent(Content); } /// &lt;summary&gt; /// UnloadContent will be called once per game and is the place to unload /// all content. /// &lt;/summary&gt; protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// &lt;summary&gt; /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// &lt;/summary&gt; /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt; 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); } /// &lt;summary&gt; /// This is called when the game should draw itself. /// &lt;/summary&gt; /// &lt;param name="gameTime"&gt;Provides a snapshot of timing values.&lt;/param&gt; 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); } } </code></pre> <p>Thats it, I don't know what to do with the initialize part :/</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload