2013-08-01 56 views
-1

好吧所以我正在重温一个我制作的原型游戏并且从未完成过,当原型设计时你并不总是想出最好的想法哈哈。这对我来说很难解释,但基本上屏幕上有可以“吃掉”的游戏对象,那么我就可以使用新的点值和纹理重新生成它。代码效率,阶段

当游戏对象被“吃掉”刚当纹理相交屏幕上的嘴

隐藏扰流板

代码下一阶段的方法被称为:

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

    }  

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

+0

你要买什么?也许写下你想要完成的事,然后有人可以建议实施模式。 – ZeroPhase

+0

嗯,也许我只是想知道switch语句是否是真正实现这个阶段操作的最佳方式?有没有更好的方法来做这样的阶段?它对我来说有点笨拙。 – Ricehead

+2

你的代码基本上不会使用类来捆绑你的属性。你的代码没有什么特别的错误,要求更简洁的代码而不使用类是浪费时间。 –

回答

0

您可能可以组合一些这些开关。

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

将变成:

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

好吧,我认为<<运算符会将您在此例中指定的数字左移的变量位移动8?为什么我需要它和它的论点?开关(foodNumber | food1Phase) {case(1 | 1):could not I just have this? – Ricehead

+0

'1 | 3'和'3 | 3'都产生'3'。这个转变是为两个值腾出空间,假设'foodNumber'最多需要24位,'food1Phase'最多需要8位。 –