Having a hard time with cases (c#/XNA)
本文关键字:XNA cases hard time with Having | 更新日期: 2023-09-27 18:16:03
我试图为编程课程中的作业编程Pong,但当我试图使用用例制作主菜单时,错误弹出。代码位于XNA窗口游戏的更新区域。游戏案例中的代码自己运行得很好。当我尝试制作主菜单并将我的pong代码移动到播放案例
时,问题就出现了。switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
break;
case GameState.Playing:
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
if (POPBox.Y >= 373)
{
POPBox.Y += 0;
}
else
{
POPBox.Y += PlayersSpeed;
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
if (POPBox.Y <= 0)
{
POPBox.Y += 0;
}
else
{
POPBox.Y += -PlayersSpeed;
}
}
// Ball Limets
if (BallBox.Y <= 0)
VelocityY *= -1;
if (BallBox.Y >= 463)
VelocityY *= -1;
if (BallBox.X <= 0)
VelocityX *= -1;
//Collision Detection (Runs this code if it hits the player one's paddle)
if (BallBox.Intersects(POPBox))
{
//Used to deflect in different directions for some veriety
if (PlayersSpeed > 0)
VelocityY += 3;
if (PlayersSpeed < 0)
VelocityY -= 3;
VelocityX *= -1;
HitCount++;
ShockerGeneratorPlayerOne();
//Stopping the no slope bug. If it wants to bounce perfectly straight, it is slightly shifty to fix that error.
if (VelocityY == 0)
VelocityY = VelocityY += 3;
if (VelocityX == 0)
VelocityX = VelocityX += 3;
//speed control
if (VelocityX > 10)
VelocityX = 10;
if (VelocityY > 10)
VelocityY = 10;
}
// Runs this code if the ball hits player two's paddle
if (BallBox.Intersects(PTPBox))
{
VelocityX *= -1;
ShockerGeneratorPlayerTwo();
if (VelocityY == 0)
VelocityY = VelocityY += 3;
if (VelocityX == 0)
VelocityX = VelocityX += 3;
}
//Object a collision
if (BallBox.Intersects(ShocObjectARectangle))
{
VelocityY *= -1;
}
if (BallBox.Intersects(ShocObjectBRectangle))
{
VelocityX *= -1;
}
// If Player One Loses
if (BallBox.X >= 790)
{
PlayerOneLoses();
}
//Player Two's "AI" and limets
if (PTPBox.Y >= 173)
PTPBox.Y += 0;
else
PTPBox.Y = BallBox.Y;
if (PTPBox.Y <= 0)
PTPBox.Y += 0;
else
PTPBox.Y = (BallBox.Y -30);
//Object A movement code
ShocObjectARectangle.X += ObjectASpeed;
if (ShocObjectARectangle.X <= 80)
ObjectASpeed *= -1;
else if (ShocObjectARectangle.X >= 600)
ObjectASpeed *= -1;
//Object B movement code
ShocObjectBRectangle.Y += ObjectBSpeed;
if (ShocObjectBRectangle.Y <= 0)
ObjectBSpeed *= -1;
else if (ShocObjectBRectangle.Y >= 415)
ObjectBSpeed *= -1;
// Ball Velocity
BallBox.Y += -VelocityY;
BallBox.X += VelocityX;
PlayersSpeed = 10;
}
}
}
//Called Every Hit
public void ShockerGeneratorPlayerOne()
{
Random rnd = new Random();
RanShocMatch = rnd.Next(10);
if (RanShocMatch == 1)
{
//Speed Boost!
VelocityX = (VelocityX - 1);
VelocityY = (VelocityY - 1);
}
else if (RanShocMatch == 2)
{
if (ObjectBCalled == false)
{
ShocObjectBRectangle = new Rectangle(362, 200, 10, 100);
ObjectBCalled = true;
}
}
else if (RanShocMatch == 3)
{
if (ObjectACalled == false)
{
ShocObjectARectangle = new Rectangle(80, 200, 100, 10);
ObjectACalled = true;
}
}
}
//Called Every Hit
public void ShockerGeneratorPlayerTwo()
{
Random rnd = new Random();
RanShocMatch = rnd.Next(5);
if (RanShocMatch == 1)
{
//Speed Boost!
VelocityX = (VelocityX + 3);
VelocityY = (VelocityY + 3);
}
}
//Called When Player One Loses
public void PlayerOneLoses()
{
// MediaPlayer.Play(LosingBeep);
VelocityX = -BasicVelocity;
VelocityY = BasicVelocity;
BallBox.X += -360;
if (HitCount > highScore)
highScore = HitCount;
HitCount = 0;
break;
}
}
}
base.Update(gameTime);
}
错误:
- 命名空间不能直接包含成员,如字段或方法
- 类型或名称空间定义,或文件结束
- 控制不能通过一个案例标签
- 没有要中断或继续的封闭循环
我刚来这些论坛,我的格式很糟糕,如果你有任何改进的建议,我将进行编辑。
看起来你高亮显示代码并向下移动,试试这个
更新方法 switch (CurrentGameState)
{
case GameState.MainMenu:
if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
btnPlay.Update(mouse);
break;
case GameState.Playing:
if (Keyboard.GetState().IsKeyDown(Keys.Down))
{
if (POPBox.Y >= 373)
{
POPBox.Y += 0;
}
else
{
POPBox.Y += PlayersSpeed;
}
}
if (Keyboard.GetState().IsKeyDown(Keys.Up))
{
if (POPBox.Y <= 0)
{
POPBox.Y += 0;
}
else
{
POPBox.Y += -PlayersSpeed;
}
}
// Ball Limets
if (BallBox.Y <= 0)
VelocityY *= -1;
if (BallBox.Y >= 463)
VelocityY *= -1;
if (BallBox.X <= 0)
VelocityX *= -1;
//Collision Detection (Runs this code if it hits the player one's paddle)
if (BallBox.Intersects(POPBox))
{
//Used to deflect in different directions for some veriety
if (PlayersSpeed > 0)
VelocityY += 3;
if (PlayersSpeed < 0)
VelocityY -= 3;
VelocityX *= -1;
HitCount++;
ShockerGeneratorPlayerOne();
//Stopping the no slope bug. If it wants to bounce perfectly straight, it is slightly shifty to fix that error.
if (VelocityY == 0)
VelocityY = VelocityY += 3;
if (VelocityX == 0)
VelocityX = VelocityX += 3;
//speed control
if (VelocityX > 10)
VelocityX = 10;
if (VelocityY > 10)
VelocityY = 10;
}
// Runs this code if the ball hits player two's paddle
if (BallBox.Intersects(PTPBox))
{
VelocityX *= -1;
ShockerGeneratorPlayerTwo();
if (VelocityY == 0)
VelocityY = VelocityY += 3;
if (VelocityX == 0)
VelocityX = VelocityX += 3;
}
//Object a collision
if (BallBox.Intersects(ShocObjectARectangle))
{
VelocityY *= -1;
}
if (BallBox.Intersects(ShocObjectBRectangle))
{
VelocityX *= -1;
}
// If Player One Loses
if (BallBox.X >= 790)
{
PlayerOneLoses();
}
//Player Two's "AI" and limets
if (PTPBox.Y >= 173)
PTPBox.Y += 0;
else
PTPBox.Y = BallBox.Y;
if (PTPBox.Y <= 0)
PTPBox.Y += 0;
else
PTPBox.Y = (BallBox.Y -30);
//Object A movement code
ShocObjectARectangle.X += ObjectASpeed;
if (ShocObjectARectangle.X <= 80)
ObjectASpeed *= -1;
else if (ShocObjectARectangle.X >= 600)
ObjectASpeed *= -1;
//Object B movement code
ShocObjectBRectangle.Y += ObjectBSpeed;
if (ShocObjectBRectangle.Y <= 0)
ObjectBSpeed *= -1;
else if (ShocObjectBRectangle.Y >= 415)
ObjectBSpeed *= -1;
// Ball Velocity
BallBox.Y += -VelocityY;
BallBox.X += VelocityX;
PlayersSpeed = 10;
break;
}
base.Update(gameTime);
和其他3个方法
//Called Every Hit
public void ShockerGeneratorPlayerOne()
{
Random rnd = new Random();
RanShocMatch = rnd.Next(10);
if (RanShocMatch == 1)
{
//Speed Boost!
VelocityX = (VelocityX - 1);
VelocityY = (VelocityY - 1);
}
else if (RanShocMatch == 2)
{
if (ObjectBCalled == false)
{
ShocObjectBRectangle = new Rectangle(362, 200, 10, 100);
ObjectBCalled = true;
}
}
else if (RanShocMatch == 3)
{
if (ObjectACalled == false)
{
ShocObjectARectangle = new Rectangle(80, 200, 100, 10);
ObjectACalled = true;
}
}
}
//Called Every Hit
public void ShockerGeneratorPlayerTwo()
{
Random rnd = new Random();
RanShocMatch = rnd.Next(5);
if (RanShocMatch == 1)
{
//Speed Boost!
VelocityX = (VelocityX + 3);
VelocityY = (VelocityY + 3);
}
}
//Called When Player One Loses
public void PlayerOneLoses()
{
// MediaPlayer.Play(LosingBeep);
VelocityX = -BasicVelocity;
VelocityY = BasicVelocity;
BallBox.X += -360;
if (HitCount > highScore)
highScore = HitCount;
HitCount = 0;
}