gpt4 book ai didi

c# - XNA - 当前上下文中不存在名称 'ball'

转载 作者:太空宇宙 更新时间:2023-11-03 21:44:08 25 4
gpt4 key购买 nike

因为我是 XNA 的新手,所以我正在开发一个乒乓球游戏,但我遇到了一个问题......我有 3 个类,"Game1.cs""Ball.cs""GreenPaddle.cs"

GreenPaddle.cs 包含Rectangle gpRectTexture2D gPtextureVector2 position

我有运动等等,在球类中我有一个相交 bool 值。但是当我尝试在 game1.cs 中初始化它时,我得到了错误。以下是类(class):

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;
}
}
}

球.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;
}
}
}

游戏1.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);
}
}

就是这样,我不知道如何处理初始化部分:/

最佳答案

仔细看看你是如何初始化你的球的,你在方法的范围内声明它(大括号之间)。这意味着您无法在其他任何地方访问它,而您正试图在 UpdateDraw 方法中进行访问。

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

此外,一旦函数结束,您的 Ball 对象就会被删除。

这可以通过将 Ball ball 放入你的类范围内来解决,这样它对类的所有成员都可用,如下所示:

Ball ball;  //In the class scope

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

如果您不熟悉作用域,请查看 this文章(MSDN 上没什么好东西)

关于c# - XNA - 当前上下文中不存在名称 'ball',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17897370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com