如何从plist文件中激活精灵

时间:2014-03-03 23:14:10

标签: javascript animation sprite cocos2d-html5

我是Cocos2d的新手,我在 Cocos2d-HTML5 中遇到了动画问题。

下面是我的精灵的构造函数。 我已使用 TexturePacker 生成plist个文件。 我想播放动画并无限重复。我可以通过以下方式创建动画来播放一次:

var animation = new cc.Animation(frames, 0.2);

但是当我将循环计数作为第三个参数传递时,我得到错误Uncaught TypeError: Object #<Class> has no method 'getDelayUnits'

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

    var cache = cc.SpriteFrameCache.getInstance();
    cache.addSpriteFrames(s_dogList, s_Dog);

    var frames = [];
    for (var i = 1; i <= this.NUMBER_OF_FRAMES; i++) {
        var spriteFrame = cache.getSpriteFrame('dog' + i + '.png');
        frames.push(spriteFrame);
    };
    this.initWithSpriteFrame(frames[0]);

    var animation = cc.Animation.create(frames, 0.2, 100);
    var animate = cc.Animate.create(animation);
    this.runAction(animate);
}

我已经调查了代码,我发现在使用3个参数(带loops参数)创建动画时,它需要每个帧都是AnimationFrame类的实例。但是当我只传递2个参数时,帧必须是SpriteFrame类的实例。如何使用AnimationFrame创建动画并无限重复?

1 个答案:

答案 0 :(得分:1)

好的,我找到了解决方案。要永久重复给定的动画,有特殊类型的动作cc.RepeatForever。你只需这样写:

var animation = cc.Animation.create(frames, 0.1),
    actionToRepeat = cc.Animate.create(animation),
    action = cc.RepeatForever.create(actionToRepeat);