1

我想在我制作的 Actionscript 3 游戏中限制子弹的射速。任何帮助将非常感激。下面是我的代码。

//shooting bullet
function shoot(Evt:MouseEvent)
{
    var sound2 = new bullet_shoot();
    sound2.play();
    if (bulletCounter < 5)
    {
        bulletCounter++;
    }
    else
    {
        bulletCounter = 0;
    }
    shootmc(bulletArray[bulletCounter]);
}

function shootmc(mc:MovieClip)
{
    mc.visible = true;
    mc.x = spaceman_mc.x;
    mc.y = spaceman_mc.y;
    mc.gotoAndPlay(2);
}
4

1 回答 1

1

In function shoot(), set a delay/countdown variable which prevents shooting if larger than 0. For example:

function shoot(Evt:MouseEvent) {
    if (shootDelay == 0) {

        // set shoot delay
        shootDelay = 10;

        // shoot logic
        var sound2 = new bullet_shoot();
        if (bulletCounter < 5) bulletCounter++;
        else bulletCounter = 0;
        shootmc(bulletArray[bulletCounter]);
    }
}

Now, you must still ensure that shootDelay is decreased once per frame/update if it is larger than 0, otherwise you would never be able to fire again. You can either call an update() method each frame, or subscribe to the ENTER_FRAME event and do your update in the corresponding event listener. A simple update() method would look like this:

public function update():void {
    if (shootDelay > 0) shootDelay --;
}

Good luck.

于 2012-04-22T14:00:03.850 回答