0

我正在创建游戏,其中从天上掉下 4 个不同的物体,需要通过单击鼠标按钮来摧毁它们。我的问题是所有 4 个对象同时产生。我需要让所有 4 个随机生成,但不是在同一时间,在玩家获得 200 分之后,我需要让生成的对象掉落更多。(我有计数点的工作计数器)。如果您知道如何使这些对象不会在另一个对象上产生,那么还有一件事。我是说:

坏产卵。

坏产卵

这是我的常量变量:

public static const GRAVITY:Number = 3;
public static const HIT_TOLERANCE:Number = 50;  

//Powerup
public static const APPLE_END_Y:Number = 640;
public static const APPLE_SPAWN_CHANCE:Number = 0.02; //per frame per second
public static const APPLE_START_Y:Number = 110;
public static const APPLE_SPAWN_START_X:Number = 50;
public static const APPLE_SPAWN_END_X:Number = 500;

//Scoring
public static const PLAYER_START_SCORE:Number = 0;
public static const SCORE_PER_APPLE:Number = 10;

这是我的代码的一部分:

 public function startGame()
        {
            speed = C.PLAYER_SPEED;
            gravity = C.GRAVITY;
            score = C.PLAYER_START_SCORE;
            randomChance = C.APPLE_SPAWN_CHANCE;
        tol = C.HIT_TOLERANCE;
            apples = new Array();
            mcGameStage.addEventListener(Event.ENTER_FRAME,update);

        }       

        private function update(evt:Event)
        {

            //Spawn new apples
            if (Math.random() < randomChance)
            {
                //spawn x  coordinates
                var newPirmas = new Pirmas();
                newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
                var newAntras = new Antras();
                newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
                var newTrecias = new Trecias();
                newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
                var newApple = new Apple();
                newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
                //spawn y coordinates
                newPirmas.y = C.APPLE_START_Y;
                newAntras.y = C.APPLE_START_Y;
                newTrecias.y = C.APPLE_START_Y;
                newApple.y = C.APPLE_START_Y;
                apples.push(newPirmas);
                apples.push(newAntras);
                apples.push(newTrecias);
                apples.push(newApple);
                newPirmas.addEventListener(MouseEvent.CLICK, onClick);
                newAntras.addEventListener(MouseEvent.CLICK, onClick);
                newTrecias.addEventListener(MouseEvent.CLICK, onClick);
                newApple.addEventListener(MouseEvent.CLICK, onClick);

                mcGameStage.addChildAt(newPirmas,0);
                mcGameStage.addChildAt(newAntras,0);
                mcGameStage.addChildAt(newTrecias,0);
                mcGameStage.addChildAt(newApple,0);
            }           
            //Move Apples
            for (var i = apples.length-1; i >= 0; i--)
            {
                apples[i].y += gravity;

                if (apples[i].y > C.APPLE_END_Y)
                {
                    mcGameStage.removeChild(apples[i]);

                    apples.splice(i,1);
                     m_iLives--;
                     if (!m_iLives)
        {
            trace("Game Over");

            // newApple.removeEventListener(MouseEvent.CLICK, onClick);
            break;
        }
                }
            }           
            txtScore.text = String(score);
        }
        function onClick(evt:MouseEvent):void{
        var apples = evt.target;        
        apples.visible = false;
    apples = tol;   
        score += C.SCORE_PER_APPLE; 
}
}
4

2 回答 2

0

在您的更新代码中,您正在检查一次随机值是否小于 randomChance,当它是时,您会生成 4 个苹果。

如果您希望它们在不同时间生成,您可以一次调用它们。我会创建一个函数来创建一个随机放置的苹果,并在满足您的 if 语句时调用它。

例如:

private function update(evt:Event){
    if(Math.random() < randomValue){
        createApple();
    }

createApple() 将创建一个新苹果,定位它,将其添加到数组中,然后将其添加到舞台。

如果您每次都尝试创建一个不同的苹果,您可以将不同的类型放在一个数组中,并在创建新苹果时对其进行迭代。

例如。

private function createApple(){
    ...
    var newApple = appleToSpawn[currentApple]
    ...
}

其中 appleToSpawn 是一个苹果对象数组,而 currentApple 是您当前所在的苹果的索引。

或者,您可以对随机数进行一系列比较,并根据满足的条件生成不同的苹果。

至于确保它们不重叠,您可以保留它们生成点的历史记录,并更改获取它们随机 X 值的方式。只需遍历先前 X 值的数组,并确保新值不在 (oldX + (width/2)) 内。

现在要在获得更多积分时生成超过 4 个,只需将 randomChance 更改为更大的数字即可。如果它是一个更大的数字,你会更频繁地满足你的条件语句,从而产生更多的苹果。

于 2013-05-28T16:04:59.660 回答
0
public function startGame()
{
    speed = C.PLAYER_SPEED;
    gravity = C.GRAVITY;
    score = C.PLAYER_START_SCORE;
    randomChance = C.APPLE_SPAWN_CHANCE;
    tol = C.HIT_TOLERANCE;
    apples = [];
    itemsToSpawn = [];
    mcGameStage.addEventListener(Event.ENTER_FRAME,update);

    nextSpawn:Number = 0;

}       

private function update(evt:Event)
{
    //Spawn new apples
    if (Math.random() < randomChance)
    {
        //spawn x  coordinates
        var newPirmas = new Pirmas();
        newPirmas.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newAntras = new Antras();
        newAntras.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newTrecias = new Trecias();
        newTrecias.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        var newApple = new Apple();
        newApple.x = Math.random() * C.APPLE_SPAWN_END_X + C.APPLE_SPAWN_START_X;
        //spawn y coordinates
        newPirmas.y = C.APPLE_START_Y;
        newAntras.y = C.APPLE_START_Y;
        newTrecias.y = C.APPLE_START_Y;
        newApple.y = C.APPLE_START_Y;

        newPirmas.addEventListener(MouseEvent.CLICK, onClick);
        newAntras.addEventListener(MouseEvent.CLICK, onClick);
        newTrecias.addEventListener(MouseEvent.CLICK, onClick);
        newApple.addEventListener(MouseEvent.CLICK, onClick);

        //add items to itemsToAdd
        itemsToAdd.push(newPirmas, newAntras, newTrecias, newApple);

    }           

    //Move Apples
    for (var i = apples.length-1; i >= 0; i--)
    {
        apples[i].y += gravity;

        if (apples[i].y > C.APPLE_END_Y)
        {
            mcGameStage.removeChild(apples[i]);

            apples.splice(i,1);
            m_iLives--;
            if (!m_iLives)
            {
                trace("Game Over");

                // newApple.removeEventListener(MouseEvent.CLICK, onClick);
                break;
            }
        }
    }           

    txtScore.text = String(score);


    if( itemsToSpawn.length > 0 && getTimeout() > nextSpawn  )
    {
        var item:DisplayObject = itemsToAdd.shift();
        apples.push(item);
        mcGameStage.addChild(item);
        nextSpawn = getTimeout() + Math.random() * 4000;
    }

}

function onClick(event:MouseEvent):void
{
    var apples = event.currentTarget;        
    apples.visible = false;
    apples = tol;   
    score += C.SCORE_PER_APPLE; 

    if(score % 100 == 0)
    {
        gravity += 10;
    }
}
于 2013-05-28T15:55:25.047 回答