码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d 动作切换

时间:2014-09-25 11:21:58      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   for   div   sp   

#include "HelloWorldScene.h"

USING_NS_CC;

CCScene* HelloWorld::scene()
{
    CCScene *scene = CCScene::create();
    HelloWorld *layer = HelloWorld::create();
    scene->addChild(layer);
    return scene;
}

bool HelloWorld::init()
{

    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize winSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("hero.plist");

    tank=    CCSprite::createWithSpriteFrameName("hero_1.png");
    tank->setPosition(ccp(winSize.width/2,winSize.height/2));
    addChild(tank);

    initCreep();
    this->scheduleOnce(schedule_selector(HelloWorld::start), 3.0f); 
    this->scheduleOnce(schedule_selector(HelloWorld::stop), 6.0f); 
    this->scheduleOnce(schedule_selector(HelloWorld::restart), 9.0f); 
    this->scheduleUpdate(); 
    return true;
}


void HelloWorld::initCreep()
{
    CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("hero.plist");
    
}

void HelloWorld::start(float t) 
{
    CCLOG("start");
    CCAnimation* animation=CCAnimation::create();
    CCSpriteFrame* frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()
        ->spriteFrameByName("hero_1.png");
    animation->addSpriteFrame(frame1);
    CCSpriteFrame* frame2 = CCSpriteFrameCache::sharedSpriteFrameCache()
        ->spriteFrameByName("hero_2.png"); 
    animation->addSpriteFrame(frame2);
    animation->setDelayPerUnit(0.1);
    animation->setRestoreOriginalFrame(true);
    creep=CCRepeatForever::create(CCAnimate::create(animation));
    tank->runAction(creep);
}

void HelloWorld::restart(float t) 
{
    CCLOG("restart");
    CCAnimation* animation=CCAnimation::create();
    CCSpriteFrame* frame1 = CCSpriteFrameCache::sharedSpriteFrameCache()
        ->spriteFrameByName("hero_1.png");
    animation->addSpriteFrame(frame1);
    CCSpriteFrame* frame2 = CCSpriteFrameCache::sharedSpriteFrameCache()
        ->spriteFrameByName("hero_2.png"); 
    animation->addSpriteFrame(frame2);
    animation->setDelayPerUnit(0.1);
    animation->setRestoreOriginalFrame(true);
    creep=CCRepeatForever::create(CCAnimate::create(animation));
    tank->runAction(creep);
}

void HelloWorld::stop(float t) 
{
    CCLOG("stop");
    tank->stopAction(creep);
}

void HelloWorld::update(float t) 
{

}

 

cocos2d 动作切换

标签:style   blog   color   io   os   ar   for   div   sp   

原文地址:http://www.cnblogs.com/yufenghou/p/3992028.html

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