码迷,mamicode.com
首页 > 系统相关 > 详细

cocos2d-x3.0 SpriteFrameCache

时间:2014-10-27 21:04:54      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:blog   http   io   color   os   ar   sp   div   on   


bool HelloWorld::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !Layer::init() )
    {
        return false;
    }
    auto s = Director::getInstance()->getWinSize();
    
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("grossini.plist");
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("grossini-generic.plist");

    auto layer1 = LayerColor::create(Color4B(255, 0, 0, 255), 85, 121);
    layer1->setPosition(Point(s.width/2-80 - (85.0f * 0.5f), s.height/2 - (121.0f * 0.5f)));
    addChild(layer1);
    
    
    sprite1 = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini_dance_01.png"));
    sprite1->setPosition(Point( s.width/2-80, s.height/2));
    addChild(sprite1);
    
    
    sprite1->setFlippedX(false);
    sprite1->setFlippedY(false);
 
    
    auto layer2 = LayerColor::create(Color4B(255, 0, 0, 255), 85, 121);
    layer2->setPosition(Point(s.width/2+80 - (85.0f * 0.5f), s.height/2 - (121.0f * 0.5f)));
    addChild(layer2);
    
    sprite2 = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("grossini_dance_generic_01.png"));
    sprite2->setPosition(Point( s.width/2 + 80, s.height/2));
    addChild(sprite2);
    
    sprite2->setFlippedX(false);
    sprite2->setFlippedY(false);

    
    
     schedule(schedule_selector(HelloWorld::startIn05Secs), 1.0f);
    
    sprite1->retain();
    sprite2->retain();
    
    counter = 0;

    
    
    
    return true;
}

void HelloWorld::startIn05Secs(float dt)
{
    unschedule(schedule_selector(HelloWorld::startIn05Secs));
    schedule(schedule_selector(HelloWorld::flipSprites), 0.5f);
}

static int spriteFrameIndex = 0;
void HelloWorld::flipSprites(float dt)
{
    counter++;
    
    bool fx = false;
    bool fy = false;
    int i = counter % 4;
    
    switch ( i )
    {
        case 0:
            fx = false;
            fy = false;
            break;
        case 1:
            fx = true;
            fy = false;
            break;
        case 2:
            fx = false;
            fy = true;
            break;
        case 3:
            fx = true;
            fy = true;
            break;
    }
    
    sprite1->setFlippedX(fx);
    sprite2->setFlippedX(fx);
    sprite1->setFlippedY(fy);
    sprite2->setFlippedY(fy);
    
    if(++spriteFrameIndex > 14)
    {
        spriteFrameIndex = 1;
    }
    
    char str1[32] = {0};
    char str2[32] = {0};
    sprintf(str1, "grossini_dance_%02d.png", spriteFrameIndex);
    sprintf(str2, "grossini_dance_generic_%02d.png", spriteFrameIndex);
    sprite1->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str1));
    sprite2->setSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(str2));
}


bubuko.com,布布扣


cocos2d-x3.0 SpriteFrameCache

标签:blog   http   io   color   os   ar   sp   div   on   

原文地址:http://www.cnblogs.com/hrhguanli/p/4054968.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!