2015-09-19 82 views
1

在Cocos2d-x V 3.81項目中,我試圖在擴展Sprite的對象內部創建一個Sprite動畫,但它似乎沒有做任何事情。是否有可能在這樣一個節點中有一個動畫精靈,還是它必須擴展圖層以獲得動畫?Sprite對象中的Cocos2d動畫

var Player = cc.Sprite.extend ({ 
ctor: function() { 

    this._super(res.Player_png); 

    this.UP = false; 
    this.DOWN = false; 
    this.LEFT = false; 
    this.RIGHT = false; 
    this.ACTION = false; 

    this.speed = 5; 

    cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist); 

    var leftFrames = []; 
    for(var i = 0; i < 4; i++) 
    { 
     var str = "Left" + i + ".png"; 
     var frame = cc.spriteFrameCache.getSpriteFrame(str); 
     leftFrames.push(frame); 
    } 

    this.leftAnim = new cc.Animation(leftFrames, 0.3); 
    this.runLeft = new cc.repeatForever(new cc.Animate(this.leftAnim)); 

    this.state = "nothing"; 
    this.nothing = "nothing"; 

    this.scheduleUpdate(); 

    cc.eventManager.addListener (
     cc.EventListener.create ({ 
      event: cc.EventListener.KEYBOARD , 
      onKeyPressed: function(key, event) 
      { 
       if(key == 87) this.UP = true; 
       else if(key == 65) this.LEFT = true; 
       else if(key == 83) this.DOWN = true; 
       else if(key == 68) this.RIGHT = true; 
       else if (key == 69 || key == 32) this.ACTION = true; 
      }.bind(this), 
      onKeyReleased: function(key, event) 
      { 

       if(key == 87) this.UP = false; 
       else if(key == 65) this.LEFT = false; 
       else if(key == 83) this.DOWN = false; 
       else if(key == 68) this.RIGHT = false; 
       else if (key == 69 || key == 32) this.ACTION = false; 
      }.bind(this) 
     }),this); 

    return true; 
}, 

update:function(dt) { 
    if(this.UP) 
    { 
     this.y += this.speed; 
     this.runAction(this.runLeft); 
    } 
    else if(this.DOWN) 
    { 
     this.y -= this.speed; 
    } 
    if(this.LEFT) 
    { 
     this.x -= this.speed; 
     this.runAction(this.runLeft); 
    } 
    else if(this.RIGHT) 
    { 
     this.x += this.speed; 
    } 
} 
}); 
+0

你應該標記爲正確的user1615873的答案 –

回答

0

的多元化主要是這個問題。

對於那些好奇的人,這裏是功能代碼塊:

你是對的。非常感謝你。對於那些有興趣的人,這是功能代碼塊:

cc.spriteFrameCache.addSpriteFrames(res.playerRun_plist); 
    var i,f; 
    var frames=[]; 
    for (i=1; i <= 4; i++) { 
     f=cc.spriteFrameCache.getSpriteFrame("playerRun"+i+".png"); 
     frames.push(f); 
    } 
    var playerRunAnim = new cc.Animation(frames, 0.1); 
    this.playerAction = new cc.RepeatForever(new cc.Animate(playerRunAnim)); 

    this.runAction(this.playerAction); 
0

此代碼適用於Sprite類以外的您?例如在一個普通的精靈?

我認爲這是錯誤的,但我沒有測試它:

this.leftAnim =新cc.Animation(leftFrames,0.3); this.runLeft = new cc.repeatForever(new cc.Animate(this.leftAnim));

與您的代碼是什麼我會做:

var leftAnim = new cc.Animation(); 
    for(var i = 0; i < 4; i++) 
    { 
     var str = "Left" + i + ".png"; 
     var frame = cc.spriteFrameCache.getSpriteFrame(str); 
     leftAnim.addSpriteFrame(frame); 
    } 
leftAnim.setDelayPerUnit(0.08); 
this.runAction(cc.animate(leftAnim)); //or this.runAction(cc.animate(leftAnim).repeatForever()); 

希望它可以幫助

1

我認爲這個問題是這一行:

cc.spriteFrameCache.addSpriteFrame(res.Player_Left_plist); 

它應該閱讀

cc.spriteFrameCache.addSpriteFrames(res.Player_Left_plist); 

其中SpriteFrames i複數。在您手動創建SpriteFrame後,最後沒有「s」的方法用於將單個cc.SpriteFrame對象加載到緩存中。最後使用's'的方法是爲plist提供一個URL來一次加載整個spritesheet的方法。

http://www.cocos2d-x.org/reference/html5-js/V3.8/symbols/cc.spriteFrameCache.html