js cocos 中鼠标点击事件调用其它方法出错, uncaught typeerror: undefined is not a function 5
代码如下:/***CreatedbyAdministratoron2014/9/30.*/varShooter=cc.Sprite.extend({speed:100,H...
代码如下:
/**
* Created by Administrator on 2014/9/30.
*/
var Shooter=cc.Sprite.extend({
speed:100,
HP:5,
layer:null,
ctor:function(layer,x,y){
this._super("#ship03.png");
this.tag = this.zOrder;
this.x = x;
this.y = y;
this.scale=1;
this.layer=layer;
this.shootBullet(this.x,this.y);
this.addMouse();
},
addMouse: function () {
if ('mouse' in cc.sys.capabilities) {
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: function (event) {
cc.log("shoot MOUSE DOWN EVENT");
this.shootBullet(this.x,this.y);
},
onMouseUp: function (event) {
cc.log("shoot MOUSE UP EVENT");
}
}, this);
cc.log("ADD shoot MOUSE");
} else {
cc.log("MOUSE Not supported");
}
},
//添加子弹,并且向上移动;
shootBullet: function (x,y) {
cc.log("Bullet shooted");
var Bullet = new Bullets(x, y);
Bullet.setTag(1);
Bullet.Move();
Bullet.setTag(6);
this.layer.addChild(Bullet, 2);
this.layer._bullets.push(Bullet);
GC.FILE = false;
}
});
在点击事件中调用this.shootBullet时报错,估计在Listener中this指针已经发生变化,但是不知道解决方法,望大神相助~~ 展开
/**
* Created by Administrator on 2014/9/30.
*/
var Shooter=cc.Sprite.extend({
speed:100,
HP:5,
layer:null,
ctor:function(layer,x,y){
this._super("#ship03.png");
this.tag = this.zOrder;
this.x = x;
this.y = y;
this.scale=1;
this.layer=layer;
this.shootBullet(this.x,this.y);
this.addMouse();
},
addMouse: function () {
if ('mouse' in cc.sys.capabilities) {
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: function (event) {
cc.log("shoot MOUSE DOWN EVENT");
this.shootBullet(this.x,this.y);
},
onMouseUp: function (event) {
cc.log("shoot MOUSE UP EVENT");
}
}, this);
cc.log("ADD shoot MOUSE");
} else {
cc.log("MOUSE Not supported");
}
},
//添加子弹,并且向上移动;
shootBullet: function (x,y) {
cc.log("Bullet shooted");
var Bullet = new Bullets(x, y);
Bullet.setTag(1);
Bullet.Move();
Bullet.setTag(6);
this.layer.addChild(Bullet, 2);
this.layer._bullets.push(Bullet);
GC.FILE = false;
}
});
在点击事件中调用this.shootBullet时报错,估计在Listener中this指针已经发生变化,但是不知道解决方法,望大神相助~~ 展开
1个回答
展开全部
/**
* Created by Administrator on 2014/9/30.
*/
var Shooter = cc.Sprite.extend({
speed: 100,
HP: 5,
layer: null,
ctor: function(layer, x, y) {
this._super("#ship03.png");
this.tag = this.zOrder;
this.x = x;
this.y = y;
this.scale = 1;
this.layer = layer;
this.shootBullet(this.x, this.y);
this.addMouse();
},
addMouse: function() {
var that = this; //缓存this指针
if ('mouse' in cc.sys.capabilities) {
cc.eventManager.addListener({
event: cc.EventListener.MOUSE,
onMouseDown: function(event) {
cc.log("shoot MOUSE DOWN EVENT");
that.shootBullet(that.x, that.y);
},
onMouseUp: function(event) {
cc.log("shoot MOUSE UP EVENT");
}
}, that);
cc.log("ADD shoot MOUSE");
} else {
cc.log("MOUSE Not supported");
}
},
//添加子弹,并且向上移动;
shootBullet: function(x, y) {
cc.log("Bullet shooted");
var Bullet = new Bullets(x, y);
Bullet.setTag(1);
Bullet.Move();
Bullet.setTag(6);
this.layer.addChild(Bullet, 2);
this.layer._bullets.push(Bullet);
GC.FILE = false;
}
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询