(基于cocos2dx 2.2.2)
如果很多图片很小或常用,可以使用CCSpriteFrameCache,来避免资源被反复加载。
(注:对于plist,我这里使用的是一个免费的工具,SpritePacker,http://spritepacker.kernys.net)
如:CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("basetank.plist");
<?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>frames</key>
<dict>
<key>basetank_down.png</key>
<dict>
<key>frame</key>
<string>{{0,0},{26,26}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{3,3},{26,26}}</string>
<key>sourceSize</key>
<string>{32,32}</string>
</dict>
<key>basetank_left.png</key>
<dict>
<key>frame</key>
<string>{{0,27},{26,26}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{3,3},{26,26}}</string>
<key>sourceSize</key>
<string>{32,32}</string>
</dict>
<key>basetank_right.png</key>
<dict>
<key>frame</key>
<string>{{0,54},{26,26}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{3,3},{26,26}}</string>
<key>sourceSize</key>
<string>{32,32}</string>
</dict>
<key>basetank_up.png</key>
<dict>
<key>frame</key>
<string>{{0,81},{26,26}}</string>
<key>offset</key>
<string>{0,0}</string>
<key>rotated</key>
<false/>
<key>sourceColorRect</key>
<string>{{3,3},{26,26}}</string>
<key>sourceSize</key>
<string>{32,32}</string>
</dict>
</dict>
<key>metadata</key>
<dict>
<key>format</key>
<integer>2</integer>
<key>realTextureFileName</key>
<string>basetank.png</string>
<key>size</key>
<string>{32,128}</string>
<key>smartupdate</key>
<string>{None}</string>
<key>textureFileName</key>
<string>basetank.png</string>
</dict>
</dict>
</plist>
这样,就可以完成一组图片资源加载。
CCSprite * p1Pic = CCSprite::createWithSpriteFrameName(“basetank_up.png”);
p1Pic->setPosition(GetPosition(pPlayerLayer, "Player1"));
p1Pic->setAnchorPoint(ccp(0, 0));
this->addChild(p1Pic);
如需更换图片,
p1Pic
->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("basetank_right.png"));
这里的作用相当于是把tank向右转