Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在设定的时间内从每个敌人身上射出3颗子弹_Javascript_Html_Canvas_Html5 Canvas_Kineticjs - Fatal编程技术网

Javascript 在设定的时间内从每个敌人身上射出3颗子弹

Javascript 在设定的时间内从每个敌人身上射出3颗子弹,javascript,html,canvas,html5-canvas,kineticjs,Javascript,Html,Canvas,Html5 Canvas,Kineticjs,我试图在画布上创建一个简单的游戏,使用kineticJS(只是一些练习),并设法让我的玩家射击子弹。这同样适用于繁殖的敌人。每次最后一颗子弹离开舞台时,他们都会射出一颗子弹 但是:我希望所有的敌人(可变数量)以2秒的间隔射出3颗子弹。但我完全陷入困境,想不出一个办法来完成它 谁能看看我的小提琴,看看有什么事吗? 注:第573行是循环的函数(并每隔30FPS绘制项目符号等) 下面是我创建新bullet对象的代码:(fiddle中的第406行) 以及提供新项目符号的函数:(小提琴中的第247行)

我试图在画布上创建一个简单的游戏,使用kineticJS(只是一些练习),并设法让我的玩家射击子弹。这同样适用于繁殖的敌人。每次最后一颗子弹离开舞台时,他们都会射出一颗子弹

但是:我希望所有的敌人(可变数量)以2秒的间隔射出3颗子弹。但我完全陷入困境,想不出一个办法来完成它

谁能看看我的小提琴,看看有什么事吗?

注:第573行是循环的函数(并每隔30FPS绘制项目符号等)

下面是我创建新bullet对象的代码:(fiddle中的第406行)

以及提供新项目符号的函数:(小提琴中的第247行)


这个问题最难的部分可能是计算出什么时候每2秒发射一颗子弹,让三颗子弹同时发射。要使子弹均匀发射,需要将间隔中的帧数除以该间隔中要发射的子弹数

因为你以每秒30帧的速度运行游戏,2秒等于60帧

60帧/3个项目符号=20帧/bullet

因此,我们将每20帧为每个敌人创建一个新子弹,或者每20次调用
refreshLoop()
,在
refreshLoop()
中,您现在必须循环遍历每个敌人在其
子弹
数组中的所有子弹,因为现在可能不止一个

项目符号
数组中可以有多个项目符号这一事实给从数组中删除项目符号的方式带来了一个新问题。以前,您依赖于这样一个事实:一次一个项目符号意味着它将始终是数组中的第一个项目符号,因此您的代码称为
bullets.splice(0,1)。然而,当玩家四处移动,敌人在不同的位置开火时,子弹完全有可能离开屏幕,并比之前发射的子弹更快地被移除。这将导致删除正确的项目符号精灵,但数组中的第一个项目符号将从
项目符号中删除,因此它将不再在
刷新循环()
中更新,而它将只是坐在屏幕上无所事事

为了避免这种情况,有必要将被抽取的子弹所在的索引传递给敌人子弹的
draw()
函数。由于无论如何都需要循环数组,索引已经在
refreshLoop()
中,所以只需将其传递给
draw()
。现在,每次需要删除项目符号时,您都可以调用
bullets.splice(bulletIndex,1)

我希望你不介意;我请求您使用下面列出的更改对其进行更新

编辑:用于突发火力而非持续火力的新属性

// Inside your Enemybullet definition
// One simple change to draw(), pass in the index of the bullet in the array
this.draw = function(indexEnemy, indexBullet) {

    var mayDelete = false;

    ...

    if (bulletLeftField(this.sprite) == true) {
        mayDelete = true;
    }

    if (mayDelete == true) {
        this.sprite.remove();

        // Since you now have multiple bullets, you'll have to make
        // sure you're removing the correct one from the array
        enemies[indexEnemy].bullets.splice(indexBullet, 1);
    }

    ammoLayer.draw();
}

...

// Inside your refreshLoop function
// If there are enemies they should be checked
if (enemies.length > 0) {
    for (var i = 0; i < enemies.length; i++) {
        enemies[i].draw();

        // At 30 frames per second, 3 bullets in 2 seconds would be
        // one bullet for every 20 frames. So, every 20 frames,
        // create a new bullet for each enemy
        if ((enemyShootTimer % 20) == 0) {
            createEnemyBullet(enemies[i]);
        }

        // The same way you draw all of the player's bullets,
        // loop through the array of bullets for this enemy,
        // and draw each one, passing in the new parameters
        if (enemies[i].bullets.length > 0) {
            for (var j = 0; j < enemies[i].bullets.length; j++) {
                enemies[i].bullets[j].draw(i, j);
            }
        }
    }
}

// Update loop for burst-fire instead of sustained fire
var burstTime = 10; // 10 frames between bullets, 3 per second
var needToShoot = ((enemyShootTimer % burstTime) == 0);
if (enemies.length > 0) {
    for (var i = 0; i < enemies.length; i++) {
        enemies[i].draw();

        // if the enemies still have bullets to shoot this burst
        // and if 10 frames have passed since the last shot
        // ( enemyBurstCounter is declared outside refreshLoop() )
        if (enemyBurstCounter < 3 && needToShoot) {
            createEnemyBullet(enemies[i]);
        }
        if (enemies[i].bullets.length > 0) {
            for (var j = 0; j < enemies[i].bullets.length; j++) {
                enemies[i].bullets[j].draw(i, j);
            }
        }
    }
    if ((enemyShootTimer % 60) == 0) {
        enemyBurstCounter = 0; // if 2 seconds have passed, reset burst counter
    } else if (needToShoot) {
        enemyBurstCounter++; // if the enemies shot, update burst counter
    }
}
//在您的Enemybullet定义中
//对draw()进行一个简单的更改,传入数组中项目符号的索引
this.draw=函数(indexEnemy,indexBullet){
var-mayDelete=false;
...
if(bulletLeftField(this.sprite)==true){
mayDelete=true;
}
if(mayDelete==true){
this.sprite.remove();
//既然你现在有多发子弹,你就必须
//确定要从阵列中删除正确的
敌人[IndexEnmy]。子弹。拼接(indexBullet,1);
}
amomlayer.draw();
}
...
//在refreshLoop函数中
//如果有敌人,就应该加以制止
如果(长度>0){
对于(变量i=0;i<0.length;i++){
敌人[i].draw();
//以每秒30帧的速度,2秒内3发子弹将被删除
//每20帧就有一颗子弹所以每20帧,
//为每个敌人创建一个新子弹
如果((EneyShootTimer%20)==0){
createnemybullet(敌人[i]);
}
//就像你画玩家所有的子弹一样,
//在敌人的子弹阵中循环,
//并绘制每一个,传入新参数
如果(敌人[i].bullets.length>0){
对于(var j=0;j0){
对于(变量i=0;i<0.length;i++){
敌人[i].draw();
//如果敌人还有子弹来射这一枪
//如果从上次拍摄到现在已经过了10帧
//(enemyBurstCounter在refreshLoop()外部声明)
如果(enemyBurstCounter<3&&NeedToShot){
createnemybullet(敌人[i]);
}
如果(敌人[i].bullets.length>0){
对于(var j=0;j
这个问题最难的部分可能是弄清楚什么时候每2秒发射三颗子弹。要使子弹均匀发射,需要将间隔中的帧数除以该间隔中要发射的子弹数

因为你以每秒30帧的速度运行游戏,2秒等于60帧

60帧/3个项目符号=20帧/bullet

因此,我们将每20帧为每个敌人创建一个新子弹,或者每调用
refreshLoop()
20次,在
refreshLoop()
中,您现在必须循环遍历每个敌人在其内部的所有子弹
function createEnemyBullet(enemy) {
    var blt = new Enemybullet(player.sprite.getX(), player.sprite.getY(), enemy.sprite);
    ammoLayer.add(blt.sprite);
    enemy.bullets.push(blt);
}
// Inside your Enemybullet definition
// One simple change to draw(), pass in the index of the bullet in the array
this.draw = function(indexEnemy, indexBullet) {

    var mayDelete = false;

    ...

    if (bulletLeftField(this.sprite) == true) {
        mayDelete = true;
    }

    if (mayDelete == true) {
        this.sprite.remove();

        // Since you now have multiple bullets, you'll have to make
        // sure you're removing the correct one from the array
        enemies[indexEnemy].bullets.splice(indexBullet, 1);
    }

    ammoLayer.draw();
}

...

// Inside your refreshLoop function
// If there are enemies they should be checked
if (enemies.length > 0) {
    for (var i = 0; i < enemies.length; i++) {
        enemies[i].draw();

        // At 30 frames per second, 3 bullets in 2 seconds would be
        // one bullet for every 20 frames. So, every 20 frames,
        // create a new bullet for each enemy
        if ((enemyShootTimer % 20) == 0) {
            createEnemyBullet(enemies[i]);
        }

        // The same way you draw all of the player's bullets,
        // loop through the array of bullets for this enemy,
        // and draw each one, passing in the new parameters
        if (enemies[i].bullets.length > 0) {
            for (var j = 0; j < enemies[i].bullets.length; j++) {
                enemies[i].bullets[j].draw(i, j);
            }
        }
    }
}

// Update loop for burst-fire instead of sustained fire
var burstTime = 10; // 10 frames between bullets, 3 per second
var needToShoot = ((enemyShootTimer % burstTime) == 0);
if (enemies.length > 0) {
    for (var i = 0; i < enemies.length; i++) {
        enemies[i].draw();

        // if the enemies still have bullets to shoot this burst
        // and if 10 frames have passed since the last shot
        // ( enemyBurstCounter is declared outside refreshLoop() )
        if (enemyBurstCounter < 3 && needToShoot) {
            createEnemyBullet(enemies[i]);
        }
        if (enemies[i].bullets.length > 0) {
            for (var j = 0; j < enemies[i].bullets.length; j++) {
                enemies[i].bullets[j].draw(i, j);
            }
        }
    }
    if ((enemyShootTimer % 60) == 0) {
        enemyBurstCounter = 0; // if 2 seconds have passed, reset burst counter
    } else if (needToShoot) {
        enemyBurstCounter++; // if the enemies shot, update burst counter
    }
}