本文整理汇总了C++中SpriteFrameCache类的典型用法代码示例。如果您正苦于以下问题:C++ SpriteFrameCache类的具体用法?C++ SpriteFrameCache怎么用?C++ SpriteFrameCache使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SpriteFrameCache类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: InitAnimation
//INITS
void GameScreen::InitAnimation()
{
SpriteBatchNode* spritebatch = SpriteBatchNode::create("Assets/Animation/Idle.png");
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("Assets/Animation/Idle.plist");
_santaPaused = Sprite::createWithSpriteFrameName("Assets/Santa/idle_0001.png");
spritebatch->addChild(_santaPaused);
addChild(spritebatch);
_santaPaused->setPosition(Vec2(-_winSizeW*0.5f, _winSizeH*0.7f));
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);
_santaPaused->runAction(RepeatForever::create(Animate::create(animation)));
}
示例2: init
bool HeroPlane::init(){
Size visibleSize = Director::getInstance()->getVisibleSize();
Point origin = Director::getInstance()->getVisibleOrigin();
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("heroplane.plist", "heroplane.png");//加载全局资源
plane = Sprite::createWithSpriteFrameName("plane1.png");//生成飞机
plane->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 5));
this->addChild(plane, 0, 1);
Blink *blink = Blink::create(3,8);//闪烁动画
Animation* animation = Animation::create();
animation->setDelayPerUnit(0.1f);
animation->addSpriteFrame(frameCache->getSpriteFrameByName("plane1.png"));
animation->addSpriteFrame(frameCache->getSpriteFrameByName("plane2.png"));
Animate* animate = Animate::create(animation);//帧动画
plane->runAction(blink);//执行闪烁动画
plane->runAction(RepeatForever::create(animate));// 执行帧动画
//开启触摸事件,让飞机跟随手指移动
auto listen = EventListenerTouchOneByOne::create();
listen->onTouchBegan = CC_CALLBACK_2(HeroPlane::onTouchBegan, this);
listen->onTouchMoved = CC_CALLBACK_2(HeroPlane::onTouchMoved, this);
listen->onTouchEnded = CC_CALLBACK_2(HeroPlane::onTouchEened, this);
listen->onTouchCancelled = CC_CALLBACK_2(HeroPlane::onTouchCancelled, this);
listen->setSwallowTouches(false);
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listen, this);
return true;
}
示例3: runAnimation
void XHelper::runAnimation(string name, int count, float time, bool isRepeat, Sprite* _sprite)
{
if (_sprite != nullptr)
{
_sprite->getActionManager()->removeAllActionsFromTarget(_sprite);
//CCLOG(&name[0]);
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(name + ".plist");
//Chuyển thành sprite frame
Animation* animation = Animation::create();
animation->setDelayPerUnit(time);
char frameName[100];
for (int i = 1; i <= count; i++)
{
sprintf(frameName, "%s%d.png", &name[0], i);
//CCLOG("framename : %s", frameName);
auto frame = cache->getSpriteFrameByName(frameName);
animation->addSpriteFrame(frame);
}
Action* action = nullptr;
if (isRepeat)
action = RepeatForever::create(Animate::create(animation));
else action = Animate::create(animation);
_sprite->runAction(action);
}
else{
log("XHelper::runAnimation : _sprite iss null");
}
}
示例4: RefreshShopList
void ShopLayer::RefreshShopList(Ref*pSender)
{
if (shadow->isVisible())
{
shadow->setVisible(false);
}
int _count = UserProfile::instance()->GetShopItemCount();
rongqi = Widget::create();
SpriteFrameCache * cache = SpriteFrameCache::getInstance();
for (auto i = 0; i < SHOPITEMNUM; i++)
{
CAKE = CSLoader::createNode("shop_info.csb");
CAKE->setPosition(Vec2(i % 3* 230 + 75, (-i/3*180) + 262));
CAKE->setTag(i);
auto pro_id_btn = dynamic_cast<Button*>(CAKE->getChildByName("shop_id_01_node")->getChildByTag(1));
pro_id_btn->setTag(i+1);
goods_name = dynamic_cast<Text*>(CAKE->getChildByName("shop_id_01_node")->getChildByName("Text"));
goods_name->setTag(i);
goods_price = dynamic_cast<Text*>(CAKE->getChildByName("shop_id_01_node")->getChildByName("coin_num_text"));
goods_price->setTag(i);
goods_level = dynamic_cast<Text*>(CAKE->getChildByName("shop_id_01_node")->getChildByName("Cur_lev"));
goods_level->setTag(i);
String * str_shop_id = String::createWithFormat("shop_id_%02d", i + 1);
int cur_level = UserProfile::instance()->GetShopItemLevel(i);
pro_id_btn->addClickEventListener(CC_CALLBACK_1(ShopLayer::BuyProp, this));
goods_isheigh = dynamic_cast<Text*>(CAKE->getChildByName("shop_id_01_node")->getChildByName("Heigh_Lev"));
goods_isheigh->setVisible(false);
int test = atoi(goods_level->getString().c_str());
switch (i)
{
case 0:
case 2:
case 3:
if (cur_level == 2)
{
goods_isheigh->setVisible(true);
pro_id_btn->setEnabled(false);
cur_level = 1;
}
break;
default:
if (cur_level == 3)
{
goods_isheigh->setVisible(true);
pro_id_btn->setEnabled(false);
cur_level = 2;
}
break;
}
goods_name->setString(Json::getInstance()->getJson("ITEM", i, str_shop_id->getCString(), "ITEM_NAME", cur_level));
goods_price->setString(Json::getInstance()->getJson("ITEM", i, str_shop_id->getCString(), "ITEM_SOLD_PRICE", cur_level));
goods_level->setString(Json::getInstance()->getJson("ITEM", i, str_shop_id->getCString(), "ITEM_LEVEL", cur_level));
std::string sp_name = Json::getInstance()->getJson("ITEM", i, str_shop_id->getCString(), "ITEM_IMAGE", cur_level);
sp_image = dynamic_cast<Sprite*>(CAKE->getChildByName("shop_id_01_node")->getChildByName("sp_image"));
sp_image->setDisplayFrame(cache->getSpriteFrameByName(sp_name));
rongqi->addChild(CAKE);
}
item_layout->addChild(rongqi);
}
示例5: makeAnim
Sprite* CTextRich::makeAnim( Sprite* pTarget,RenderElement elem )
{
SpriteFrameCache *pCache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*> v;
SpriteFrame *pFrame = NULL;
int index = 1;
char buf[128] = {0};
do
{
sprintf(buf,"%s%d.png",elem.anim.c_str(),index++);
pFrame = pCache->getSpriteFrameByName(buf);
if(pFrame == NULL){
break;
}
v.pushBack(pFrame);
} while (true);
Animation *pAnim = Animation::createWithSpriteFrames(v);
pAnim->setLoops(elem.isLoop?-1:0);
pAnim->setRestoreOriginalFrame(true);
pAnim->setDelayPerUnit(elem.delay);
pTarget->setContentSize(v.at(0)->getOriginalSize());
pTarget->stopAllActions();
pTarget->runAction(Animate::create(pAnim));
pTarget->setUserData( elem.data != "" ? new std::string(elem.data) : nullptr );
return pTarget;
}
示例6: init
bool HelloWorld::init()
{
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
auto background = Sprite::create("background.png");//加载背景精灵
background->setAnchorPoint(Vec2::ZERO);
this->addChild(background,0);
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();//单例对象
frameCache->addSpriteFramesWithFile("SpriteSheet.plist");//加载精灵图集
auto mountain1 = Sprite::createWithSpriteFrameName("mountain1.png");//通过精灵帧名创建精灵
mountain1->setAnchorPoint(Vec2::ZERO);//设置锚点
mountain1->setPosition(Vec2(-200,80));
this->addChild(mountain1,0);
SpriteFrame *heroSpriteFrame = frameCache->getSpriteFrameByName("hero1.png");//通过精灵帧名字获取精灵帧
Sprite *hero1 = Sprite::createWithSpriteFrame(heroSpriteFrame);//通过精灵帧创建精灵
//上面的两条语句相当于前面一条语句的效果auto mountain1 = Sprite::createWithSpriteFrameName("mountain1.png");
hero1->setPosition(Vec2(800,200));
this->addChild(hero1,0);
return true;
}
示例7: CCLOG
bool WalkerLayer::init()
{
if (!Layer::init()) {
CCLOG("Error in init WalkerLayer!");
return false;
}
float screenWidth = Director::getInstance()->getVisibleSize().width;
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile(WALKER_PLIST_PATH, WALKER_TEXTURE_PATH);
char frameName[30];
Sprite *walker = Sprite::create();
walker->setAnchorPoint(Vec2(0.5f, 0.0f));
walker->setPosition(Vec2(screenWidth * 0.5, _vPos));
addChild(walker, 0, "walker");
Animation *walkAnimation = Animation::create();
for (int i = 1; i <= _frameCount; ++i) {
sprintf_s(frameName, 20, "%s%d.png", _name.c_str(), i);
walkAnimation->addSpriteFrame(cache->getSpriteFrameByName(frameName));
}
for (int i = _frameCount; i > 1; --i) {
sprintf_s(frameName, 20, "%s%d.png", _name.c_str(), i);
walkAnimation->addSpriteFrame(cache->getSpriteFrameByName(frameName));
}
walkAnimation->setLoops(-1);
walkAnimation->setRestoreOriginalFrame(false);
walkAnimation->setDelayPerUnit(0.2f);
walker->runAction(RepeatForever::create(Animate::create(walkAnimation)));
return true;
}
示例8: addCharacter
KDvoid SampleLayer::addCharacter ( KDvoid )
{
// plist 에서 달리기 애니매이션 로딩
SpriteFrameCache* pFrameCache = SpriteFrameCache::getInstance ( );
pFrameCache->addSpriteFramesWithFile ( "ch01.plist" );
Dictionary* pDictionary = Dictionary::createWithContentsOfFile ( "ch01_1_aniinfo.plist" );
Array* pAnimationList = (Array*) pDictionary->objectForKey ( "animationlist" );
Array* pFrameList = (Array*) pDictionary->objectForKey ( "framelist" );
String* pName = (String*) pDictionary->objectForKey ( "name" );
String* pTexture = (String*) pDictionary->objectForKey ( "texture" );
String* pType = (String*) pDictionary->objectForKey ( "type" );
Dictionary* pAnimationItem = (Dictionary*) pAnimationList->getObjectAtIndex ( 12 );
Array* pTemp = (Array*) pAnimationItem->objectForKey ( "FrameList" );
KDfloat fTemp = ( (String*) pAnimationItem->objectForKey ( "FPS" ) )->floatValue ( );
Array* pArraySpriteFrame = Array::createWithCapacity ( pTemp->count ( ) );
Animation* pAnimation = Animation::create ( );
for ( KDint32 i = 0; i < pTemp->count ( ); i++ )
{
KDint ii = ( (String*) pTemp->getObjectAtIndex ( i ) )->intValue ( );
String* pString = (String*) pFrameList->getObjectAtIndex ( ii );
pAnimation->addSpriteFrame ( pFrameCache->getSpriteFrameByName ( pString->getCString ( ) ) );
}
pAnimation->setDelayPerUnit ( fTemp );
Sprite* pSprite = Sprite::createWithSpriteFrameName ( ( (String*) pFrameList->getObjectAtIndex ( 21 ) )->getCString ( ) );
Point tPoint = Point ( Point ( 100, 200 ) );
Point tOffset = pSprite->getDisplayFrame ( )->getOffset ( );
pSprite->setPosition ( tPoint - tOffset );
pSprite->runAction ( RepeatForever::create ( Animate::create ( pAnimation ) ) );
pSprite->setTag ( 10 );
this->addChild ( pSprite );
// Box2D body setting
b2BodyDef tBodyDef;
tBodyDef.type = b2_dynamicBody;
tBodyDef.position.Set ( tPoint.x / PTM_RATIO, tPoint.y / PTM_RATIO );
tBodyDef.userData = pSprite;
b2PolygonShape tDynamicBox;
Size tSize = pSprite->getDisplayFrame ( )->getRect ( ).size;
tDynamicBox.SetAsBox ( tSize.width / 2 / PTM_RATIO, tSize.height / 2 / PTM_RATIO );
m_pBody = m_pWorld->CreateBody ( &tBodyDef );
b2FixtureDef tFixtureDef;
tFixtureDef.shape = &tDynamicBox;
tFixtureDef.density = 1.0f;
tFixtureDef.friction = 0.0f;
m_pBody->CreateFixture ( &tFixtureDef );
}
示例9: CC_BREAK_IF
bool StartLayer::init()
{
bool ret = false;
do {
CC_BREAK_IF(!Layer::init());
Size s = Director::getInstance()->getWinSize();
SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("ui/shoot_background.plist", "ui/shoot_background.png");
frameCache->addSpriteFramesWithFile("ui/shoot.plist");
Sprite *backGround = Sprite::createWithSpriteFrameName("background.png");
backGround->setPosition(Point(s.width * 0.5, s.height * 0.5));
addChild(backGround, -1, 0);
Sprite *title = Sprite::createWithSpriteFrameName("shoot_copyright.png");
title->setPosition(Point(s.width * 0.5, s.height * 2 / 3));
addChild(title, 0, 1);
Sprite *gameLoad = Sprite::createWithSpriteFrameName(LOADIMAGE[0].c_str());
gameLoad->setPosition(Point(s.width * 0.5, s.height * 0.5));
addChild(gameLoad, 0, 2);
Animation* animation = Animation::create();
animation->setDelayPerUnit(0.4f);
animation->addSpriteFrame(frameCache->getSpriteFrameByName(LOADIMAGE[0].c_str()));
animation->addSpriteFrame(frameCache->getSpriteFrameByName(LOADIMAGE[1].c_str()));
animation->addSpriteFrame(frameCache->getSpriteFrameByName(LOADIMAGE[2].c_str()));
animation->addSpriteFrame(frameCache->getSpriteFrameByName(LOADIMAGE[3].c_str()));
Animate* animate = Animate::create(animation);
gameLoad->runAction(RepeatForever::create(animate));
auto touchListener = EventListenerTouchOneByOne::create();
touchListener->setSwallowTouches(true);
touchListener->onTouchEnded = [](Touch *touch, Event *event) {
Scene *st = Scene::create();
BackgroundLayer *back = BackgroundLayer::create();
st->addChild(back);
GameLayer *gameLayer = GameLayer::create();
st->addChild(gameLayer);
Director::getInstance()->replaceScene(st);
return;
};
touchListener->onTouchMoved = [](Touch* touch, Event* event){
return;
};
touchListener->onTouchBegan = [](Touch *touch, Event *event) {
return true;
};
_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this);
ret = true;
} while (0);
return ret;
}
示例10: setSpriteFrame
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);
CCASSERT(spriteFrame, std::string("Invalid spriteFrameName :").append(spriteFrameName).c_str());
setSpriteFrame(spriteFrame);
}
示例11: setSpriteFrame
void Sprite::setSpriteFrame(const std::string &spriteFrameName)
{
SpriteFrameCache *cache = SpriteFrameCache::getInstance();
SpriteFrame *spriteFrame = cache->getSpriteFrameByName(spriteFrameName);
CCASSERT(spriteFrame, "Invalid spriteFrameName");
setSpriteFrame(spriteFrame);
}
示例12: CC_CALLBACK_1
bool PlayLayer::init()
{
if(!BaseLayer::init())
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
MenuItemFont *back = MenuItemFont::create("back", CC_CALLBACK_1(PlayLayer::back, this));
Menu *menu = Menu::create(back, NULL);
menu->setPosition(visibleSize.width*9/10, visibleSize.height*9/10);
this->addChild(menu);
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("UI.plist", "UI.png");
mJoystick = NULL;
mJoystick = new SneakyJoystick();
mJoystick->initWithRect(Rect::ZERO);
mJoystick->setAutoCenter(true);
mJoystick->setHasDeadzone(true);
mJoystick->setDeadRadius(10);
SneakyJoystickSkinnedBase* jstickSkin = new SneakyJoystickSkinnedBase();
jstickSkin->autorelease();
jstickSkin->init();
jstickSkin->setBackgroundSprite(CCSprite::createWithSpriteFrameName("JoyStick-base.png"));
jstickSkin->setThumbSprite(CCSprite::createWithSpriteFrameName("JoyStick-thumb.png"));
//jstickSkin->getThumbSprite()->setScale(0.5f);
jstickSkin->setPosition(visibleSize.width*1/10, visibleSize.width*1/10);
jstickSkin->setJoystick(mJoystick);
this->addChild(jstickSkin);
mButtonA = NULL;
mButtonA = new SneakyButton();
mButtonA->initWithRect(Rect::ZERO);
mButtonA->setIsToggleable(false);
mButtonA->setIsHoldable(true);
SneakyButtonSkinnedBase* btnASkin = new SneakyButtonSkinnedBase();
btnASkin->autorelease();
btnASkin->init();
btnASkin->setPosition(visibleSize.width*9/10, visibleSize.width*1/10);
btnASkin->setDefaultSprite(CCSprite::createWithSpriteFrameName("button-default.png"));
btnASkin->setPressSprite(CCSprite::createWithSpriteFrameName("button-pressed.png"));
btnASkin->setActivatedSprite(CCSprite::createWithSpriteFrameName("button-activated.png"));
//btnASkin->setDisabledSprite(CCSprite::createWithSpriteFrameName("button-disabled.png"));
btnASkin->setButton(mButtonA);
this->addChild(btnASkin);
this->schedule(schedule_selector(PlayLayer::inputUpdate));
startPlay();
return true;
}
示例13:
Vector<SpriteFrame*>* Face::createFrameVector(const std::string& patternName, int startFrame, int endFrame) {
SpriteFrameCache *spriteFrameCache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*>*framesVector = new Vector<SpriteFrame*>;
char frameName[128];
for(int i = startFrame; i <= endFrame; i++){
sprintf(frameName, patternName.c_str(), i);
framesVector->pushBack(spriteFrameCache->getSpriteFrameByName(frameName));
}
return framesVector;
}
示例14: createTankWithTankType
Tank* Tank::createTankWithTankType(const char* tankTypeName, TileMapInfo* tileMapInfo)
{
SpriteFrameCache* pCache = SpriteFrameCache::getInstance();
pCache->addSpriteFramesWithFile("tank.plist");
Tank* tank = new Tank();
tank->initTankWithTankType(tankTypeName, tileMapInfo);
tank->autorelease();
return tank;
}
示例15: addPipe
void Pipe::addPipe(float dt)
{
SpriteFrameCache *pFrameCache = SpriteFrameCache::getInstance();
auto pipe_up = Sprite::createWithSpriteFrame(pFrameCache->getSpriteFrameByName("pipe_up.png"));
pipe_up->setPosition(Point(pipe_up->getContentSize().width/2,pipe_up->getContentSize().height/2));
auto body_up=PhysicsBody::create();
auto body_shape_up=PhysicsShapeBox::create(pipe_up->getContentSize());
body_up->addShape(body_shape_up);
body_up->setDynamic(false);
body_up->setGravityEnable(false);
body_up->setCategoryBitmask(1);
body_up->setCollisionBitmask(-1);
body_up->setContactTestBitmask(-1);
pipe_up->setPhysicsBody(body_up);
//向下管道初始化,这边的THROUGH_HEIGHT是两根管道之间的空隙
auto pipe_down = Sprite::createWithSpriteFrame(pFrameCache->getSpriteFrameByName("pipe_down.png"));
pipe_down->setPosition(Point(pipe_down->getContentSize().width/2,pipe_down->getContentSize().height/2+pipe_up->getContentSize().height+THROUGH_HEIGHT));
auto body_down=PhysicsBody::create();
auto body_shape_down=PhysicsShapeBox::create(pipe_down->getContentSize());
body_down->addShape(body_shape_down);
body_down->setDynamic(false);
body_down->setGravityEnable(false);
body_down->setCategoryBitmask(1);
body_down->setCollisionBitmask(-1);
body_down->setContactTestBitmask(-1);
pipe_down->setPhysicsBody(body_down);
//这边的node相当于一个容器把这两个管道封装在一个节点中并设置target
auto node=Node::create();
node->addChild(pipe_up,0,PIPE_UP);
node->addChild(pipe_down,0,PIPE_DOWN);
node->setAnchorPoint(Point::ANCHOR_BOTTOM_LEFT);
//关于管道Y坐标的设置(就是管道上下长度不一样的处理),大家还是看图例,说不清楚
//管道是从右边移动到左边,所以PIPE_X的值肯定比游戏的width要大这里设定是300
int range=rand()%PIPE_RANGE;
node->setPosition(Point(PIPE_X, PIPE_Y + range));
ActionManager *pActionManager = Director::getInstance()->getActionManager();
MoveBy *pMoveBy = MoveBy::create(3.2f, Point(-400, 0));
CallFuncN *pMoveOverCallback = CallFuncN::create(CC_CALLBACK_1(Pipe::moveOverHandle, this));
ActionInterval *pSeq = Sequence::create(pMoveBy, pMoveOverCallback, nullptr);
pActionManager->addAction(pSeq, node, false);
this->addChild(node);
pPipeVector.pushBack(node);
if (isStart) {
scheduleUpdate();
isStart = false;
}
}