如何修正当AI打开左侧球拍时右侧球拍不起作用的问题?在canvs javascript的乒乓球比赛

问题描述 投票:0回答:1

我在画布上制作一个乒乓球游戏,并且当左侧拨片设置为AI时,右侧拨片的AI或移动键不起作用。

以下是桨对象更新(人体移动)和updateAI函数的代码:

/////////NEED TO FIX AI SO RIGHT PADDLE MOVEMENT WORKS ALL THE ITME AND AI DOES TOO
    Update: function(modifier) {
        //is game paused?
        //LEFT OR RIGHT PLAYER?
        if (this.side === 'left') {
            if (this.isHuman === true) {
                //is player object within boundaries?
                if (this.y >= 0 && this.y <= (canvas.height - this.height)) {

                    if (65 in keysDown) { // Player holding a its 65keycode or 97ascii
                        this.y -= this.speed * modifier;
                    }

                    if (90 in keysDown) { // Player holding z its 90key or 122ascii
                        this.y += this.speed * modifier;
                    }
                }
            }
            else if(this.isHuman === false){this.UpdateAI(modifier);}
        //reset paddle if too high or low
            if (this.y < 0) {this.y = 0;}
            if (this.y + this.height > canvas.height) {this.y = canvas.height - this.height;}
        }
        else if (this.side === 'right') {
            if (this.isHuman === true) {
                //is player object within boundaries?
                if (this.y >= 0 && this.y <= (canvas.height - this.height)) {

                    if (75 in keysDown) { // Player holding k
                        this.y -= this.speed * modifier;
                    }

                    if (77 in keysDown) { // Player holding m
                        this.y += this.speed * modifier;
                    }
                }
            }
            else if(this.isHuman === false){this.UpdateAI(modifier);}
        //reset paddle if too high or low
            if (this.y < 0) {this.y = 0;}
            if (this.y + this.height > canvas.height) {this.y = canvas.height - this.height;}
        }
    },
    UpdateAI: function(modifier) {
        //create an AI timer or REACTION TIME
        for (i = 0; i < Balls.length; i++) {
            //FAST REACTION
            //the paddle wants it's center to have an equal y coordinate to the ball's y coordinate
            //if the ball is lower than the paddle       paddle goes lower
            if ((this.y + this.height/2) + this.deadZone < Balls[i].GetY()) {this.y += this.speed * modifier;}
            //if the ball is higher than the paddle       paddle goes higher
            else if ((this.y + this.height/2) - this.deadZone > Balls[i].GetY()) {this.y -= this.speed * modifier;}
        }
    },
    /////////////////FIX AI

乒乓球拍是一组名为Paddles的Paddle对象。我在主循环中调用UpdatePaddles,然后UpdatePaddles运行Paddle [0] .Update,然后运行Paddles [1] .Update。如果isHuman设置为Paddle.Update检查按键,如果isHuman关闭则运行.UpdateAI更新位置。

以下是切换paddles的isHuman变量的按钮:

var SwitchLeftAIOnOff = function() {
    Paddles[0].SetIsHuman(!Paddles[0].GetIsHuman());
    if (Paddles[0].GetIsHuman()) {
        leftAIButton.value = 'Left Player: Human';
    }
    else {leftAIButton.value = 'Left Player: AI';}
};

var SwitchRightAIOnOff = function() {
    Paddles[1].SetIsHuman(!Paddles[1].GetIsHuman());
    if (Paddles[1].GetIsHuman()) {
        rightAIButton.value = 'Right Player: Human';
    }
    else {rightAIButton.value = 'Right Player: AI';}
};

当左侧桨叶设置为人体时右侧AI工作,当左侧是人类时右侧桨叶移动键工作,但如果左侧是AI,则不起作用。如果我需要提供更多代码,请告诉我。

javascript html5 canvas
1个回答
0
投票

我的朋友帮我解决了这个问题,我的所有for循环迭代器都没有var i,所以它出了问题!

© www.soinside.com 2019 - 2024. All rights reserved.