代码效率,阶段性

时间:2013-08-01 09:08:13

标签: c# .net windows-phone-7 phase

好的所以我正在重新审视我制作的原型游戏并且从未完成过。当原型设计时,你总是想出最好的想法哈哈。这对我来说很难解释,但基本上屏幕上有游戏对象可以“吃掉”然后我基本上用新点值和纹理重新生成它。

当纹理与屏幕上的嘴相交时,当游戏对象被“吃掉”时,会调用下一个阶段方法

Hide Spoiler

代码:

void NextPhase(int food)
    {

        switch (foodNumber)
        {
                //Detects what food object has been eaten
            case 1:
                {
                    food1Phase++;
                    break;
                }
            case 2:
                {
                    food2Phase++
                    break;
                }
            case 3:
                {
                    food3Phase++;
                    break;
                }
                //
        }
        switch (foodNumber)
        {
            case 1:
                {

                    switch (food1Phase)
                    {
                    //Each phase is a new set of Textures, Points Values and specific spawn delay.
                    //Multiplier sotrage tells another method
                    //what food has been eaten and decides whether to increase the score mulitplier
                    //the position properties reset it to a location off screen and then it flies on screen
                    //for the player to collect
                    //delayCompleted false activates another method that starts timing the spawn
                    //delay
                case 1:
                    {


                        multiplierStorage("Gay Bacon");
                        food1Position.X = -200;
                        food1Position.Y = 60;
                        food1 = contentManager.Load<Texture2D>("Food2Img");
                                food1Height = food1.Bounds.Height + 20;
                                food1Width = food1.Bounds.Width + 30;
                                food1Speed.X = 0;
                                food1Speed.Y = 0;
                        delayCompleted = false;
                        if (gameOverallTimer < 30.0)
                        {

                            timeWanted = gameOverallTimer + 4.0;
                        }
                        break;
                    }
                case 2:
                    {
                        food1 = contentManager.Load<Texture2D>("Food6");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        multiplierStorage("Bacon");




                        break;
                    }
                case 3:
                    {
                        food1 = contentManager.Load<Texture2D>("Food1Img");
                        food1Height = food1.Bounds.Height + 20;
                        food1Width = food1.Bounds.Width + 30;
                        food1Position.X = -100;
                        food1Position.Y = 60;
                        food1Speed.X = 0;
                        food1Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        food1Phase = 0;
                        break;
                    }
                }
            }
        }

            case 2:
                {

                    switch (food2Phase)
                    {
                        food2 = contentManager.Load<Texture2D>("Food4");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;

                        multiplierStorage("Bacon");
                        break;
                    }
                case 2:
                    {


                        food2 = contentManager.Load<Texture2D>("Food5");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        vegetableEatenMultiplier();
                        lives = lives - 1;
                        UpdateLives();
                        break;
                    }
                case 3:
                    {
                        food2 = contentManager.Load<Texture2D>("Food2Img");
                        food2Height = food2.Bounds.Height + 30;
                        food2Width = food2.Bounds.Width + 30;
                        food2Position.X = 850;
                        food2Position.Y = 200;
                        food2Speed.X = 0;
                        food2Speed.Y = 0;
                        food2Phase = 0;
                        multiplierStorage("Steak");
                        break;
                    }
                }
            }



            case 3:
                {

                    switch (food3Phase)
                    {

                        food3Position.X = 600;
                        food3Position.Y = -100;
                        food3Speed.X = 0;
                        food3Speed.Y = 0;
                        food3Phase = 0;
                        multiplierStorage("Gay Bacon");
                        break;
                    }
                }
            }
        }

    }      

事情是这对我来说似乎不是很有效,现在是的我已经想过用Texture创建一个新类和位置Etc.属性然后我只是创建该类的新对象并使它更好,但我仍然必须在该类中使用此阶段代码。我想要一个替代代码。

1 个答案:

答案 0 :(得分:0)

你可以组合其中一些开关。

switch (foodNumber)
{
    case 1:
        switch (food1Phase)
        {
            case 1:

可能成为:

switch (foodNumber << 8 | food1Phase)
{
    case (1 << 8 | 1):