在cocos 2d-x中创建启动画面

时间:2014-06-19 07:21:22

标签: cocos2d-x

您好我正在使用cocos2d-x创建一个游戏,当我在启动画面上为运行游戏安排一个事件时。它显示编译错误(在扩展宏'schedule_selector'中)

以下是我的代码

Splash.h

#ifndef SPLASH_H_
#define SPLASH_H_
#include "cocos2d.h"
class CCSplashLayer : public cocos2d::CCLayer {
private :
     void runGame();
public:
    static cocos2d::CCScene* createScene();
    virtual bool init();
    CREATE_FUNC(CCSplashLayer);
};
#endif /* SPLASH_H_ */

和SplashScene.cpp

#include "splash.h"
#include "cocos2d.h"
#include "HelloWorldScene.h"

USING_NS_CC;

bool CCSplashLayer::init() {
    if (!Layer::init()) {
        return false;
    }
    Size visibleSize = Director::getInstance()->getVisibleSize();

    auto sprite = Sprite::create("splash.png");
    sprite->setScale(Director::getInstance()->getContentScaleFactor());
    sprite->setPosition(Vec2(visibleSize.width / 2,visibleSize.height/2));
    this->addChild(sprite, 0);

    //This line cause problem show i symbol on this line in eclipse
    this->scheduleOnce(schedule_selector(CCSplashLayer::runGame),4.0f);

    return true;
}

Scene* CCSplashLayer::createScene() {
    auto scene = CCScene::create();
    auto layer = CCSplashLayer::create();
    scene->addChild(layer);
    return scene;
}

void CCSplashLayer::runGame(){
     auto scene = HelloWorld::createScene();
     Director::getInstance()->setDepthTest(true);
     TransitionScene *transition = TransitionScene::create(0.5f, scene);
     Director::getInstance()->pushScene(transition);
}

3 个答案:

答案 0 :(得分:0)

schedule_selector接受函数指针,需要浮点参数。

在定义和声明中将方法CCSplashLayer::runGame()更改为CCSplashLayer::runGame(float dt)

此外,您正在飞溅场景上推动场景,这不是飞溅场景的推荐方式。你必须用新场景替换飞溅场景,因为除非游戏有特定的设计要求,否则我们永远不需要在游戏中再次显示启动。

答案 1 :(得分:0)

试试这个宏:

#define CCDL_PERFORM_SELECTOR( __OWNER__, __DELAY__, __CALLFUNC_SELECTOR__ ) \
    __OWNER__->runAction( cocos2d::Sequence::create( \
        cocos2d::DelayTime::create( __DELAY__ ), \
        cocos2d::CallFunc::create( CC_CALLBACK_0( __CALLFUNC_SELECTOR__,__OWNER__) ), \
        nullptr )); \

(来源:https://github.com/duksel/Cocos2dx-DukselLib/blob/master/Cocos2dx-DukselLib/DukselMacros.h#L70-L74

允许用于任何CCNode(+子类)实例。 在你的情况下将是:

bool CCSplashLayer::init()
{
    CCDL_PERFORM_SELECTOR( this, 4.f, CCSplashLayer::runGame );
}    

答案 2 :(得分:-1)

试试这样...... 在init方法中,

this->runAction(CCSequence::create(CCDelayTime::create(4.0f),
                CCCallFuncN::create(this,callfuncN_selector(CCSplashLayer::runGame)),
                NULL));

并添加此方法。

void CCSplashLayer::runGame(){
     auto scene = HelloWorld::createScene();
     Director::getInstance()->setDepthTest(true);
     TransitionScene *transition = TransitionScene::create(0.5f, scene);
     Director::getInstance()->pushScene(transition);
}
相关问题