1

我有安卓开发背景。前几天开始学习Cocos2d-x进行游戏开发。我正在创建一个简单的初始屏幕和一些菜单屏幕,但遇到了一些应用程序崩溃问题。我希望 Splash Scene 停留 3 秒,然后加载 MenuScene。所以我尝试使用调度方法来替换场景。但是在执行计划任务时我遇到了崩溃。这是我的简单课程;

Splash.h 类:

#ifndef SPLASHSCREEN_H_
#define SPLASHSCREEN_H_

#include <layers_scenes_transitions_nodes/CCLayer.h>
#include <cocos2d.h>

/**
 * Splash Screen for the game
 *
 */

USING_NS_CC;

class SplashScreen: public cocos2d::CCLayer {
public:
    SplashScreen();
    virtual ~SplashScreen();
    virtual bool init();
    static cocos2d::CCScene* newInstance();
    void endSplash(float dt);

    // implement the "static node()" method manually
    CREATE_FUNC(SplashScreen);


};

#endif

闪屏.cpp:

    #define COCOS2D_DEBUG 1

#include "SplashScreen.h"
#include "GeneralMenuScreen.h"


USING_NS_CC;

SplashScreen::SplashScreen() {
    // TODO Auto-generated constructor stub

}

SplashScreen::~SplashScreen() {
    // TODO Auto-generated destructor stub
}

bool SplashScreen::init() {

    if(! CCLayer::init() ) {
        return false;
    }
    CCSize windowSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCSprite* background = CCSprite::create("background.png");
    background->setPosition(ccp(origin.x + windowSize.width/2, origin.y + windowSize.height/2));
    this->addChild(background);

    CCLabelTTF* loadingText = CCLabelTTF::create("Loading...", "Arial", 36);
    loadingText->setAnchorPoint(ccp(0,0));
    loadingText->setPosition(ccp(origin.x + windowSize.width/2 - loadingText->getContentSize().width/2, origin.y + windowSize.height/2 - loadingText->getContentSize().height/2));
    this->addChild(loadingText);

    /*  this->schedule(schedule_selector(SplashScreen::endSplash), 5.0);        */
CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(SplashScreen::endSplash), this , 5, false, 0, 0 );

/*   CCTimer* ccTimer = CCTimer::timerWithTarget(this, schedule_selector(SplashScreen::endSplash), 5.0f);
ccTimer->update(1.0);       */


/* CCDelayTime* startDelay = CCDelayTime::create(5.0);
CCCallFunc *showHearts = CCCallFunc::create(this, callfunc_selector(SplashScreen::endSplash));
CCSequence* seq = CCSequence::create(startDelay, showHearts);
background->runAction(seq);*/

    return true;

}


CCScene* SplashScreen::newInstance() {

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

    // 'layer' is an autorelease object
    SplashScreen* splashScreen = SplashScreen::create();

    scene->addChild(splashScreen);

    return scene;

}

void SplashScreen::endSplash(float dt) {

    //CCLOG("FilePath = %f", dt);
    CCScene* menuScreen = GeneralMenuScreen::newInstance();
    CCDirector::sharedDirector()->replaceScene(menuScreen);

}

GeneralMenuScreen.cpp:

#include "GeneralMenuScreen.h"

USING_NS_CC;

GeneralMenuScreen::GeneralMenuScreen() {

}

GeneralMenuScreen::~GeneralMenuScreen() {

}

CCScene* GeneralMenuScreen::newInstance() {

    CCScene* scene = CCScene::create();
    GeneralMenuScreen* layer = GeneralMenuScreen::create();
    scene->addChild(layer);

    return scene;

}

bool GeneralMenuScreen::init() {

    if( ! CCLayer::init()) {
        return false;
    }

    CCSize windowSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

    CCSprite* background = CCSprite::create("background.png");
    background->setPosition(ccp(origin.x + windowSize.width/2, origin.y + windowSize.height/2));
    this->addChild(background);

    int margin = 10;

    CCLabelTTF* playLabel = CCLabelTTF::create("PLAY", "Arial", 36);
    CCMenuItemLabel* menuPlay = CCMenuItemLabel::create(playLabel);
    menuPlay->setPosition(origin.x + windowSize.width/2 - menuPlay->getContentSize().width/2, origin.y + windowSize.height/2 + menuPlay->getContentSize().height + margin/2 );

    CCMenu* menu = CCMenu::create(menuPlay);
    menu->setPosition(CCPointZero);
    this->addChild(menu);


    return true;


}

启动画面运行,但 3 秒后,endSplash 方法没有被调用并且应用程序崩溃。我从来没有用 C/C++ 编程过。知道我哪里出错了吗?还有,有没有什么方法可以像我们在 android/ios 中那样使用调试器来调试 cocos2d-x 应用程序?

4

4 回答 4

0

而不是这个...

CCDirector::sharedDirector()->getScheduler()->scheduleSelector(schedule_selector(SplashScreen::endSplash), this , 5, false, 0, 0 );

尝试这个..

this->scheduleOnce(schedule_selector(SplashScreen::endSplash),delayTime);

其中 delayTime =3 或任何东西。

于 2013-12-03T04:55:10.277 回答
0

所以经过一番调查:

  • CCObject 的构造函数应该是私有的(读作:几乎总是必须的)
  • 在 CCObject.h 中,用 static_cast 替换默认的类似 c 的强制转换是一个好主意 --> 您将避免花费大量时间调试选择器
  • 你的代码在 VC12 下对我有用(但我使用 pushScene 而不是 replaceScene)--> 你使用的是 cocos 的 master 分支吗?
于 2013-12-02T19:00:26.467 回答
0

使用它它会为你工作

schedule(SEL_SCHEDULE(&SplashScreen::endSplash), 5);
于 2013-12-05T11:53:37.137 回答
0

您可以使用 gdb 进行调试。

你写这些 getInstance 方法是为了什么?在大多数情况下,一对 create + init 就足够了。

也许尝试使用 CCDirector::sharedDirector()->getScheduler()->scheduleSelector 或只是运行调用对象上的某些方法的操作:http: //www.cocos2d-x.org/reference/native-cpp/V3.0alpha0/ d3/d32/classcocos2d_1_1_call_func.html。管理在您的对象上运行的操作比处理调度程序 IMO 更容易。

当出现此类错误时,您可能需要尝试放置一些断言来检查对象是否未死(检查是否不是 nullptr 和 retaincount)。当然,使用调试器运行它以查看爆炸发生的位置。现在我在你的代码中找不到错误,我稍后会尝试在家里运行它。

于 2013-12-02T09:23:07.997 回答