ReplaceScene asserts, I'm doing it wrong :(

ReplaceScene asserts, I'm doing it wrong :(
0.0 0


Hey guys, I’m porting my iOS game from cocos2d-iphone to cocos2d-x.

I’ve hit a problem and been stuck there for the second day in a row, I’m trying to have a landing scene that loads either an intro scene or the main menu (depending on the user prefs)

the problem occurs once i try to:

The app immediately asserts and gives this error:
Assertion failed: (index<=arr->num), function ccArrayInsertObjectAtIndex, file xxx/libs/cocos2dx/support/data_support/ccCArray.cpp, line 153.

This is cocos2d-x 2.0.2

In AppDelegate.cpp I call the landing Scene like this:

The contens ot the LandingScene class is:

.h file:

#ifndef __LANDING_SCENE_H__
#define __LANDING_SCENE_H__

// When you import this file, you import all the cocos2d classes
#include "cocos2d.h"
#include "GameState.h"


class landingScene : public cocos2d::CCLayer {
    void loadGame();
    void loadIntro();
    virtual bool init();
    static cocos2d::CCScene* scene();

    GameState *sharedGameState;

#endif // __LANDING_SCENE_H__

and .CPP:

#include "landingScene.h"
#include "SimpleAudioEngine.h"
//#include "mainScene.h"
#include "introScene.h"

using namespace cocos2d;
using namespace CocosDenshion;


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

    // add layer as a child to scene
    landingScene *layer = landingScene::create();


    return scene;

bool landingScene::init()

    if ( !CCLayer::init() )
        return false;
    sharedGameState = GameState::sharedInstance();

    CCSize s = CCDirector::sharedDirector()->getWinSize();

    CCSprite *fadeSplash;
    fadeSplash = CCSprite::create("Default.png");
    fadeSplash->setPosition(ccp(s.width/2, s.height/2));

     //Load intro
    return true;

void landingScene::loadIntro(){

The contents of the introScene is exactly the same, but i have a different sprite in it, that’s all !
can you guys help me out where my error is ? each scene works ok alone when tested, the error only pops up
when i try replacing any scene with the other …

can you show me a little example on how to replace scenes ?? cause in the cocos2d-x hello world code it doesn’t replace scenes the same way I do, it
creates layers instead of scenes and then calls replaceScene, i’m not sure how that works !?

any help ?:slight_smile:


I have solved the problem, apparently I was replacing scene too fast that the initial scene wasnt’t added to the array yet, trying to replace a nonexistent scene !

The solution was just to add 500 msec delay and it worked Perfectly !