[Solved].plist animation problem

I want to share an answer to the problem i had with managing he animations.I’m only a beginner ,so please correct me if i made any mistakes. Okay so i was trying to reasonably manage my animations in cocos2d-x, and i was looking for the best way to do it. I thought that loading every frame in the code was a bit too troublesome when i’m using a lot of animation. So i wanted to go with this method:

auto cache = AnimationCache::getInstance();
cache->addAnimationsWithFile("animations/animations-2.plist");
auto animation = cache->getAnimation("dance_1");  
auto animate = Animate::create(animation);  
sprite->runAction(animate);

but in general my problem that i was trying to pack sprites using texturepacker or shoebox(it does support.plist),and they didn’t name the animation packed inside, so i didn’t know how to use getAnimationByName() method. So i took a look on the ActionTest.cpp and animations.plist file used there.

<?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>dance_1</key>
        <dict>
            <key>delay</key>
            <real>0.2</real>
            <key>frames</key>
            <array>
                <string>grossini_dance_01.png</string>
                <string>grossini_dance_02.png</string>
                <string>grossini_dance_03.png</string>
                <string>grossini_dance_04.png</string>
                <string>grossini_dance_05.png</string>
                <string>grossini_dance_06.png</string>
                <string>grossini_dance_07.png</string>
                <string>grossini_dance_08.png</string>
                <string>grossini_dance_09.png</string>
                <string>grossini_dance_10.png</string>
                <string>grossini_dance_11.png</string>
                <string>grossini_dance_12.png</string>
                <string>grossini_dance_13.png</string>
                <string>grossini_dance_14.png</string>
            </array>
        </dict>
        <key>dance_2</key>
        <dict>
            <key>delay</key>
            <real>0.2</real>
            <key>frames</key>
            <array>
                <string>grossini_dance_gray_01.png</string>
                <string>grossini_dance_gray_02.png</string>
                <string>grossini_dance_gray_03.png</string>
                <string>grossini_dance_gray_04.png</string>
                <string>grossini_dance_gray_05.png</string>
                <string>grossini_dance_gray_06.png</string>
                <string>grossini_dance_gray_07.png</string>
                <string>grossini_dance_gray_08.png</string>
                <string>grossini_dance_gray_09.png</string>
                <string>grossini_dance_gray_10.png</string>
                <string>grossini_dance_gray_11.png</string>
                <string>grossini_dance_gray_12.png</string>
                <string>grossini_dance_gray_13.png</string>
                <string>grossini_dance_gray_14.png</string>
            </array>
        </dict>
        <key>dance_3</key>
        <dict>
            <key>delay</key>
            <real>0.2</real>
            <key>frames</key>
            <array>
                <string>grossini_blue_01.png</string>
                <string>grossini_blue_02.png</string>
                <string>grossini_blue_03.png</string>
                <string>grossini_blue_04.png</string>
            </array>
        </dict>
    </dict>
</dict>
</plist>

and i saw that animations are named there but the png files are not “linked” , the pngs were in another .plist file that was very similar to the one generated by shoebox or texturepacker.

So what i did was :

  1. Generating my spritesheet with shoebox (link) (let’s say i called it sprites,plist)
  2. Cuting out a piece of animations.plist file and changing frame names to fit the ones in my sprites.plist file (you can open any .plist file with notepad or notepad++, or anything basically).
  3. Using both files in my code.

So i named my frames in sprites.plist file for example frame0000,frame0001 etc.
and i created the animations.plist file (just changed the file extension from .txt to .plist)
with this code inside :

<?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_1</key>
        <dict>
            <key>delay</key>
            <real>0.2</real>
            <key>frames</key>
            <array>
                <string>frame0000.png</string>
                <string>frame0001.png</string>
                <string>frame0002.png</string>
                <string>frame0003.png</string>
                <string>frame0004.png</string>
                <string>frame0005.png</string>
                <string>frame0006.png</string>
                <string>frame0007.png</string>

            </array>
        </dict>
    </dict>
</dict>
</plist>

and then using this code:

SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sprites/sprites.plist");
AnimationCache::getInstance()->addAnimationsWithFile("sprites/animations.plist");
auto sprajcik = Sprite::createWithSpriteFrameName("frame0000.png");
  sprajcik->runAction(RepeatForever::create(Animate::create(AnimationCache::getInstance()->getAnimation("animation_1"))));
3 Likes

anim.zip (3.9 KB)


SpriteFrameCache::getInstance()->addSpriteFramesWithFile(“anim/bulletplist”,“anim/bulletani.png”);
AnimationCache::getInstance()->addAnimationsWithFile(“anim/animation.plist”);
auto bullet1 = Sprite::createWithSpriteFrameName(“0.png”);
bullet1->setPosition(ccp(ScreenSize.width/2,ScreenSize.height/2));
this->addChild(bullet1,1);
bullet1->runAction(RepeatForever::create(Animate::create(AnimationCache::getInstance()->getAnimation(“animation_1”))));

but only drawing is done not animation.