2013-04-02 71 views
0

獲取對象錯誤經歷時的財產SRC:http://www.cocos2d-x.org/reference/html5-js/symbols/cc.html的Cocos2D HTML5不能設置爲null

請注意,我用的是2.1.2 beta版本,所以我知道會出現在本教程的差異。下面的代碼我到目前爲止:

(function() { 
    var AppLayer = cc.LayerColor.extend({ 
    init: function() { 
     this._super(new cc.Color4B(0, 0, 0, 255)); 
     var size = cc.Director.getInstance().getWinSize(); 
     this._jetSprite = new JetSprite(); 
     this.setTouchEnabled(true); 
     this.setKeyboardEnabled(true); 
     this.setPosition(new cc.Point(0, 0)); 

     this.addChild(this._jetSprite); 
     this._jetSprite.setPosition(new cc.Point(size.width/2, size.height/2)); 
     this._jetSprite.scheduleUpdate(); 
     this.schedule(this.update); 
     return true; 
    }, 
    _jetSprite: null, 
    onEnter: function() { 
     this._super(); 
    }, 
    onKeyDown: function(e) { 
     this._jetSprite.handleKey(e); 
    }, 
    onKeyUp: function() { 

    }, 
    onTouchesEnded: function(pTouch, pEvent) { 
     this._jetSprite.handleTouch(pTouch[0].getLocation()); 
    }, 
    onTouchesMoved: function(pTouch, pEvent) { 
     this._jetSprite.handleTouchMove(pTouch[0].getLocation()); 
    }, 
    update: function(dt) { 

    } 
    }); 


    window.AppScene = cc.Scene.extend({ 
    onEnter: function() { 
     this._super(); 
     var layer = new AppLayer(); 
     layer.init(); 
     this.addChild(layer); 
    } 
    }); 
})(); 

var JetSprite = cc.Sprite.extend({ 
    _currentRotation:0, 
    ctor: function() { 
    this.initWithFile("images/jet.png"); 
    }, 
    handleKey: function(e) { 
    if(e == cc.KEY.left) { 
     this._currentRotation--; 
    } 
    else if(e == cc.KEY.right) { 
     this._currentRotation++; 
    } 

    if(this._currentRotation < 0) { 
     this._currentRotation = 360; 
    } 
    if(this._currentRotation > 360) { 
     this._currentRotation = 0; 
    } 
    }, 
    handleTouch: function(touchLocation) { 
    if(touchLocation.x < 300) { 
     this._currentRotation = 0; 
    } 
    else { 
     this._currentRotation = 180; 
    } 
    }, 
    handleTouchMove: function(touchLocation) { 
    var angle = Math.atan2(touchLocation.x - 300, touchLocation.y - 300); 

    angle = angle * (180/Math.PI); 
    this._currentRotation = angle; 
    }, 
    update: function(dt) { 
    this.setRotation(this._currentRotation); 
    } 
}); 

下面是詳細的錯誤:

Uncaught TypeError: Cannot set property 'src' of null CCSprite.js:2209 
cc.SpriteWebGL.cc.Node.extend.initWithTexture CCSprite.js:2209 
(anonymous function) CCSprite.js:2161 

回答

2

貌似教程我是繼缺少一條線。 ctor函數需要一個this._super(),這樣所有東西都可以正確初始化。

var JetSprite = cc.Sprite.extend({ 
    _currentRotation:0, 
    ctor: function() { 
    this._super(); 
    this.initWithFile("images/jet.png"); 
    },