我想在检测到旧对象后再推送一个新对象

时间:2019-05-14 04:50:42

标签: javascript arrays push collision-detection

我正在创建一个简单的2d游戏,该硬币只应在通过碰撞检测收集到前一个硬币后才重生。

function spawnRandomCoins()
{
    var coin = 
    {
        x: Math.random()*(canvas.width-30)+15,
        y: canvas.height-30,
        r: 10,
        status: 1
    };
    coins.push(coin);
}

function animateRandomCoins()
{

    for(var i = 0; i < coins.length; i++)
    {
        var coin = coins[i];
        if (coin.status == 1)
        {

            ctx.beginPath();
            ctx.fillStyle = "yellow";
            ctx.arc(coin.x, coin.y, coin.r, 0, 2*Math.PI);
            ctx.closePath();
            ctx.fill();
        }

        var coinRight = coin.x + coin.r;
        var coinLeft = coin.x - coin.r;
        var coinTop = coin.y - coin.r;
        var coinBottom = coin.y + coin.r;
        var bobRight = bobX + bobWidth;
        var bobLeft = bobX ;
        var bobTop = bobY;
        var bobBottom = bobY + bobHeight;

        if (coinRight > bobLeft && coinLeft < bobRight && coinBottom > bobTop && coinTop < bobBottom && coin.status == 1)
        {
            coin.status = 0;
            spawnRandomCoins();
        }

    }
}

我希望第一个硬币出现,然后被碰撞检测到,然后第二个硬币出现。但是什么也没发生。请帮忙!

0 个答案:

没有答案