簡體   English   中英

如何創建子彈動畫? (射擊游戲)

[英]How to create a bullet animation? (Shooter Game)

我正在嘗試制作射擊游戲,但子彈動畫遇到了一些麻煩。 每次單擊時,都會創建一個新的項目符號對象並開始動畫,但是每次單擊后,創建的項目符號都會消失,並且相同的項目符號會重新開始,這比以前的項目符號要快。 因此,我試圖在每次單擊后創建新的項目符號。 基本的射擊游戲邏輯。 這是我的代碼:

    function newBullet(x,y,angle,speed,id,type) {
    this.x = x;
    this.y = y;
    this.speed = speed;
    this.angle = angle;
    this.radians = this.angle * Math.PI / 180;
    this.id = id;
    this.type = type;
    this.drawBullet = drawBullet;
    this.moveBullet = moveBullet;
    }

    function moveBullet() {
    this.x = this.x + Math.cos(this.radians) * this.speed ;
    this.y = this.y + Math.sin(this.radians) * this.speed;
    ctx.drawImage( bulletImg, this.x, this.y);
    }

    function drawBullet() {
        bullet = new newBullet(playerX,playerY,getAngle(),2,1,1);
        bullets[bullets.length] = bullet;
        setInterval("bullets[bullets.length - 1].moveBullet()", 25);
    }

canvas.addEventListener("mousedown",drawBullet,false);  

嘗試在此處添加var

    var bullet = new newBullet(playerX,playerY,getAngle(),2,1,1);

如果要向陣列添加項目符號,則應使用push 這將更新bullets.length

function drawBullet() {
        var bullet = new newBullet(playerX,playerY,getAngle(),2,1,1);
        bullets.push(bullet);
        setInterval(bullets[bullets.length - 1].moveBullet, 25);
}

您創建的項目符號消失的原因:您每次都替換了bullets[0] 新的項目符號比舊的項目符號要快,因為bullets[0].moveBullet在每個25ms的間隔中被稱為n次,其中n是您“創建”的項目符號數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM