【问题标题】:A simple sprite sheet animation in Cocos2d-JSCocos2d-JS 中一个简单的精灵表动画
【发布时间】:2014-06-02 03:30:21
【问题描述】:

基于 helloworld-example 和 cocos-2d-x 文档 (http://www.cocos2d-x.org/wiki/Sprite_Sheet_Animation) 我尝试制作一个简单的精灵表动画。代码如下:

this.mostafa = cc.Sprite.create(res.Mostafa_png);        
this.mostafa.attr({
    x: size.width / 3,
    y: size.height / 3,
    scale: 0.2,
    rotation: 180
});
this.addChild(this.mostafa, 0);
var rotate = cc.RotateTo.create(2, 0);

cc.spriteFrameCache.addSpriteFrames(res.Mostafa_plist);

var animFrames = [];
var str = "";
for (var i = 1; i < 9; i++) {
    str = "mosquito_fly" + (i < 10 ? ("0" + i) : i) + ".png";
    var frame = cc.spriteFrameCache.getSpriteFrame(str);
    animFrames.push(frame);
}
var animation = cc.Animation.create(animFrames, 0.04);
var animate   = cc.Animate.create(animation); 

this.mostafa.runAction(animate);  // shows nothing
//this.mostafa.runAction(rotate);   // shows turning sprite

它没有显示任何东西。但是,如果我放入最后一行并放出第二行,那么它会显示一个旋转的精灵。 (精灵帧缓存加载正确)

缺少什么?

【问题讨论】:

    标签: animation sprite-sheet cocos2d-js


    【解决方案1】:

    问题是双重的。首先必须为动画定义一个纹理,其次,如果它应该是一个连续动画,那么 animFrames 必须是 animationFrame 类型。工作代码如下(放入helloworld例子的ctor函数中):

    // Create sprite and set attributes
        this.mostafa = cc.Sprite.create(res.Mostafa_single_png);        
        this.mostafa.attr({
            x: size.width / 3,
            y: size.height / 3,
            scale: 0.5,
            rotation: 0
        });
        this.addChild(this.mostafa, 0);
    
    // Load sprite frames to frame cache, add texture node
        cc.spriteFrameCache.addSpriteFrames(res.Mostafa_plist);
        var mostafaTexture = cc.textureCache.addImage(res.Mostafa_png),
            mostafaImages  = cc.SpriteBatchNode.create(mostafaTexture);
        this.addChild(mostafaImages);
    
        var animFrames = [];
        var str = "";
        for (var i = 1; i < 9; i++) {
            str = "mosquito_fly" + (i < 10 ? ("0" + i) : i) + ".png";
            var spriteFrame = cc.spriteFrameCache.getSpriteFrame(str);
            var animFrame = new cc.AnimationFrame();
                animFrame.initWithSpriteFrame(spriteFrame, 1, null);
            animFrames.push(animFrame);
        }
        var animation = cc.Animation.create(animFrames, 0.08, 100);
        var animate   = cc.Animate.create(animation); 
    
        this.mostafa.runAction(animate); 
    

    【讨论】:

      【解决方案2】:
      junk = this;
      cc.spriteFrameCache.addSpriteFrames("res/e.plist");
      spriteSheet = new cc.SpriteBatchNode("res/e.png");
      junk.addChild(spriteSheet,15);
      
      var animFrames = [];
      for (var i = 1; i < 19; i++) {
          var str = "e/e"+i+".png";
          var frame = cc.spriteFrameCache.getSpriteFrame(str);
          animFrames.push(frame);
      }
      var animation = new cc.Animation(animFrames, 0.1);
      runningAction =  new cc.RepeatForever(new cc.Animate(animation));
      
      mask = new cc.Sprite("#e/e1.png");
      mask.attr({
          x: 400,
          y:105,
          anchorX: 0.5,
          anchorY: 0.5,
          scale:0.5
      });
      spriteSheet.addChild(mask,25);
      mask.runAction(runningAction)
      

      【讨论】:

        猜你喜欢
        • 2014-05-11
        • 1970-01-01
        • 1970-01-01
        • 2010-12-18
        • 1970-01-01
        • 1970-01-01
        • 2012-07-18
        • 2014-05-16
        • 2011-11-05
        相关资源
        最近更新 更多