cocos2d-Html5中的Html5鼠标移动事件
这是一个使用cocos2d-html5的网络游戏 我有一个游戏层,我想在里面处理鼠标悬停事件:cocos2d-Html5中的Html5鼠标移动事件,html,cocos2d-iphone,mouseevent,Html,Cocos2d Iphone,Mouseevent,这是一个使用cocos2d-html5的网络游戏 我有一个游戏层,我想在里面处理鼠标悬停事件: var GameLayer = cc.Layer.extend({ init:function () { // ...... this.curPosition = null; if( 'mouse' in sys.capabilities ) { //this.setMouseEnabled(true);
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);
}
});
我只想在侦听器中调用函数或设置类变量
希望有人能给你一个提示,谢谢:)你可以用
init:function () {
var self=this;
//......
然后,您可以使用
canvas.addEventListener('mousemove', function(evt) {
self.updatePosition(1);
// ....
Cocos2d-html5确实支持鼠标悬停事件。您需要将其添加到init函数中
if( 'mouse' in sys.capabilities ) {
this.setMouseEnabled(true);
}
然后将onMouseMoved委托函数添加到层中
onMouseMoved: function(event) {
var pos = event.getLocation();
console.log("onMouseMoved at: " + pos.x + " " + pos.y );
}
要查看事件委托系统的工作方式,请执行以下操作: