【问题标题】:How to spawn objects in array without repeating?如何在不重复的情况下在数组中生成对象?
【发布时间】:2019-08-01 20:49:11
【问题描述】:

我有 MC 的 rockThrowers 被添加到舞台的 3 个不同位置。它们使用运行良好的随机生成器随机生成。用户单击舞台上的一个按钮,rockThrowers 被添加到舞台并推入他们自己的数组 aRockThrowerArray 我希望能够检查它们产生的 3 个位置中的哪一个,而不是与下一个 rockThrowers 重叠被添加到舞台,如果是这样,那么在空位置添加一个新的。我尝试了不同的策略,主要是布尔值,并将它们从他们自己的类调用到我的主类,但似乎没有任何效果。这是我的

rockThrowers 类:

    private function startPosition():void 
    {
        // y position 
        this.y =  (stage.stageHeight / 2) + 200;

        //Start Speed
        nSpeed = randomNumber(5, 8);

        leftScreenSpawn = randomNumber(1, 3);


        //For Left Screen
        leftNeg =  (stage.stageWidth / 2) - 200;
        leftMiddle =  (stage.stageWidth / 2) - 150;
        leftPos =  (stage.stageWidth / 2) - 100;



        //Left Screen
        if (leftScreenSpawn == 1)
        {
            this.x = leftNeg;
            bLeftNeg = true; // Now if the left Rock thrower is destroyed then turn back to false on main engine class
        }else
        if (leftScreenSpawn == 2)
        {
            this.x = leftMiddle;
            bLeftMiddle = true;
        }else
        if (leftScreenSpawn == 3)
        {
            this.x = leftPos;
            bLeftPos = true;
        }



        //Move 
        startMoving();
    }

现在在我的主类中,当用户单击左屏幕 Btn 时,我已经设置了这样的设置:

    private function rockThrowerSpawn(e:MouseEvent):void 
    {
        //Instantiate screens before hand
        rockThrowerSpawnScreen.x = (stage.stageWidth / 2);
        rockThrowerSpawnScreen.y = (stage.stageHeight / 2) + 200;
        addChild(rockThrowerSpawnScreen);

        rockThrowerSpawnScreen.left.addEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);

    }

然后是 Spawn 函数:

private function chooseSpawnSideRockThrowers(e:MouseEvent):void 
    {
        if (e.currentTarget == rockThrowerSpawnScreen.left) // Spawn LEFT
        {

            //add new rock thrower
             rockThrowers = new mcRockThrowers();
             //Add object
             addChild(rockThrowers);
             //Add to Array
             aRockThrowerArray.push(rockThrowers);
             //trace("LEFT SPAWN");

        }

        //Subtract resources and update text
        nResources -= 10;
        updateResourceTextField();


        //Remove Listeners
        rockThrowerSpawnScreen.left.removeEventListener(MouseEvent.CLICK, chooseSpawnSideRockThrowers);
        rockThrowerSpawnScreen.destroy();
    }

我知道仅此一项总是会产生随机位置我删除了所有不起作用的东西现在我回到了这个正方形。关于我如何做到这一点的任何想法?感谢所有支持。

【问题讨论】:

    标签: actionscript-3 adobe flashdevelop


    【解决方案1】:

    简单。您需要一个有限的数组,它以随机顺序生成 3 个值。

    var L:Array =
    [
        (stage.stageWidth / 2) - 200,
        (stage.stageWidth / 2) - 150,
        (stage.stageWidth / 2) - 100,
    ];
    
    function fetchPosition():Number
    {
        // Get a random index based on the current length of L.
        var anIndex:int = Math.random() * L.length;
    
        // Record the result.
        var result:Number = L[anIndex];
    
        // Remove the result from the list.
        L.splice(anIndex, 1);
    
        return result;
    }
    

    因此,您可以在每次应用程序运行时有效地调用 fetchPosition() 3 次,并且每次运行 L 的内容都将按随机顺序获取,更重要的是,您不会两次获得相同的值,因为获取的值已从数据集中删除。

    【讨论】:

    • 嘿,Organis 感谢您的帮助,它运行良好。我在 Main 类中使用该函数,并从我的 rockThrower 类中删除了所有 x Position Spawns。可以的话问一个问题。假设现在删除了其中一个 rockThrower 使用的位置,并且删除了持有该位置的 rockThrower 我如何将最后一个位置重新添加回 L 数组?我会做类似 if (L.indexOf((stage.stageWidth / 2) - 100) == -1) { L.push((stage.stageWidth / 2) - 100); }
    • @NathanFitchett 你想多了(而且过于复杂)了。当您移除抛石器时,只需将其 x 坐标推回列表即可。如果它的位置不是固定的,那么,将一些存储变量 originalX 附加到它并使用它来代替。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-12
    • 2022-11-22
    • 1970-01-01
    • 2022-01-21
    • 2016-04-03
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多