Change Animations with one Sprite?

Hi, I’m working on a character with sprite and animations.

unique_ptr<Sprite> sprite;
map<string, unique_ptr<Animate>> animations;
...
animations["Idle"]->initWithAnimation(Animation::createWithSpriteFrames(frames, 1.0 / numImgs));
sprite->initWithSpriteFrame(frames.front());

What I did before was to create one Sprite per one Animation.

But I don’t want to create by Sprite::createWithSpriteFrame() cause of allocating Sprite per Animation(I thought it need more memory).

Is that code right way to re-use one allocated Sprite object? or use setSpriteFrame() ?

I think the best way for create animations is to have 2 files:

  • png that contains all frames.
  • plist that contains info of each frame.

I use ShoeBox for generate animation files.
After create both files, you can use AnimationCache:

Pre-load animation:
AnimationCache::getInstance()->addAnimationsWithFile("animation.plist");

Run animation:
sprite->runAction(RepeatForever::create(Animate::create(AnimationCache::getInstance()->getAnimation("animation"))));

Also it will be convenient use SpriteFrameCache for load the first frame as Sprite.

Thanks a lot.

I used TexturePacker to make .plist file but it does not have Animation information in .plist file. I failed to try what you wrote.

I installed ShoeBox, you mentioned, but I couldn’t even find Animation information in .plist file of ShoeBox.

If you don’t mind, could you give me some information about extracting .png files to .plist file within Animation?

PNG and PLIST should be generated by the tool.
And after that, i forgot mention the third file: animation.plist.

In my case, i create this third file manually. Example:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>animations</key>
 

    <dict>
        <key>animation</key>
        <dict>
            <key>delay</key>
            <real>0.2</real>
            <key>frames</key>
            <array>
                <string>sprite1.png</string>
                <string>sprite2.png</string>
                <string>sprite3.png</string>
                <string>sprite4.png</string>
                <string>sprite5.png</string>
           </array>
        </dict>
    </dict>    
   
    
</dict>
</plist>

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.