【问题标题】:Code Efficiency, Phasing代码效率,分阶段
【发布时间】:2013-08-01 09:08:12
【问题描述】:

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

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

隐藏剧透

代码:

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

    }      

问题是这对我来说似乎不是很有效,现在是的,我已经考虑过创建一个具有纹理和位置等属性的新类,然后我只是制作该类的新对象并以这种方式使其变得更好,但我仍然必须在该类中使用此阶段代码。我只想要代码的替代方案。

【问题讨论】:

  • 您在寻找什么?也许写下你想要完成的事情,有人可以建议要实现的模式。
  • 嗯,也许我只是想知道 switch 语句是否真的是执行此阶段操作的最佳方式?有没有更好的方法来做这样的相位?我只是觉得有点笨拙。
  • 您的代码基本上受到不使用类来捆绑您的属性的影响。您的代码没有什么特别的问题,而要求更简洁的代码而不使用类是浪费时间。
  • @Ricehead 如果您在简洁的段落中描述代码应该做什么,那么建议更改会容易得多。即,要使用哪种类型的模式,我可以为您指出一些所述模式的示例。浏览一下您的代码,您似乎需要抽象工厂和具体工厂。
  • @Kai Hartmann 如果您阅读我在问题底部的段落,我已经知道这一点,但这无关紧要,因为即使将这个方法封装在一个类中仍然会导致我基本上使用相同的代码。跨度>

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


【解决方案1】:

您可能可以组合其中一些开关。

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

可能变成:

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

【讨论】:

  • 好的,我认为
  • 1|33|3 都导致 3。这种转变是为这两个值腾出空间,假设foodNumber 最多需要 24 位,food1Phase 最多需要 8 位。
猜你喜欢
  • 2010-10-27
  • 1970-01-01
  • 2013-06-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-01
  • 1970-01-01
相关资源
最近更新 更多