cocos2d hello world

StartScene.cpp

CCScene* Start::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        Start *layer = Start::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

bool Start::init()
{
    bool bRet = false;
    do 
    {

        CC_BREAK_IF(! CCLayer::init());


        //CCTouchDispatcher::addTargetedDelegate(this,kCCMenuMousePriority,true);
        
        CCSize size = CCDirector::sharedDirector()->getWinSize();

        CCSprite *bg = CCSprite::createWithSpriteFrameName("start_bg.png");
        bg->setContentSize(size);
        bg->setPosition(ccp(size.width/2,size.height/2));
        this->addChild(bg,1);

        CCSprite *rabit = CCSprite::createWithSpriteFrameName("start_rabbit_1.png");
        rabit->setContentSize(CCSize(100,100));
        rabit->setPosition(ccp(150,150));
        this->addChild(rabit,2);


        CCLabelTTF* pLabel = CCLabelTTF::create("Edragonrise", "微软雅黑", 24);
        CC_BREAK_IF(! pLabel);
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

        ccColor3B c0;  
        c0.r=255;  
        c0.g=255;  
        c0.b=0;
        pLabel->setColor(c0);
        this->addChild(pLabel, 2);

        bRet = true;

    } while (0);

    return bRet;
};  


void Start::enterAdWall(){


}
void Start::QueryScore(){

}
void Start::ReduceScore(){

}

LoadScene.cpp

#include "LoadScene.h"
#include "HelloWorldScene.h"
#include "StartScene.h"

using namespace cocos2d;

int printIndex;
CCLabelTTF* lblGuide;
CCSprite *loadSprite;
CCSprite *logo;
CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();

CCScene* Load::scene()
{
    CCScene * scene = NULL;
    do 
    {
        // 'scene' is an autorelease object
        scene = CCScene::create();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        Load *layer = Load::create();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene
        scene->addChild(layer);
    } while (0);

    // return the scene
    return scene;
}

bool Load::init()
{
    bool bRet = false;
    do 
    {
        
        CC_BREAK_IF(! CCLayer::init());

        CCLabelTTF* pLabel = CCLabelTTF::create(G2U("Edragonrise 龙腾云网"), "微软雅黑", 24);
        CC_BREAK_IF(! pLabel);

        size = CCDirector::sharedDirector()->getWinSize();
        pLabel->setPosition(ccp(size.width / 2, size.height - 50));

        ccColor3B c0;  
        c0.r=255;  
        c0.g=255;  
        c0.b=0;
        pLabel->setColor(c0);
        this->addChild(pLabel, 1);
        
        loadSprite=CCSprite::createWithSpriteFrameName("loading_1.png");
        this->addChild(loadSprite,2);
        loadSprite->setPosition(ccp(size.width/2+20, 20));

        int animateNum = 4;
        char tmp[50];
        CCArray *array = CCArray::create();
        for (int i = 0; i < animateNum; i++)
        {
            sprintf(tmp,"loading_%d.png",i+1);
            CCSpriteFrame *frame = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(tmp);
            array->addObject(frame);
        }
        //CCActionInterval
        CCAnimation *animation = CCAnimation::createWithSpriteFrames(array,0.3f);
        CCAnimate *animate = CCAnimate::create(animation);
        loadSprite->runAction(CCRepeatForever::create(animate));


        logo=CCSprite::create("logo.png");
        logo->setPosition(ccp(size.width/2, size.height/2));
        this->addChild(logo,1);

        Load::showNowTime("init:1"); 
        scheduleOnce(schedule_selector(Load::loadResouces),3.0f);
        Load::showNowTime("init:2"); 

        


        //CCActionInterval *ac1 = CCFadeOut::create(1.5f);
        //CCCallFunc *ac2=;
        /*
        CCFiniteTimeAction *action = CCSequence::create(
            //CCCallFunc::create(this,callfunc_selector(Load::loadResouces)),
            CCFadeOut::create(1.5f),
            CCCallFunc::create(this,callfunc_selector(Load::gotoBegin)),
            NULL);
        logo->runAction(action);
        */
        //logo->runAction(CCFadeOut::create(1.5f));

        bRet = true;
    } while (0);

    return bRet;
}


char* Load::G2U(const char* gb2312)    
{    
    int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);    
    wchar_t* wstr = new wchar_t[len+1];    
    memset(wstr, 0, len+1);    
    MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);    
    len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);    
    char* str = new char[len+1];    
    memset(str, 0, len+1);    
    WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);    
    if(wstr) delete[] wstr;    
    return str;    
}

void Load::gotoBegin()
{
    Load::showNowTime("gotoBegin:1");
    lblGuide = CCLabelTTF::create("", "微软雅黑", 24);
    printIndex = 0;
    this->schedule(schedule_selector(Load::print),0.1f);
    lblGuide->setColor(ccWHITE);
    //lblGuide->setAnchorPoint(ccp(0,0));
    lblGuide->setPosition(ccp(size.width/2,size.height/2));
    //lblGuide->setDimensions(CCSize(size.width,90));
    this->addChild(lblGuide, 5);
    
    CCLabelTTF *swip = CCLabelTTF::create(G2U("跳过"), "STXINGKA.ttf", 22);
    CCMenuItemLabel *menu = CCMenuItemLabel::create(swip,this,menu_selector(Load::gotoStart));
    CCMenu *startMenu=CCMenu::create(menu,NULL);
    startMenu->setPosition(ccp(size.width-80,50));
    this ->addChild(startMenu,2); 
    

    
    CCActionInterval* a2=CCScaleTo::create(0.3f,1.3f);
    CCActionInterval* a3=CCScaleTo::create(0.3f,1.0f);
    CCActionInterval* a4=CCScaleTo::create(0.3f,1.3f);
    CCActionInterval* a5=CCScaleTo::create(0.3f,1.0f);
    CCActionInterval *a6 = CCSequence::create(CCDelayTime::create(1.0f), a2,a3,a4,a5,NULL);
    /*
    CCFiniteTimeAction *action2 = CCSequence::create(
        CCRepeat::create(a6,3.0f),
        CCCallFunc::create(this,callfunc_selector(Load::gotoStart)),
        NULL);
    */
    menu->runAction(CCRepeatForever::create(a6));
    //menu->runAction(CCRepeatForever::create(a6,3.0f));
    
    /*
    CCMenuItemImage *select=CCMenuItemImage::create("p0.png",  
        "p1.png",  
        this,                                                          
        menu_selector(Load::selectGame)
        );  

    CCSize selectSize=select->getContentSize();  

    select->setPosition(ccp(230,145));  

    CCMenu *menu=CCMenu::create(select,NULL);

    menu->setPosition(ccp(0,0));
    this ->addChild(menu,1);  
    */

}

void Load::gotoStart(CCObject* pSender)
{
    //[[SimpleAudioEngine sharedEngine] playEffect:@"button.m4a"];
    //CCTransitionScene *ts = [CCTransitionFade transitionWithDuration:1.0f scene:[Start scene] withColor:ccBLACK];
    //[[CCDirector sharedDirector] replaceScene:ts];
    Load::showNowTime("gotoStar:1");
    CCScene *s=Start::scene();  
    CCDirector::sharedDirector()->replaceScene(CCTransitionFade::create(1.5f, s)); 

}


void Load::loadResouces(float dt)
{
    /*
    struct tm *tm; 
    time_t timep; 
    time(&timep); 
    tm = localtime(&timep); 
    */
    Load::showNowTime("loadResoucees:1");
    cache->addSpriteFramesWithFile("platform.plist");
    cache->addSpriteFramesWithFile("platform2.plist");
    cache->addSpriteFramesWithFile("platform3.plist");
    cache->addSpriteFramesWithFile("bg.plist"); 
    Load::showNowTime("loadResoucees:2");
    this->removeChild(loadSprite);
    Load::showNowTime("init:3");   

    CCFiniteTimeAction *action = CCSequence::create(
        CCFadeOut::create(1.5f),
        CCCallFunc::create(this,callfunc_selector(Load::gotoBegin)),
        NULL);
    logo->runAction(action);
    Load::showNowTime("init:4");
}

void Load::print(float interval)
{
    printIndex++;
    std::string txt="Hello world 
 I want play a game !";
    int maxPrint=txt.length();
    lblGuide->setString(txt.substr(0,printIndex).c_str());
    //CCLog(txt.substr(0,printIndex).c_str());
    if(printIndex>=maxPrint)
    {
        unschedule(schedule_selector(Load::print));
    }

}

void Load::showNowTime(char* para){
    struct timeval tv;     
    gettimeofday(&tv,NULL);     
    long haomiaos=tv.tv_sec * 1000 + tv.tv_usec/1000;
    char string[25];
    char* ch;
    ch = ltoa(haomiaos, string,10); 
    CCLog(para);
    CCLog(ch);
}
原文地址:https://www.cnblogs.com/jevil/p/3442082.html