2012-04-30 51 views
-1

我有這樣的源代碼:JavaScript函數遊戲

var Game = function() { 
    var canvas = document.getElementById('world'); 
    this.context = canvas.getContext('2d'); 

我使用了一些函數來處理這個類:

Game.prototype.updateAll = function() {...} 

和一些其他功能。

現在我想使用鼠標功能(點擊右,左......)

我覺得是需要在我的遊戲類類似的東西補充:

this.canvas.addEventListener("click", this.getmouse, false); 

但是瀏覽器不要」 t給我什麼東西...

如何以及在哪裏可以在我的班級做這個功能?

+0

您是否定義了'Game.prototype.getmouse'? – Alnitak

回答

0

從您發佈的代碼中,Game沒有this.canvas。您用var canvas定義了一個局部變量,但您需要設置Game成員變量this.canvas。如果您同時需要,只需按照您當前的var canvas = ...this.canvas = canvas;