【参考文章】http://www.cnblogs.com/suguoqiang/archive/2013/04/03/2997316.html
在HelloWorldScene.h中声明void roleRun();
在HelloWorldScene.cpp实现如下:
void HelloWorld::roleRun()
{
//1.读取素材文件
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("RoleRun.plist"); //从plist中加载图片信息
//2.创建逐帧数组
Vector <SpriteFrame*> animFrames1;
char str1[100] = { 0 };
for (int i = 0; i<10; i++)
{
sprintf(str1, "RoleRun%d.png", i);
SpriteFrame* pFrame = cache->getSpriteFrameByName(str1);
animFrames1.pushBack(pFrame);
}
//3.设置起始帧
Size s = Director::getInstance()->getVisibleSize();
Sprite* sprite1 = Sprite::createWithSpriteFrameName("RoleRun0.png");//纹理plist中包含RoleRun0.png
sprite1->setPosition(Point(s.width / 2, s.height / 4));
addChild(sprite1);
//4.执行动画
Animation* animation1 = Animation::createWithSpriteFrames(animFrames1, 0.2);
sprite1->runAction(RepeatForever::create(Animate::create(animation1)));
}
在HelloWorld::init()中调用roleRun()即可。
附:
1、RoleRun.plist
RoleRun.plist
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>frames</key>
<dict>
<key>RoleRun0.png</key>
<dict>
<key>frame</key>
<string>{{0,0},{100,124}}</string>
<key>offset</key>
<string>{-4,-7}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{16,15},{100,124}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun1.png</key>
<dict>
<key>frame</key>
<string>{{100,0},{92,118}}</string>
<key>offset</key>
<string>{1,-3}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{25,14},{92,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun2.png</key>
<dict>
<key>frame</key>
<string>{{192,0},{104,112}}</string>
<key>offset</key>
<string>{1,-1}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{19,15},{104,112}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun3.png</key>
<dict>
<key>frame</key>
<string>{{296,0},{110,114}}</string>
<key>offset</key>
<string>{-2,-2}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{13,15},{110,114}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun4.png</key>
<dict>
<key>frame</key>
<string>{{406,0},{112,118}}</string>
<key>offset</key>
<string>{-6,-5}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{8,16},{112,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun5.png</key>
<dict>
<key>frame</key>
<string>{{518,0},{98,118}}</string>
<key>offset</key>
<string>{-7,-6}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{14,17},{98,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun6.png</key>
<dict>
<key>frame</key>
<string>{{616,0},{102,122}}</string>
<key>offset</key>
<string>{-3,-5}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{16,14},{102,122}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun7.png</key>
<dict>
<key>frame</key>
<string>{{718,0},{96,118}}</string>
<key>offset</key>
<string>{2,-1}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{24,12},{96,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun8.png</key>
<dict>
<key>frame</key>
<string>{{814,0},{96,118}}</string>
<key>offset</key>
<string>{0,-1}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{22,12},{96,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
<key>RoleRun9.png</key>
<dict>
<key>frame</key>
<string>{{910,0},{100,118}}</string>
<key>offset</key>
<string>{-2,-2}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{18,13},{100,118}}</string>
<key>sourceSize</key>
<string>{140,140}</string>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>realTextureFileName</key>
<string>RoleRun.png</string>
<key>size</key>
<string>{2048,128}</string>
<key>smartupdate</key>
<string>$TexturePacker:SmartUpdate:43e6d77d8691aadfa1c598803e171096$</string>
<key>textureFileName</key>
<string>RoleRun.png</string>
</dict>
</dict>
</plist>
2、RoleRun.png
版权声明:本文为博主原创文章,未经博主允许不得转载。