8
Html5 mousemove event in cocos2d-html5
source link: https://www.codesd.com/item/html5-mousemove-event-in-cocos2d-html5.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Html5 mousemove event in cocos2d-html5
advertisements
This is a web game using cocos2d-html5.
I have a game layer and I want to handle mouseover event inside:
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);
}
});
All I want is to call functions or set class variable inside the listener.
Hope someone can give a hint, thanks :)
you can use
init:function () {
var self=this;
//......
Then you can call the class-functions inside the listener with
canvas.addEventListener('mousemove', function(evt) {
self.updatePosition(1);
// ....
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK