2014-02-12 38 views
0

我工作的一個遊戲,在我的遊戲用戶獲得武器(紙杯蛋糕)在整個水平。我有一個按鈕,玩家點擊更改爲下一個蛋糕(獲得的武器存儲在一個陣列中)。通過遊戲對象的數組,而不顯示重複的導航對象

這裏是我的問題:如果玩家獲得2個黃蛋糕,1個紅色蛋糕,和2層藍色的蛋糕,我怎麼通過數組導航不顯示兩次相同的蛋糕? 我的按鈕看起來好像它不改變的武器,當代碼是,它要在陣列中的下一個元素,但它是相同的武器[黃色蛋糕,黃色蛋糕,紅色蛋糕,藍色蛋糕,藍色的蛋糕。

public function CupcakeChangeButton(e: MouseEvent) { 
    cakeButtonCounter++; 

    //Make sure the cakeButtonCounter never exceeds the amount of 
    //elements in array 

    if (cakeButtonCounter >= CupcakesArray.length - 1) { 
     cakeButtonCounter = 0; 
    } 

    //NOTE: may need function called "cupcake count", this function should 
    //be called at the beginning of THIS function and in the constructor function 
    //Should count the amount of cupcakes and maybe set wasSeen Elements to No 

    /* 
      The switch statment makes its decisions based on the type of cupcake 
      is the current elment. The current element is represented by 
      "cakeButtonCounter" (CupcakesArray[cakeButtonCounter]) 

      The if statements decides if the cupcake has been seen already. 
      If it hasnt been seen, it sets the button's text box to show how many of 
      those kind of cupcakes are left. 

      After the amount of cupcakes of that type is shown in the text box, 
      the "unshift" method is used to place "yes" in the first element, of it's 
      own Array type, WITHIN THE WAS SEEN ARRAY.......confusing!!!!!! 

      Example: 
      wasSeen.PinkCupcake.unshift("yes") = wasSeen.PinkCupcake[0] == "yes" 

      This is done so that we can keep track of what cupcakes has been seen 
      already 

      When the game is initialized the was seen elements are set to "no". So when 
      it's set to "yes", after being seen, The initial value,"no", is placed in the 
      next element. It's no longer needed, so we use the "splice" method to delete it 
      and HOPEFULLY, remove it from memory 

      The "else" block of code takes place if the cupcake has already been seen. 
      It increments the cakeButtonCounter so that the button will display the next 
      cupcake in the array, that has NOT been seen 

      After all decisions are made,(which cupcakes are next and which cupakes have 
      been seen) 
      Update the button's face by displaying the next cupcake that hasnt been seen 
      (button goes to and stop and next element) 

      NOTE: The ACTUAL case will be the dynamic var cupcake type (the NAME not the actual 
        cupcake) 
      */ 

    switch (CupcakesArray[cakeButtonCounter]) { 
     case "PinkCupcake": 
      if (wasSeen.PinkCupcake[0] == "no") { 
       CupcakeNavigationBox.countBox.text = "x" + PinkCupcakeCount; 
       wasSeen.PinkCupcake[0] == "yes"; 
       trace("element change? " + wasSeen.PinkCupcake[0]); 
      } else { 
       cakeButtonCounter++; 
       trace("if yes...its starting that way " + wasSeen.PinkCupcake[0]); 
      } 
      break; 

     case "YellowCupcake": 
      if (wasSeen.YellowCupcake[0] == "no") { 
       CupcakeNavigationBox.countBox.text = "x" + YellowCupcakeCount; 
       wasSeen.YellowCupcake[0] == "yes"; 
      } else { 
       cakeButtonCounter++; 
      } 
      break; 

     case "GreenCupcake": 
      if (wasSeen.GreenCupcake[0] == "no") { 
       CupcakeNavigationBox.countBox.text = "x" + GreenCupcakeCount; 
       wasSeen.GreenCupcake[0] == "yes"; 
      } else { 
       cakeButtonCounter++; 
      } 
      break; 

     case "PurpleCupcake": 
      if (wasSeen.PurpleCupcake[0] == "no") { 
       CupcakeNavigationBox.countBox.text = "x" + PurpleCupcakeCount; 
       wasSeen.PurpleCupcake[0] == "yes"; 
      } else { 
       cakeButtonCounter++; 
      } 
      break; 
    } 

    CupcakeNavigationBox.buttonFace.gotoAndStop(CupcakesArray[cakeButtonCounter]); 
    changeCupcake(); 
} 
+0

謝謝@ Klaster_1爲我清理。 – rtpenick

回答

0

而應該存儲提供類型從那裏你選擇的武器類型的數組中的武器。此外,該數組應包含int S,因爲很明顯你的蛋糕是用在火/行動,因此你將在您的處置彈藥存儲陣列。如果你需要解鎖數組,通過一切手段製作另一個數組。 (確保有一個默認的武器,或者蛋糕選擇功能可以進入無限循環)

var cupcakesAmmo:Array=[0,0,0,0,0,0,0]; // as many as you have types of cupcakes 
var cupcakesTypes:Array=[RedCupcake,YellowCupcake,PinkCupcake,GreenCupcake,BlueCupcake,PurpleCupcake,BlackCupcake]; 
// let's say we have seven cupcakes 
var currentCupcake:int; // the selected weapon 
function cupcakeChangeButton(e: MouseEvent) { 
    // first check if we actually have anything to change to 
    var weHave:int=0; // how many different cupcakes we have 
    var i:int; 
    for (i=0;i<cupcakesAmmo.length;i++) if (cupcakesAmmo[i]>0) weHave++; 
    if (weHave<2) return; // hehe, we either have no cupcakes or just one type of em 
    // otherwise let's change 
    do { // we have to do this at least once 
     currentCupcake++; 
     if (currentCupcake==cupcakesAmmo.length) currentCupcake=0; // change type 
    } while (cupcakesAmmo[currentCupcake]==0); // change until we find the type with nonzero ammo 
    // okay, type selected, let's get proper display 
    CupcakeNavigationBox.countBox.text = "x" + cupcakesAmmo[currentCupcake]; 
    // see, no switch is needed! Just get the data off your array :) 
    // TODO change the displayed cupcake too (I don't see where you do this) 
} 
+0

OMG你真了不起!你讓這個看起來很容易和簡單。非常感謝您的幫助。這是我第一個移動應用程序,我對編程還比較陌生,所以我仍然在學習一些東西。再次感謝您的幫助。如果我有任何未來的問題,可以,如果我在這裏給你留言? – rtpenick

+0

如果你有更多的問題可以從StackOverflow詢問他們,那麼在這裏有比我更好的人。如果出現問題,請在此處詢問,您可能會收到詳細的答案。 – Vesper

+0

我遇到了另一個動作問題,並想知道如果你能提供一些見解。我發佈了這個問題,並沒有收到任何有用的見解。我的按鈕正在放慢我的遊戲幀頻。我已經將鼠標屬性設置爲false來顯示對象,根本沒有任何幫助。當我不點擊按鈕時,它會非常流暢地運行。幀率下降導致可怕的滯後 – rtpenick