1

プログラミング コースの課題で Pong をプログラミングしようとしていますが、ケースを使用してメイン メニューを作成しようとするとエラーが発生します。コードは、XNA ウィンドウのゲームの更新領域にあります。再生ケースのコードは、それ自体で問題なく動作しました。メインメニューを作成しようとして、ポンコードをプレイングケースに移動したときに問題が発生しました

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

エラー:

  1. 名前空間には、フィールドやメソッドなどのメンバーを直接含めることはできません
  2. タイプまたは名前空間の定義、またはファイルの終わりが必要です
  3. コントロールは 1 つのケース ラベルからフォールスルーできません
  4. 中断または継続する囲みループはありません

私はこれらのフォーラムに不慣れで、私の書式設定はひどいものです。改善するためのヒントがあれば、編集します。

4

1 に答える 1

1

コードを強調表示して下に移動したように見えたので、これを試してください

更新方法

 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;
                } 
于 2013-05-30T19:18:55.960 に答える