本文整理汇总了C++中SpriteFrameCache::getSpriteFrameByName方法的典型用法代码示例。如果您正苦于以下问题:C++ SpriteFrameCache::getSpriteFrameByName方法的具体用法?C++ SpriteFrameCache::getSpriteFrameByName怎么用?C++ SpriteFrameCache::getSpriteFrameByName使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SpriteFrameCache
的用法示例。
在下文中一共展示了SpriteFrameCache::getSpriteFrameByName方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: textHandler
void textHandler(void *ctx, const char *ch, int len)
{
CC_UNUSED_PARAM(ctx);
const std::string text = std::string((char*)ch,len);
if (_key)
{
if (_keys.size() == 1 && _keys.front() == "frames")
{
SpriteFrame* frame = _cache->getSpriteFrameByName(text);
_frames.pushBack(frame);
}
_keys.push_back(text);
}
}
示例2: setLevel
void Bacterial::setLevel(int value)
{
if(level != value && value <= MAXLEVEL)
{
level = value;
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
if(level > 0)
{
inited = true;
SpriteFrame* frame = cache->getSpriteFrameByName(String::createWithFormat("res/%d%d.png", type, level)->getCString());
bg->setSpriteFrame(frame);
}
else
{
inited = false;
}
}
}
示例3:
cocos2d::Animation * Entity::createAnimation(const char *str, int num) {
//»ñÈ¡»º´æʵÀý
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
SpriteFrame *frame = nullptr;
Vector<SpriteFrame*> frameVec;
for (int i = 0; i < num; i++) {
frame = frameCache->getSpriteFrameByName(StringUtils::format("%s%d.png", str, i));
frameVec.pushBack(frame);
}
Animation *animation = Animation::createWithSpriteFrames(frameVec);
animation->setLoops(-1);
animation->setDelayPerUnit(0.2f);
return animation;
}
示例4: _getMesureSpriteContentSize
Size CTextRich::_getMesureSpriteContentSize(const std::string& path)
{
if (_mesureSprite == nullptr)
{
_mesureSprite = Sprite::create();
_mesureSprite->retain();
}
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
SpriteFrame *spriteFrame = cache->getSpriteFrameByName(path);
if (spriteFrame)
{
_mesureSprite->setSpriteFrame(spriteFrame);
return _mesureSprite->getContentSize();
}else{
CCLOG("miss SpriteFrame: ",path.c_str());
return Size();
}
}
示例5: createFrameCaheAction
Action* CSprite::createFrameCaheAction(const string& pattern, int count, float duration, bool loop) {
SpriteFrameCache* spriteFrameCache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*>* framesVector = new Vector<SpriteFrame*>;
char frameName[128];
for(int i = 1; i <= count; i++){
sprintf(frameName, pattern.c_str(), i);
SpriteFrame* frame = spriteFrameCache->getSpriteFrameByName(frameName);
framesVector->pushBack(frame);
}
float delay = duration / framesVector->size();
Animation* animation = Animation::createWithSpriteFrames(*framesVector, delay);
Animate* animate = Animate::create(animation);
Action* action = NULL;
if (loop) {
action = RepeatForever::create(animate);
} else {
action = (Action*) animate;
}
delete framesVector;
return action;
}
示例6: moving
RepeatForever* PeerJelly::moving()
{
// 3. repeat the frame
int numFrame = 3;
cocos2d::Vector<cocos2d::SpriteFrame *> frames;
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
char file[100] = {0};
for (int i = 0; i < numFrame; i++)
{
sprintf(file, "redJelly_%d.png", i+1);
SpriteFrame *frame = frameCache->getSpriteFrameByName(file);
frames.pushBack(frame);
}
Animation *animation = Animation::createWithSpriteFrames(frames, 0.3f);
Animate *animate = Animate::create(animation);
auto easeInAnimation = EaseIn::create(animate, 0.3f);
RepeatForever *repeat = RepeatForever::create(easeInAnimation);
return repeat;
}
示例7: explode
void Item::explode(){
//play the explode animation
explodeIndicator = 1;
if(haveExplode == 0){
haveExplode=1;
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("explode/explode.plist");
Vector<SpriteFrame*> animFrames(14);
char str[100]={0};
for(int i=1; i<=14; i++){
sprintf(str, "exp%d.png",i);
SpriteFrame* frame = cache->getSpriteFrameByName(str);
animFrames.insert(i-1, frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
Animate* act = Animate::create(animation);
Sequence* endAct=Sequence::create(act,CallFunc::create( std::bind(&Item::explodeEnd,this) ),NULL);
item->runAction(endAct);
}
}
示例8: animFrames
void gameLevel2::shipExplosions(Vec2 vec,bool scale) {
SpriteBatchNode* spritebatch = SpriteBatchNode::create("animations/explosion.png");
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("animations/explosion.plist");
auto explosion = Sprite::createWithSpriteFrameName("explosion_01.png");
explosion->setPosition(vec);
if (scale) explosion->setScale(0.5);
spritebatch->addChild(explosion);
this->addChild(spritebatch);
Vector<SpriteFrame*> animFrames(48);
char str[100] = { 0 };
for (int i = 1; i < 49; i++)
{
sprintf(str, "explosion_%02d.png", i);
SpriteFrame* frame = cache->getSpriteFrameByName(str);
animFrames.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.02);
explosion->runAction(Sequence::create(Animate::create(animation),RemoveSelf::create(),NULL));
}
示例9: sprintf
RepeatForever*Bird::moving()
{
// 3. repeat the frame
int numFrame = 3;
//ocos2d::Vector<cocos2d::SpriteFrame *> frames;
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
char file[100] = {0};
for (int i = 0; i < numFrame; i++) {
sprintf(file, "FlappyBird%d.png", i+1);
SpriteFrame *frame = frameCache->getSpriteFrameByName(file);
frames.pushBack(frame);
// frames->getPositionY.
}
Animation *animation = Animation::createWithSpriteFrames(frames, 0.1);
Animate *animate = Animate::create(animation);
RepeatForever *repeat = RepeatForever::create(animate);
return repeat;
}
示例10: getSkillEffectById
Animate* EffectUtil::getSkillEffectById(int id,int loop){
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(String::createWithFormat("Skill/Effect/%d.plist",id)->getCString(),
String::createWithFormat("Skill/Effect/%d.png",id)->getCString());
SpriteFrame* temp;
Vector<SpriteFrame*> v;
int index = 1;
do{
CCLOG("INDEX = %d",index);
temp = cache->getSpriteFrameByName(String::createWithFormat("%d_%d.png",id,index)->getCString());
index++;
if(temp == nullptr){
break;
}else{
v.pushBack(temp);
}
}while(true);
Animation* animation = Animation::createWithSpriteFrames(v);
animation->setLoops(loop);
animation->setDelayPerUnit(0.1f);
Animate* ret = Animate::create(animation);
return ret;
}
示例11: readKeyframe
CCBKeyframe* CCBReader::readKeyframe(PropertyType type)
{
CCBKeyframe *keyframe = new (std::nothrow) CCBKeyframe();
keyframe->autorelease();
keyframe->setTime(readFloat());
CCBKeyframe::EasingType easingType = static_cast<CCBKeyframe::EasingType>(readInt(false));
float easingOpt = 0;
Value value;
if (easingType == CCBKeyframe::EasingType::CUBIC_IN
|| easingType == CCBKeyframe::EasingType::CUBIC_OUT
|| easingType == CCBKeyframe::EasingType::CUBIC_INOUT
|| easingType == CCBKeyframe::EasingType::ELASTIC_IN
|| easingType == CCBKeyframe::EasingType::ELASTIC_OUT
|| easingType == CCBKeyframe::EasingType::ELASTIC_INOUT)
{
easingOpt = readFloat();
}
keyframe->setEasingType(easingType);
keyframe->setEasingOpt(easingOpt);
if (type == PropertyType::CHECK)
{
value = readBool();
}
else if (type == PropertyType::BYTE)
{
value = readByte();
}
else if (type == PropertyType::COLOR3)
{
unsigned char r = readByte();
unsigned char g = readByte();
unsigned char b = readByte();
ValueMap colorMap;
colorMap["r"] = r;
colorMap["g"] = g;
colorMap["b"] = b;
value = colorMap;
}
else if (type == PropertyType::DEGREES)
{
value = readFloat();
}
else if (type == PropertyType::SCALE_LOCK || type == PropertyType::POSITION
|| type == PropertyType::FLOAT_XY)
{
float a = readFloat();
float b = readFloat();
ValueVector ab;
ab.push_back(Value(a));
ab.push_back(Value(b));
value = ab;
}
else if (type == PropertyType::SPRITEFRAME)
{
std::string spriteSheet = readCachedString();
std::string spriteFile = readCachedString();
SpriteFrame* spriteFrame;
if (spriteSheet.empty())
{
spriteFile = _CCBRootPath + spriteFile;
Texture2D *texture = Director::DirectorInstance->getTextureCache()->addImage(spriteFile);
Rect bounds = Rect(0, 0, texture->getContentSize().width, texture->getContentSize().height);
spriteFrame = SpriteFrame::createWithTexture(texture, bounds);
}
else
{
spriteSheet = _CCBRootPath + spriteSheet;
SpriteFrameCache* frameCache = SpriteFrameCache::getInstance();
// Load the sprite sheet only if it is not loaded
if (_loadedSpriteSheets.find(spriteSheet) == _loadedSpriteSheets.end())
{
frameCache->addSpriteFramesWithFile(spriteSheet);
_loadedSpriteSheets.insert(spriteSheet);
}
spriteFrame = frameCache->getSpriteFrameByName(spriteFile);
}
keyframe->setObject(spriteFrame);
}
if (!value.isNull())
keyframe->setValue(value);
return keyframe;
}
示例12: init
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
this->isGameOver = false;
visibleSize = Director::getInstance()->getVisibleSize();
// Ground setup
groundSprite0 = Sprite::create("ground.png");
this->addChild(groundSprite0);
groundSprite0->setPosition(Vec2(groundSprite0->getContentSize().width/2.0 , groundSprite0->getContentSize().height/2));
groundSprite1 = Sprite::create("ground.png");
this->addChild(groundSprite1);
groundSprite1->setPosition(Vec2(visibleSize.width + groundSprite1->getContentSize().width/2.0 -10, groundSprite1->getContentSize().height/2));
auto groundbody0 = PhysicsBody::createBox(groundSprite0->getContentSize());
groundbody0->setDynamic(false);
groundbody0->setContactTestBitmask(true);
groundSprite0->setPhysicsBody(groundbody0);
auto groundbody1 = PhysicsBody::createBox(groundSprite1->getContentSize());
groundbody1->setDynamic(false);
groundbody1->setContactTestBitmask(true);
groundSprite1->setPhysicsBody(groundbody1);
// SkyGround setup
Sprite *skySprite0 = Sprite::create("flappy_background.png");
Sprite *skySprite1 = Sprite::create("flappy_background.png");
Sprite *skySprite2 = Sprite::create("flappy_background.png");
this->addChild(skySprite0);
this->addChild(skySprite1);
this->addChild(skySprite2);
skySprite0->setPosition(visibleSize.width/2, 168 + 200);
skySprite1->setPosition(visibleSize.width/2 - skySprite1->getContentSize().width, 168 + 200);
skySprite2->setPosition(visibleSize.width/2 + skySprite1->getContentSize().width, 168 + 200);
// bird setup
/*
Sprite *birdSprite = Sprite::create("flappybird1.png");
this->addChild(birdSprite);
birdSprite->setPosition(visibleSize.width/2, visibleSize.height/2 + 120);
*/
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("bird.plist");
auto flyAnim = Animation::create();
for (int i = 1; i < 4; i++) {
SpriteFrame * frame = cache->getSpriteFrameByName("flappybird" + to_string(i) + ".png");
flyAnim->addSpriteFrame(frame);
}
auto birdSprite = Sprite::createWithSpriteFrameName("flappybird1.png");
flyAnim->setDelayPerUnit(0.2f);
auto action = Animate::create(flyAnim);
auto animation = RepeatForever::create(action);
birdSprite->runAction(animation);
birdSprite->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2 + 80));
this->addChild(birdSprite);
auto birdBody = PhysicsBody::createCircle(17.0);
birdBody->setDynamic(true);
birdBody->setMass(1.0f);
birdBody->setVelocity(Vec2(4.0f, 2.0f));
birdBody->setVelocityLimit(50);
birdBody->setContactTestBitmask(true);
birdSprite->setPhysicsBody(birdBody);
//pipe setup
topPipeSprite = Sprite::create("top_pipe.png");
bottomPipeSprite = Sprite::create("bottom_pipe.png");
topPipeSprite->setPosition(visibleSize.width + topPipeSprite->getContentSize().width/2, 600);
auto pipebody0 = PhysicsBody::createBox(topPipeSprite->getContentSize());
pipebody0->setDynamic(false);
topPipeSprite->setPhysicsBody(pipebody0);
pipebody0->setContactTestBitmask(true);
auto pipebody1 = PhysicsBody::createBox(bottomPipeSprite->getContentSize());
pipebody1->setDynamic(false);
pipebody1->setContactTestBitmask(true);
bottomPipeSprite->setPhysicsBody(pipebody1);
this->positionBottomPipe();
this->addChild(topPipeSprite);
this->addChild(bottomPipeSprite);
//setup touch listener
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = [=](Touch *touch, Event *event){
if (!this->isGameOver) {
birdBody->applyImpulse(Vec2(0, 90.0f));
}
log("touch detected!");
//.........这里部分代码省略.........
示例13: flashRender
void Guns::flashRender() {
const float FLASH_SPEED = 0.08f;
const float DELAY_ANIMATION = 0.2f;
const char *FLASH_NAME = "flash";
const int LOCAL_Z_ORDER_FLASH = 15;
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*> animFrames(2);
char str[100] = { 0 };
for (int i = 0; i <= 1; i++)
{
sprintf(str, "gunflash%d.png", i);
SpriteFrame* frame = cache->getSpriteFrameByName(str);
animFrames.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, DELAY_ANIMATION);
*flash = Sprite::createWithSpriteFrameName("gunflash0.png");
map->addChild(*flash, LOCAL_Z_ORDER_FLASH, FLASH_NAME);
(*flash)->setPosition(Vec2(character->getPosition().x - gun->getContentSize().width - 5, character->getPosition().y));
switch (currentKey) {
case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
case EventKeyboard::KeyCode::KEY_A:
(*flash)->setPosition(Vec2(character->getPosition().x - gun->getContentSize().width - 5, character->getPosition().y));
break;
case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
case EventKeyboard::KeyCode::KEY_D:
(*flash)->setPosition(Vec2(character->getPosition().x + gun->getContentSize().width + 5, character->getPosition().y));
break;
case EventKeyboard::KeyCode::KEY_UP_ARROW:
case EventKeyboard::KeyCode::KEY_W:
(*flash)->setPosition(Vec2(character->getPosition().x - gun->getContentSize().width - 5, character->getPosition().y));
break;
case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
case EventKeyboard::KeyCode::KEY_S:
(*flash)->setPosition(Vec2(character->getPosition().x + gun->getContentSize().width + 5, character->getPosition().y));
break;
}
(*flash)->runAction(Animate::create(animation));
// Remove flash
auto delay = DelayTime::create(FLASH_SPEED);
auto callback = CallFunc::create([=]() {
map->removeChildByName(FLASH_NAME);
});
auto sequence = Sequence::createWithTwoActions(delay, callback);
this->runAction(sequence);
}
示例14: init
bool StartScreen::init()
{
if (!Layer::init())
{
return false;
}
auto rootNode = CSLoader::createNode("Screens/StartScreen.csb");
addChild(rootNode);
this->scheduleUpdate();
auto winSize = Director::getInstance()->getVisibleSize();
//Animated Santa Sprite
SpriteBatchNode* spritebatch = SpriteBatchNode::create("Assets/Animation/Idle.png");
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("Assets/Animation/Idle.plist");
_santa = Sprite::createWithSpriteFrameName("Assets/Santa/idle_0001.png");
spritebatch->addChild(_santa);
addChild(spritebatch);
_santa->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.7f));
_background = (Sprite*)rootNode->getChildByName("Background");
_background->setPosition(Vec2(winSize.width * 0.5, winSize.height * 0.5));
Vector<SpriteFrame*> animFrames;
char str[100] = { 0 };
for (int i = 1; i <= 12; i++)
{
sprintf(str, "Assets/Santa/idle_%04d.png", i);
SpriteFrame* frame = cache->getSpriteFrameByName(str);
animFrames.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(animFrames, 0.1f);
_santa->runAction(RepeatForever::create(Animate::create(animation)));
//TOUCHES
//Set up a touch listener.
auto touchListener = EventListenerTouchOneByOne::create();
//Set callbacks for our touch functions.
touchListener->onTouchBegan = CC_CALLBACK_2(StartScreen::onTouchBegan, this);
touchListener->onTouchEnded = CC_CALLBACK_2(StartScreen::onTouchEnded, this);
touchListener->onTouchMoved = CC_CALLBACK_2(StartScreen::onTouchMoved, this);
touchListener->onTouchCancelled = CC_CALLBACK_2(StartScreen::onTouchCancelled, this);
//Add our touch listener to event listener list.
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
//BUTTONS
//Start button
_btnStart = static_cast<ui::Button*>(rootNode->getChildByName("btnStart"));
_btnStart->addTouchEventListener(CC_CALLBACK_2(StartScreen::StartButtonPressed, this));
_btnStart->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.43f));
//Highscore button.
_btnHighscore = static_cast<ui::Button*>(rootNode->getChildByName("btnHighscore"));
_btnHighscore->addTouchEventListener(CC_CALLBACK_2(StartScreen::HighscoreButtonPressed, this));
_btnHighscore->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.32f));
//Options button
_btnOptions = static_cast<ui::Button*>(rootNode->getChildByName("btnOptions"));
_btnOptions->addTouchEventListener(CC_CALLBACK_2(StartScreen::OptionsButtonPressed, this));
_btnOptions->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.21f));
//Exit button
_btnExit = static_cast<ui::Button*>(rootNode->getChildByName("btnExit"));
_btnExit->addTouchEventListener(CC_CALLBACK_2(StartScreen::ExitButtonPressed, this));
_btnExit->setPosition(Vec2(winSize.width*0.5f, winSize.height*0.1f));
return true;
}
示例15: setGameCache
void SetGameCacheController::setGameCache(){
AnimationCache* animationCache = AnimationCache::getInstance();
SpriteFrameCache * cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(Constant::getEnemyGoblinPath());
cache->addSpriteFramesWithFile(Constant::getEnemySoldierPath());
cache->addSpriteFramesWithFile(Constant::getEnemyArrowEnemyPath());
cache->addSpriteFramesWithFile(Constant::getEnemySheepPath());
cache->addSpriteFramesWithFile(Constant::getEnemyBossAttackPath());
cache->addSpriteFramesWithFile(Constant::getEnemyBossPath());
//Goblin Move
Vector <SpriteFrame*> temp;
char name[20];
memset(name, 0, sizeof(name));
for (int i = 0; i < 5; i++){
sprintf(name, "Goblin_%d.png", i + 1);
SpriteFrame* sf = cache->spriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemyGoblinMove());
//Sheep Move
temp.clear();
memset(name, 0, sizeof(name));
for (int i = 4; i < 8; i++){
sprintf(name, "Sheep_%d.png", i);
SpriteFrame* sf = cache->spriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemySheepMove());
//Soldier Move
temp.clear();
memset(name, 0, sizeof(name));
for (int i = 0; i < 8; i++){
sprintf(name, "Soldier_%d.png", i + 1);
SpriteFrame* sf = cache->spriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.1f), Constant::getEnemySoldierMove());
//ArrowEnemy Attack
temp.clear();
for (int i = 9; i < 16; i++){
sprintf(name, "ArrowEnemy_%d.png", i + 1);
SpriteFrame* sf = cache->spriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.2f), Constant::getEnemyArrowEnemyAttack());
//Goblin Death
temp.clear();
for (int i = 10; i < 14; i++){
sprintf(name, "Goblin_%d.png", i + 1);
SpriteFrame* sf = cache->getSpriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemyGoblinDeath());
//Sheep Death
temp.clear();
for (int i = 13; i < 22; i++){
sprintf(name, "Sheep_%d.png", i);
SpriteFrame* sf = cache->getSpriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemySheepDeath());
//Soldier Death
temp.clear();
for (int i = 17; i < 23; i++){
sprintf(name, "Soldier_%d.png", i + 1);
SpriteFrame* sf = cache->getSpriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemySoldierDeath());
//ArrowEnemy Death
temp.clear();
for (int i = 17; i < 21; i++){
sprintf(name, "ArrowEnemy_%d.png", i + 1);
SpriteFrame* sf = cache->getSpriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.5f), Constant::getEnemyArrowEnemyDeath());
//Boss Normal
temp.clear();
for (int i = 17; i < 23; i++){
sprintf(name, "Boss_%d.png", i);
SpriteFrame* sf = cache->getSpriteFrameByName(name);
temp.pushBack(sf);
}
animationCache->addAnimation(Animation::createWithSpriteFrames(temp, 0.2f), Constant::getEnemyBossNormal());
//Boss Attack
//.........这里部分代码省略.........