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

时间:2018-02-25 23:14:42

标签: javascript html5 canvas

我正在画布上进行乒乓球游戏,并且当左侧拨片设置为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更新位置。

以下是切换拨片的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,则不起作用。如果我需要提供更多代码,请告诉我。

1 个答案:

答案 0 :(得分:0)

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