这是一个使用 cocos2d-html5 的网页游戏。
我有一个游戏层,我想在里面处理鼠标悬停事件:
var GameLayer = cc.Layer.extend({
init:function () {
// ......
this.curPosition = null;
if( 'mouse' in sys.capabilities ) {
//this.setMouseEnabled(true);
this.mouseCaptured = false;
canvas = document.getElementById('gameCanvas');
canvas.addEventListener('mousemove', function(evt) {
var rect = document.getElementById('gameCanvas').getBoundingClientRect();
var curPos = new cc.Point();
curPos.x = evt.clientX - rect.left;
curPos.y = evt.clientY - rect.top;
// the problem is here, this is wrong since "this" stands for canvas here
this.curPosition = curPos
// also wrong
this.updatePosition(curPos);
}, false);
}
// ......
},
updatePosition:function (position) {
this.currentPosition = position;
// ......
}
});
var HelloWorldScene = cc.Scene.extend({
onEnter:function () {
this._super();
var layer = new GameLayer();
layer.init();
this.addChild(layer);
}
});
我想要的只是在侦听器中调用函数或设置类变量。
希望有大神指点一下,谢谢:)