实现一个demo,具备以下功能:

1.实现带一个参数或者两个参数的方法回调。

2.实现按钮围绕屏幕转动。

3.实现场景的切换(中间要有过渡场景,以便实现前一个场景资源的释放)。

4.实现label的循环旋转+不停的来回移动。

效果图:




[cocos2d-x]动作+场景切换_#endif

[cocos2d-x]动作+场景切换_#endif_02

[cocos2d-x]动作+场景切换_#include_03

[cocos2d-x]动作+场景切换_2d_04



实现代码:

HelloWorldScene.h:


#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include "cocos2d.h"
using namespace cocos2d;
class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);

    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
    
    //函数回调,带一个参数的回调
    void funcN_CallBack(void *sender);
    //带两个参数的回调
    void funcND_CallBack(void *sender,void *data);
};

#endif // __HELLOWORLD_SCENE_H__


HelloWorldScene.cpp:


#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
#include "SecondScene.h"
using namespace cocos2d;
using namespace CocosDenshion;

CCScene* HelloWorld::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    HelloWorld *layer = HelloWorld::create();

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

    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool HelloWorld::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }

    /
    // 2. add a menu item with "X" image, which is clicked to quit the program
    //    you may modify it.

    // add a "close" icon to exit the progress. it's an autorelease object
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                        "CloseNormal.png",
                                        "CloseSelected.png",
                                        this,
                                        menu_selector(HelloWorld::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );

    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);

    
    
    
    
    /
    // 3. add your codes below...

    // add a label shows "Hello World"
    // create and initialize a label
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);

    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    CCCallFuncN *callFuncN = CCCallFuncN::create(this, callfuncN_selector(HelloWorld::funcN_CallBack));
    pLabel->runAction(callFuncN);
    
    // position the label on the center of the screen
    pLabel->setPosition( ccp(size.width / 2, size.height - 20) );

    // add the label as a child to this layer
    this->addChild(pLabel, 1);
    
    CCLabelTTF* pLabel2 = CCLabelTTF::create("hi", "Thonburi", 34);
    
    //带两个参数的回调
    CCString *str = CCString::create("带两个参数的回调");
    str->retain();
    CCCallFuncND *callFuncND = CCCallFuncND::create(this,callfuncND_selector(HelloWorld::funcND_CallBack),str); //最后一个参数是void*可以是任意类型
    pLabel2->runAction(callFuncND);
    
    pLabel2->setPosition(ccp(size.width / 2 + 20,40));
    this->addChild(pLabel2,1);
    
    // add "HelloWorld" splash screen"
    CCSprite* pSprite = CCSprite::create("HelloWorld.png");

    // position the sprite on the center of the screen
    pSprite->setPosition( ccp(size.width/2, size.height/2) );

    // add the sprite as a child to this layer
    this->addChild(pSprite, 0);
    
    return true;
}

int i=0;
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCMenuItem *item = (CCMenuItem *)pSender;
/******舒缓动作********************************************************/
    //    CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20));
    //    item->runAction(action);
    
/******先慢再快********************************************************/
    //    CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20));
    //    CCActionInterval *ease = CCEaseInOut::create(move, 4);
    //    item->runAction(ease);
    
/*******循环移动**********************************************************/
    //float duration = CCRANDOM_0_1()*5+1;
    float duration = 0.1f;
    CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20));
    CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20));
    CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20));
    CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20));
    CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL);
    //CCRepeatForever *repeat = CCRepeatForever::create(sequence);
    item->runAction(sequence);
    i++;
    
    //跳转
    if (i==2) {
        //replease切换场景
//        SecondScene *sense = SecondScene::create();
//        CCScene *secScene = CCScene::create();
//        secScene->addChild(sense,0);
//        CCDirector::sharedDirector()->replaceScene(secScene);
        
        //push切换场景
        CCTransitionFade *secondScene = CCTransitionFade::create(1.0f,SecondScene::scene(),ccGREEN);
        CCDirector::sharedDirector()->pushScene(secondScene);
    }
}

//回调函数
void HelloWorld::funcN_CallBack(void *sender)
{
    CCLabelTTF *label = (CCLabelTTF *)sender;
    label->setString("带一个参数的回调");
    CCLog("CallFuncN的回调");
}

//带两个参数的回调
void HelloWorld::funcND_CallBack(void *sender,void *data)
{
    CCString *str = (CCString *)data;
    CCLabelTTF *label = (CCLabelTTF *)sender;
    label->setString(str->getCString());
}


SecondScene.h:


#ifndef ___013_9_4___________SecondScene__
#define ___013_9_4___________SecondScene__

#include <iostream>
#include "cocos2d.h"
using namespace cocos2d;
class SecondScene : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();
    
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(SecondScene);
    
};

#endif /* defined(___013_9_4___________SecondScene__) */


SecondScene.cpp:


#include "SecondScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

CCScene* SecondScene::scene()
{
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();
    
    // 'layer' is an autorelease object
    SecondScene *layer = SecondScene::create();
    
    // add layer as a child to scene
    scene->addChild(layer);
    
    // return the scene
    return scene;
}

// on "init" you need to initialize your instance
bool SecondScene::init()
{
    //
    // 1. super init first
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                                          "CloseNormal.png",
                                                          "CloseSelected.png",
                                                          this,
                                                          menu_selector(SecondScene::menuCloseCallback) );
    pCloseItem->setPosition( ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20) );
    
    // create menu, it's an autorelease object
    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
    pMenu->setPosition( CCPointZero );
    this->addChild(pMenu, 1);
    
    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Thonburi", 34);
    
    // ask director the window size
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    
    // position the label on the center of the screen
    
    pLabel->setPosition( ccp(size.width / 2, size.height / 2) );
    pLabel->setAnchorPoint(ccp(0.5, 0.5));
    pLabel->setTag(10);
    // add the label as a child to this layer
    this->addChild(pLabel, 1);
}
void SecondScene::menuCloseCallback(CCObject* pSender)
{
//        CCDirector::sharedDirector()->end();
//    
//    #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
//        exit(0);
//    #endif
    
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    CCMenuItem *item = (CCMenuItem *)pSender;
/******舒缓动作********************************************************/
    //    CCActionInterval *action = CCMoveTo::create(2, ccp(20,size.height-20));
    //    item->runAction(action);
    
/******先慢再快********************************************************/
    //    CCMoveTo *move = CCMoveTo::create(3, ccp(size.width-20, size.height-20));
    //    CCActionInterval *ease = CCEaseInOut::create(move, 4);
    //    item->runAction(ease);
    
    
/*******循环移动**********************************************************/
    //float duration = CCRANDOM_0_1()*5+1;
    float duration = 0.1f;
    CCMoveTo *move1 = CCMoveTo::create(duration, ccp(20, 20));
    CCMoveTo *move2 = CCMoveTo::create(duration,ccp(20, size.height-20));
    CCMoveTo *move3 = CCMoveTo::create(duration, ccp(size.width - 20, size.height-20));
    CCMoveTo *move4 = CCMoveTo::create(duration, ccp(size.width-20, 20));
    CCSequence *sequence = CCSequence::create(move1,move2,move3,move4,NULL);
    CCRepeatForever *repeat = CCRepeatForever::create(sequence);
    item->runAction(repeat);
    
    
    CCLabelTTF *label = (CCLabelTTF *)this->getChildByTag(10);
    CCRotateBy *rotateBy = CCRotateBy::create(8, 360);
    CCMoveTo *move11 = CCMoveTo::create(2, ccp(0, size.height/2));
    CCMoveTo *move13 = CCMoveTo::create(2, ccp(size.width, size.height/2));
    CCMoveTo *move12 = CCMoveTo::create(2, ccp(size.width/2, size.height/2));
    CCSequence *sequence1 = CCSequence::create(move11,move12,move13,move12,NULL);
    CCSpawn *span = CCSpawn::create(sequence1,rotateBy,NULL);
    CCRepeatForever *repeat1 = CCRepeatForever::create(span);
    label->runAction(repeat1);


}