请问老程序员如何用javascript如何达成火箭喷出子弹的效果

这是我学校拓展题遇到的难题,最后两个要求实在是完成不了,请问有没有老程序员能帮帮我

问题遇到的现象和发生背景

W​hen you have completed the rocket example from the 'This' lecture, attempt the following extensions.

C​hange the look of the rocket to your own design.

​Alter the rocket to move more quickly or slowly.

Change the rocket object to use vectors instead of the x, y and thrust values.

A​dd sounds to the rocket - watch the game project sound video later in the topic first.

Add a fire method to the rocket object which will shoot a bullet out of the top of the rocket (tricky).

完成“This”课程中的火箭示例后,尝试以下扩展。
将火箭的外观更改为您自己的设计。
​改变火箭以更快或更慢的速度移动。
将火箭对象更改为使用矢量,而不是x、y和推力值。
为火箭添加声音。
_向火箭物体添加一种射击方法,这种方法会从火箭顶部射出一颗子弹(很棘手)_。
我就是最后一个让火箭顶部射出一个子弹的方法不会做,子弹偶尔会卡在火箭上面老是发不出去,我也不知道为什么

问题相关代码,请勿粘贴截图
var rocket; 
var baseLine;
var bulletVector;


function setup()
{
    createCanvas(800, 600);
    
    baseLine = height - 100;
    
    bulletVector = createVector(0,0);

     rocket =    {
        x: width/2,
        y: baseLine,
        v: createVector(width/2,baseLine),
        thrust: false, 
        moveLeft: false,
        moveRight: false,
        fire: false,
        fired: false,
        drawRocket: function(){
            fill(180)
            beginShape();
            vertex(this.v.x + 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y);
            vertex(this.v.x + 15, this.v.y - 20);
            vertex(this.v.x + 20, this.v.y);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);

            fill(255, 0, 0);
            beginShape();
            vertex(this.v.x - 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y + 30);
            vertex(this.v.x + 10, this.v.y + 60);
            endShape(CLOSE);

            beginShape();
            vertex(this.v.x + 40, this.v.y + 60);
            vertex(this.v.x + 20, this.v.y + 30);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);

            if (this.thrust)
            {
                fill(255, 150, 0);
                beginShape();
                vertex(this.v.x + 10, this.v.y + 60);
                vertex(this.v.x + 13, this.v.y + 80);
                vertex(this.v.x + 15, this.v.y + 70);
                vertex(this.v.x + 18, this.v.y + 80);
                vertex(this.v.x + 20, this.v.y + 60);
                endShape(CLOSE);
            }
            
            
        },
        moveRocket: function(){
            if (rocket.thrust && rocket.v.y > 0)
            {
                this.v.y -= 3;
            }
            else if (rocket.v.y < baseLine)
            {
                this.v.y += 3;
            }

            if (rocket.moveLeft && rocket.v.x > 0 && rocket.v.y != baseLine)
            {
                this.v.x -= 2;
            }

            if (rocket.moveRight && rocket.v.x < width && rocket.v.y != baseLine)
            {
                this.v.x += 2;
            }
            
        },

    };
    
}

function draw()
{
    //move the rocket
    rocket.moveRocket();
    background(10);
    
    //draw the rocket
    rocket.drawRocket();
    if(rocket.fire && rocket.fired == false){
        rocket.fired = true;
        
    }
    
    if(rocket.fired == true ){
        bulletVector.y -= 5;
    }
    ellipse(bulletVector.x, bulletVector.y,10);
 

    
}
    


function keyPressed()
{
    if (key == "W")
    {
        rocket.thrust = true;
    }

    if (key == "A")
    {
        rocket.moveLeft = true;
    }

    if (key == "D")
    {
        rocket.moveRight = true;
    }
    
    if (key == "Q"){
        rocket.fired = true;
    }
    
    
    
}


    

    

function keyReleased(){
    if(key == "W")
    {
       rocket.thrust = false;
    }
    
    if(key == "A")
    {
       rocket.moveLeft = false;
    }
    
    if(key == "D")
    {
       rocket.moveRight = false;
    }
    
    if(key == "Q"){
        rocket.fired = false;
    }
}

运行结果及报错内容

我总是只有一个可以移动的火箭,但是我子弹老是发射不出去,有时只能稍微挪动一下,请老程序员帮帮忙看一下

我的解答思路和尝试过的方法

img

我想要达到的结果

我想要有子弹能够射出去然后火箭也可以随便移动的效果

var rocket; 
var baseLine;
var bulletVector;
 
 
function setup()
{
    createCanvas(800, 600);
    
    baseLine = height - 100;
    
    bulletVector = createVector(0,0);
 
     rocket =    {
        x: width/2,
        y: baseLine,
        v: createVector(width/2,baseLine),
        thrust: false, 
        moveLeft: false,
        moveRight: false,
        fire: false,
        fired: false,
        firesta: false,
        drawRocket: function(){
            fill(180)
            beginShape();
            vertex(this.v.x + 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y);
            vertex(this.v.x + 15, this.v.y - 20);
            vertex(this.v.x + 20, this.v.y);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);
 
            fill(255, 0, 0);
            beginShape();
            vertex(this.v.x - 10, this.v.y + 60);
            vertex(this.v.x + 10, this.v.y + 30);
            vertex(this.v.x + 10, this.v.y + 60);
            endShape(CLOSE);
 
            beginShape();
            vertex(this.v.x + 40, this.v.y + 60);
            vertex(this.v.x + 20, this.v.y + 30);
            vertex(this.v.x + 20, this.v.y + 60);
            endShape(CLOSE);
 
            if (this.thrust)
            {
                fill(255, 150, 0);
                beginShape();
                vertex(this.v.x + 10, this.v.y + 60);
                vertex(this.v.x + 13, this.v.y + 80);
                vertex(this.v.x + 15, this.v.y + 70);
                vertex(this.v.x + 18, this.v.y + 80);
                vertex(this.v.x + 20, this.v.y + 60);
                endShape(CLOSE);
            }
            
            
        },
        moveRocket: function(){
            if (rocket.thrust && rocket.v.y > 0)
            {
                this.v.y -= 3;
            }
            else if (rocket.v.y < baseLine)
            {
                this.v.y += 3;
            }
 
            if (rocket.moveLeft && rocket.v.x > 0 && rocket.v.y != baseLine)
            {
                this.v.x -= 2;
            }
 
            if (rocket.moveRight && rocket.v.x < width && rocket.v.y != baseLine)
            {
                this.v.x += 2;
            }
            
        },
 
    };
    
}
 
function draw()
{
    //move the rocket
    rocket.moveRocket();
    background(10);
    
    //draw the rocket
    rocket.drawRocket();
    
    if(rocket.firesta == true){
        bulletVector.y = rocket.v.y;
        bulletVector.x = rocket.v.x;
        rocket.firesta = false;
    }
    if (bulletVector.y > 0) {
        bulletVector.y -= 5;
    } else if(rocket.fired == true){
        rocket.firesta = true;
    }
    ellipse(bulletVector.x, bulletVector.y,10);
 
 
    
}
    
 
 
function keyPressed()
{
    if (key == "W")
    {
        rocket.thrust = true;
    }
 
    if (key == "A")
    {
        rocket.moveLeft = true;
    }
 
    if (key == "D")
    {
        rocket.moveRight = true;
    }
    
    if (key == "Q"){
        rocket.fired = true;
        rocket.firesta = true;
    }
    
    
    
}
 
 
    
 
    
 
function keyReleased(){
    if(key == "W")
    {
       rocket.thrust = false;
    }
    
    if(key == "A")
    {
       rocket.moveLeft = false;
    }
    
    if(key == "D")
    {
       rocket.moveRight = false;
    }
    
    if(key == "Q"){
        rocket.fired = false;
    }
}
 

大致是这样.你代码不全,我也没有法运行调试

您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632