Size visibleSize = Director::getInstance()->getVisibleSize();
//创建精灵的六种方法
//方法一
auto sprite1 = Sprite::create("Flag_1.png");
sprite1->setPosition(Vec2(visibleSize.width*0.2, visibleSize.height*0.7));
this->addChild(sprite1);
//方法二
auto sprite2 = Sprite::create("Flag_1.png", Rect(0, 0, 48, 36));
sprite2->setPosition(Vec2(visibleSize.width*0.4, visibleSize.height*0.7));
this->addChild(sprite2);
//创建2D纹理
auto texture1 = Director::getInstance()->getTextureCache()->addImage("Flag_1.png");
//auto texture1 = TextureCache::sharedTextureCache()->addImage("Flag_1.png"); //cocos2d-x 3.0已移除该方法
//方法三
auto sprite3 = Sprite::createWithTexture(texture1);
sprite3->setPosition(Vec2(visibleSize.width*0.6, visibleSize.height*0.7));
this->addChild(sprite3);
//方法四
auto sprite4 = Sprite::createWithTexture(texture1, Rect(0, 0, 48, 36));
sprite4->setPosition(Vec2(visibleSize.width*0.8, visibleSize.height*0.7));
this->addChild(sprite4);
//创建精灵帧
auto frame = SpriteFrame::create("Flag_1.png", Rect(0, 0, 48, 36));
//方法五
auto sprite5 = Sprite::createWithSpriteFrame(frame);
sprite5->setPosition(Vec2(visibleSize.width*0.3, visibleSize.height*0.3));
this->addChild(sprite5);
// //方法六
// SpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("img.plist");
//
// auto sprite6 = Sprite::createWithSpriteFrameName("HelloWorld.png");
// sprite6->setPosition(Vec2(visibleSize.width*0.7, visibleSize.height*0.3));
// this->addChild(sprite6);
Sprite精灵类的创建与设置
最新推荐文章于 2023-05-18 14:21:26 发布