直接上代码
Size size=Director::getInstance()->getWinSize();
//常见的一种形式
Sprite* sp=Sprite::create("HelloWorld.png");
sp->setposition(size.wIDth/2,size.height/2);
//截图的形式
Rect r=Rect(0,160,120);
auto sp1=Sprite::create("Blowfish0.png",r);
sp1->setposition(size.wIDth/2,size.height/2);
//纹理的方式创建
Texture2D* text=Director::getInstance()->getTextureCache()->addImage("Cn16.png");
auto sp2=Sprite::createWithTexture(text);
sp2->setposition(size.wIDth/2+100,size.height/2);
//纹理和截图结合使用
Texture2D* text2=Director::getInstance()->getTextureCache()->addImage("Blowfish0.png");
auto sp3=Sprite::createWithTexture(text2,r,false);
sp3->setposition(size.wIDth/2+100,size.height/2-50);
//利用.pList文件创建并且用到了帧缓存
SpriteFrameCache::getInstance()->addSpriteFramesWithfile("Blowfish0.pList");
//"Fish08/normal/actor_fish_hetun02_normal_001.png" 是.pList下组成图的key,就像map的键值模式一样 这里取的是值
auto frame=SpriteFrameCache::getInstance()->getSpriteFrameByname("Fish08/normal/actor_fish_hetun02_normal_001.png");
auto sp4=Sprite::createWithSpriteFrame(frame);
sp4->setposition(size.wIDth/2+100,size.height/2-100);
// 利用.pList文件创建,直接使用.pList中组成图的key的值
SpriteFrameCache::getInstance()->addSpriteFramesWithfile("Blowfish0.pList");
auto sp5=Sprite::createWithSpriteFramename("Fish08/normal/actor_fish_hetun02_normal_002.png");
sp5->setposition(size.wIDth/2+100,size.height/2-200);
资源文件自己寻找合适的添加进去就行了,贴上此.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>Fish08/normal/actor_fish_hetun02_normal_001.png</key>
<dict>
<key>height</key>
<integer>120</integer>
<key>offsetX</key>
<real>0.0</real>
<key>offsetY</key>
<real>0.0</real>
<key>originalHeight</key>
<integer>120</integer>
<key>originalWIDth</key>
<integer>160</integer>
<key>wIDth</key>
<integer>160</integer>
<key>x</key>
<integer>0</integer>
<key>y</key>
<integer>0</integer>
</dict>
<key>Fish08/normal/actor_fish_hetun02_normal_002.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>0</integer> <key>y</key> <integer>122</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_003.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>162</integer> <key>y</key> <integer>0</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_004.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>162</integer> <key>y</key> <integer>122</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_005.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>324</integer> <key>y</key> <integer>0</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_006.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>324</integer> <key>y</key> <integer>122</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_007.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>0</integer> <key>y</key> <integer>244</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_008.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>0</integer> <key>y</key> <integer>366</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_009.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>162</integer> <key>y</key> <integer>244</integer> </dict> <key>Fish08/normal/actor_fish_hetun02_normal_010.png</key> <dict> <key>height</key> <integer>120</integer> <key>offsetX</key> <real>0.0</real> <key>offsetY</key> <real>0.0</real> <key>originalHeight</key> <integer>120</integer> <key>originalWIDth</key> <integer>160</integer> <key>wIDth</key> <integer>160</integer> <key>x</key> <integer>162</integer> <key>y</key> <integer>366</integer> </dict> </dict> <key>Metadata</key> <dict> <key>format</key> <integer>0</integer> <key>realTexturefilename</key> <string>Blowfish0.png</string> <key>size</key> <string>{512,512}</string> <key>texturefilename</key> <string>Blowfish0.png</string> </dict> <key>texture</key> <dict> <key>height</key> <integer>512</integer> <key>wIDth</key> <integer>512</integer> </dict> </dict> </pList>
以上是内存溢出为你收集整理的cocos2dx-3.0以上版本之 创建Sprite精灵的6种形式全部内容,希望文章能够帮你解决cocos2dx-3.0以上版本之 创建Sprite精灵的6种形式所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)