【问题标题】:Navigating through an array of game objects without showing duplicate objects在游戏对象数组中导航而不显示重复对象
【发布时间】:2014-03-10 03:11:58
【问题描述】:

我正在开发一款游戏,在我的游戏中,用户在整个关卡中都会获得武器(纸杯蛋糕)。我有一个按钮,玩家点击它可以切换到下一个纸杯蛋糕(获得的武器存储在一个数组中)。

这是我的问题:如果玩家获得 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();
}

【问题讨论】:

  • 感谢 @Klaster_1 为我清理这些内容。

标签: actionscript-3 flash-builder flashdevelop


【解决方案1】:

您应该将可用的types武器存储在您选择武器类型的数组中。此外,该阵列应包含ints,因为显然您的纸杯蛋糕用于火/行动,因此您将拥有弹药存储阵列供您使用。如果您需要解锁阵列,请务必制作另一个阵列。 (但请确保有默认武器,否则蛋糕选择功能会进入无限循环)

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

【讨论】:

  • 天哪,你太棒了!!!!你让这个看起来很简单。非常感谢您的帮助。这是我的第一个移动应用程序,我对编程还比较陌生,所以我还在学习一些东西。再次感谢您的帮助。如果我以后有任何问题,可以在这里给你发消息吗?
  • 如果您有更多问题,请随时从 StackOverflow 提问,这里有比我更好的人。如果出现问题,请在此处提问,您可能会收到详细的答复。
  • 我遇到了另一个动作脚本问题,想知道您是否可以提供一些见解。我发布了这个问题,但没有收到任何有用的见解。我的按钮降低了游戏的帧速率。我已将显示对象的鼠标属性设置为 false,根本没有帮助。当我不单击按钮时,它运行得非常流畅。帧速率的下降导致可怕的延迟
猜你喜欢
  • 2020-10-06
  • 1970-01-01
  • 2022-07-27
  • 2016-11-04
  • 1970-01-01
  • 1970-01-01
  • 2015-04-06
  • 2018-08-18
  • 2017-10-24
相关资源
最近更新 更多