Javascript 小行星子弹运动困难

Javascript 小行星子弹运动困难,javascript,html,canvas,Javascript,Html,Canvas,因此,我正在用HTML5制作一个小行星游戏,我被子弹[I]的移动卡住了。移动功能。当我按下空格键时,程序会画出一个子弹,但它只是跟随飞船,但从未离开飞船。您可以在调用函数时直接将数字输入到该函数中,但当我为speedX和SpeedY调用对象时,子弹根本不会移动。是不是我使用的子弹的x和y位置导致了问题?我的代码: function Bullet(x, y, sx, sy) { this.x = x; this.y = y; this.sx = sx; this.sy = sy; this.r

因此,我正在用HTML5制作一个小行星游戏,我被子弹[I]的移动卡住了。移动功能。当我按下空格键时,程序会画出一个子弹,但它只是跟随飞船,但从未离开飞船。您可以在调用函数时直接将数字输入到该函数中,但当我为speedX和SpeedY调用对象时,子弹根本不会移动。是不是我使用的子弹的x和y位置导致了问题?我的代码:

function Bullet(x, y, sx, sy) {

this.x = x;
this.y = y;
this.sx = sx;
this.sy = sy;
this.r = 1;

this.show = function() {

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

};

this.move = function() {

    this.x += this.sx;
    this.y += this.sy;

};

this.wrap = function() {

    if (this.x > width+this.r) {

        this.x = -this.r;

    } else if (this.x < -this.r) {

        this.x = width+this.r;

    }

    if (this.y > height+this.r) {

        this.y = -this.r;

    } else if (this.y < -this.r) {

        this.y = height+this.r;

    }

};

}

for (var i = 0; i < bullets.length; i++) {

    var b = bullets[i];

    bullets[i] = new Bullet(b.x, b.y, b.speedX*10, b.speedY*10);

    bullets[i].move();
    bullets[i].wrap();
    bullets[i].show();

}

if (spacePressed) {

    bullets.push({x: player.pos.x, y: player.pos.y, speedX: Math.sin(player.heading), speedY: -Math.cos(player.heading)});

};
这是我的密码:

var canvas=document.getElementByIdcanvas; var ctx=canvas.getContext2d; var width=canvas.width; var height=canvas.height; var leftPressed=false; var-rightspressed=false; var upPressed=假; var spacePressed=false; document.addEventListenerkeydown,d; document.addEventListenerkeyup,u; 函数{ 如果e.keyCode==37{ leftPressed=true; }如果e.keyCode==39{ rightspressed=true; } 如果e.keyCode==38{ upPressed=true; } 如果e.keyCode==32{ spacePressed=true; } } 函数ue{ 如果e.keyCode==37{ leftPressed=false; }如果e.keyCode==39{ 右压=假; } 如果e.keyCode==38{ upPressed=false; } 如果e.keyCode==32{ spacePressed=false; } } 函数向量{ 这个.x=x | | 0; 这个.y=y | | 0; } 功能Bulletx、y、sx、sy{ 这个.x=x; 这个。y=y; this.sx=sx; this.sy=sy; 这是r=1; this.show=函数{ ctx.fillStyle=白色; ctx.beginPath; ctx.arcthis.x,this.y,this.r,0,Math.PI*2; ctx.fill; ctx.closePath; }; this.move=函数{ this.x+=this.sx; this.y+=this.sy; }; this.wrap=函数{ 如果此.x>宽度+此.r{ this.x=-this.r; }否则,如果此.x<-此.r{ 这个.x=宽度+这个.r; } 如果此.y>高度+此.r{ this.y=-this.r; }否则,如果此.y<-此.r{ 这个.y=高度+这个.r; } }; } 函数播放器{ this.pos=新矢量宽度/2,高度/2; 这个r=15; 这个方向=0; 此.facingX=0; 这个面Y=0; this.show=函数{ ctx.strokeStyle=白色; ctx.save; ctx.translatethis.pos.x,this.pos.y; ctx.rotatethis.heading; ctx.beginPath; 移动到这个.r,这个.r; ctx.lineTothis.r,this.r; ctx.lineTo0,-this.r; ctx.closePath; ctx.restore; ctx.stroke; }; this.move=函数{ this.pos.x+=this.facingX; this.pos.y+=this.facingY; 该系数=0.95; 该系数y*=0.95; }; this.applyForce=函数{ var force=新矢量路径到该航向,-Math.costhis.heading; 力x*=0.5; 力y*=0.5; 此面x+=力x; this.facingY+=力.y; }; this.rot=functionangle{ 这个。航向+=角度; }; this.wrap=函数{ 如果此.pos.x>宽度+此.r{ this.pos.x=-this.r; }否则,如果此.pos.x<-此.r{ 此.pos.x=宽度+此.r; } 如果此.pos.y>高度+此.r{ this.pos.y=-this.r; }否则,如果此位置y<-此位置r{ 此.pos.y=高度+此.r; } }; } var玩家=新玩家; var=[]; 函数图{ ctx.fillStyle=黑色; ctx.fillRect0,0,宽度,高度; 对于变量i=0;i重新初始化每一帧中的项目符号对象是问题出现的地方

for (var i = 0; i < bullets.length; i++) {
    bullets[i].move();
    bullets[i].wrap();
    bullets[i].show();
}

if (spacePressed) {
  bullets.push(
    new Bullet(
      player.pos.x, 
      player.pos.y, 
      Math.sin(player.heading) * 10, 
      -Math.cos(player.heading) * 10
    )
  );

  spacePressed = false;
};
这允许bullet对象进行相应的更新,您也可以使用instanceOf检查bullet的类型,但从长远来看,这样做的可读性较差

添加spacePressed=false是为了防止在一次按下空格键时出现多帧子弹


我有一个可以让你看到它在起作用。

你试过把船移向子弹的另一个方向吗?
如果没有,请尝试,如果bullet保持在相同的路径上,我建议更新bullet的速度要快于船速

在draw函数中,我在if spacePressed条件中放置了一个console.log'Here'指令…它只需按一次空格键就可以在此处打印7倍的消息…这是预期的结果吗?嗯,现在,我只是想让子弹移动,我现在不担心子弹的数量,我会修正那个lat 呃。