2014-10-10 89 views
0

編輯使用鍵盤輸入:解決2個物體

我在日本高中的學生努力學習如何編程。 我最近查看https://vimeo.com/105955605這個視頻,並決定我可以使用開始部分開始在JavaScript中建立pong。

我幾乎是一個完整的新手編程和/或JavaScript,我還有很長的路要走。

我得到了Player1(左槳)自己的工作,所以我想我可以複製粘貼,與一些事情混亂,並使Player2。然而,現在當我按下w/s時,Player2會移動,但Player1不再移動。我試過在player2(Player2.keyboarder = Player1.keyboarder())中使用Player1中的Player1中的this.keyboarder創建2個單獨的keyboarder()函數,並在做其他任何事情之前聲明/調用keyboarder()。

HTML:

<html> 
    <head> 
     <meta charset="UTF-8"> 
     <title>Pong</title> 
     <link type="text/css" rel="stylesheet" href="css/stylesheet.css"> 
    </head> 
    <body> 
     <canvas id="screen" width="310" height="210"></canvas> 
     <script src="js/pong.js"></script> 
    </body> 
</html> 

的JavaScript:

;(function(){ 

//Main game function 
    //tells objects in bodies array to update. 
    //stores gameSize pulled from canvasId 
    var Game = function(canvasId){ 
     var canvas = document.getElementById(canvasId); 
     var screen = canvas.getContext('2d'); 
     var gameSize = {x: canvas.width, y: canvas.height}; 
     var self = this; 
//bodies array 
     this.bodies = [new Player1(this, gameSize), new Player2(this, gameSize)]; 
//update function 
     var tick = function(){ 
     self.update(); 
     self.draw(screen,gameSize); 
     requestAnimationFrame(tick); 
     }; 
     tick(); 
    }; 
//constructer for game() function. tells bodies to update, and draw 
    Game.prototype = { 
     update: function(){ 
      for(var i =0 ; i < this.bodies.length; i++){ 
       this.bodies[i].update(); 
      } 
     }, 
     draw:function(screen,gameSize){ 
      screen.clearRect(0,0,gameSize.x,gameSize.y); 
      for(var i =0 ; i < this.bodies.length; i++){ 
       drawRect(screen, this.bodies[i]); 
      } 
     } 
    }; 
//P1 object, declares size and start position of P1 
    var Player1= function(game, gameSize){ 
     this.size = {x:30,y:gameSize.y/3}; 
     this.game = game; 
     this.gameSize = gameSize; 
     this.center = {x: 0, y:gameSize.y/2}; 
     this.keyboarder = new Keyboarder(); 
     requestAnimationFrame(this.update); 
    }; 
//constructor for P1, updates position based on keyboard input 
    Player1.prototype = { 
     update:function(){ 
      if (this.keyboarder.isDown(this.keyboarder.KEYS.DOWN) && this.center.y < (5*this.gameSize.y/6)){ 
       this.center.y += 4; 
      }else if(this.keyboarder.isDown(this.keyboarder.KEYS.UP) && this.center.y > this.size.y /2){ 
       this.center.y -= 4; 
      } 
     } 
    }; 
//P2, same as P1 aside from position 
    var Player2= function(game, gameSize){ 
     this.size = {x:30,y:gameSize.y/3}; 
     this.game = game; 
     this.gameSize = gameSize; 
     this.center = {x: gameSize.x, y:gameSize.y/2}; 
     this.keyboarder = new Keyboarder(); 
     requestAnimationFrame(this.update); 
    }; 
//constructor for P2, same as P1 
    Player2.prototype = { 
     update:function(){ 
      if (this.keyboarder.isDown(this.keyboarder.KEYS.S) && this.center.y < (5*this.gameSize.y/6)){ 
       this.center.y += 4; 
      }else if(this.keyboarder.isDown(this.keyboarder.KEYS.W) && this.center.y > this.size.y /2){ 
       this.center.y -= 4; 
      } 
     } 
    }; 
//Draw function, draws object 
    var drawRect = function(screen, body){ 
     screen.fillRect(body.center.x - body.size.x /2, 
       body.center.y - body.size.y /2, body.size.x,body.size.y); 
    }; 
//Keyboard input function 
    //reads if keys are being pressed and takes the event code 
    //isDown() returns boolean of key down = true, key up = false 
    var Keyboarder = function(
     ){ 
     var keyState = {}; 

     window.onkeydown = function(e){ 
      keyState[e.keyCode] = true; 
     }; 
     window.onkeyup = function(e){ 
      keyState[e.keyCode] = false; 
     }; 
     this.KEYS = {DOWN: 37, UP:39,W:87 , S: 83}; 

     this.isDown = function(keyCode){ 
      return keyState[keyCode] === true; 
     }; 

    }; 
//calls game() function when the page has loaded. 
    window.onload = function(){ 
     new Game("screen") 
    }; 
})(); 

很抱歉,如果這是不好的協議,用於使用計算器,我也新提出問題在這裏。

+0

無需編輯它即可解決問題 - 如果其中一個答案幫助您解決了問題,則可以使用勾號接受它。如果有一個以上是有用的,可以隨意投它們兩個,然後選擇一個作爲「最有幫助的」。 – 2014-10-10 11:27:34

回答

0

當你創建兩個Keyboarder情況下,一個與其他碰撞,因爲這樣:

window.onkeydown = function(e){ 
    keyState[e.keyCode] = true; 
}; 
window.onkeyup = function(e){ 
    keyState[e.keyCode] = false; 
}; 

當你創建一個Keyboarder,事件偵聽器連接上一個被覆蓋。嘗試這樣的:

window.addEventListener('keydown', function(e){ 
    keyState[e.keyCode] = true; 
}); 
window.addEventListener('keyup', function(e){ 
    keyState[e.keyCode] = false; 
}); 

這確保有每個Keyboarder實例的偵聽器。

+0

謝謝!它工作完美! – Spaghet 2014-10-10 11:24:41

1

問題是你有兩個Keyboarder實例,它們都通過直接給它們分配一個處理程序來綁定到鍵事件 - 這將覆蓋任何其他處理程序。有兩種方法來解決這個問題:

1:不要直接分配,而是使用addEventListener,如:

window.addEventListener('keydown', function(e){ 
    keyState[e.keyCode] = true; 
}); 

2:使用相同keyboarder實例爲雙方球員:

var kb = new Keyboarder(); 
this.bodies = [new Player1(this, gameSize, kb), new Player2(this, gameSize, kb)]; 

並在你的播放器對象中分配給第三個參數。即使你走這條路,我仍然建議使用addEventListener,以確保事件可以在多個地方綁定,如果需要的話。

另一方面,您還應該能夠將玩家重構爲單個函數,並使用不同的參數創建它的兩個實例,但這種事情最好在Code Review上處理。

+0

謝謝!它使用window.addEventListener方式工作。 – Spaghet 2014-10-10 11:25:07