为什么setPosition()有错误呢,Uncaught TypeError: Cannot read property 'setPosition' of undefined

这个是我拖动图片的代码,但是为什么报错呢,错误是:Uncaught TypeError: Cannot read property ‘setPosition’ of undefined

plane: null,
isDrag:null,

ctor:function(){
   this._super();

    //背景
   this.setColor(cc.color(126,126,126,126));
   //airplane
    var size=cc.winSize;
    this.plane=new cc.Sprite(res.airPlane);
    this.plane.setPosition(cc.p(size.width*0.5,this.plane.getContentSize().height*0.5));
    this.addChild(this.plane,1);

    this.moveAirPlane();

   return true;

}

moveAirPlane: function () {
if(‘mouse’ in cc.sys.capabilities){
cc.eventManager.addListener({
event:cc.EventListener.MOUSE,
onMouseDown: function (event) {
var pos=event.getLocation();
if(event.getButton()==cc.EventMouse.BUTTON_LEFT)
{
cc.log(“left:”+pos.x+"/"+pos.y);
}
else if(event.getButton()==cc.EventMouse.BUTTON_RIGHT)
{
cc.log(“left:”+pos.x+"/"+pos.y);
}
this.isDrag=true;
},
onMouseUp: function (event) {
var pos=event.getLocation();
cc.log(“up:”+pos.x+"/"+pos.y);
this.isDrag=false;
},
onMouseMove: function (event) {
if(this.isDrag==true){
var pos=event.getLocation();
cc.log(“move:”+pos.x+"/"+pos.y);
this.plane.setPosition(pos);
}
}
},this);
}
}

一般是前面的对象传参弄错了

应该是this.plane.setPosition(pos);中的this.plane是空的,打个log看看

onMouseXXX 里面不应该用this
在 moveAirPlane 定义个 变量 var _self = this
onMouseXXX 里的this 全用 _self 替换。

3个月以前的问题了。。。论坛真冷清

哇哦,现在才被翻出来。。。js闭包没搞明白