Reputation: 3239
I'm porting my iOS game from Cocos2d to Cocos2d-x. I'm not the best with C++ yet so I couldn't debug this on my own !
What I have is a simple scenario of two Scenes, one loads at runtime to show intro, then loads another scene, the first intro scene is loaded by :
//Create a scene. it's an autorelease object
CCScene *pScene = landingScene::scene();
// Run intro scene
pDirector->runWithScene(pScene);
Now after this loads, everything is okay, till i try to replace that scene by running:
CCDirector::sharedDirector()->replaceScene(mainScene::scene());
As soon as I call this , the app asserts and gives the following message:
Assertion failed: (index<=arr->num),functionccArrayInsertObjectAtIndex, xxx/libs/cocos2dx/support/data_support/ccCArray.cpp, line 153.
I go to the line to check out the line and this is the content:
/** Inserts an object at index */
void ccArrayInsertObjectAtIndex(ccArray *arr, CCObject* object, unsigned int index){
CCAssert(index<=arr->num, "Invalid index. Out of bounds");
CCAssert(object != NULL, "Invalid parameter!");
...
}
This is the content of my Intro (landing) Scene .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 {
public:
landingScene();
~landingScene();
void loadGame();
static cocos2d::CCScene* scene();
private:
//The game state Singleton
GameState *sharedGameState;
};
And the .cpp file:
#include "landingScene.h"
#include "SimpleAudioEngine.h"
#include "mainScene.h"
#include "introScene.h"
using namespace cocos2d;
using namespace CocosDenshion;
landingScene::landingScene(){
setTouchEnabled( true );
//Load some sprites here, removed it for simplicity
//This is where the app crashes
landingScene::loadGame();
}
landingScene::~landingScene(){
}
CCScene* landingScene::scene(){
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// add layer as a child to scene
CCLayer *layer = new landingScene();
scene->addChild(layer);
return scene;
}
void landingScene::loadGame(){
CCDirector::sharedDirector()->replaceScene(mainScene::scene());
}
And this is the content of my Main scene that I'm trying to show:
#ifndef _MAIN_SCENE_H_
#define _MAIN_SCENE_H_
//When you import this file, you import all the cocos2d classes
#include "cocos2d.h"
#include "GameState.h"
class mainScene : public cocos2d::CCLayer {
public:
~mainScene();
mainScene();
static cocos2d::CCScene* scene();
private:
GameState *sharedGameState;
};
#endif // _MAIN_SCENE_H_
And the .cpp file:
#include "mainScene.h"
#include "cocos2d.h"
#include "SimpleAudioEngine.h"
using namespace cocos2d;
using namespace CocosDenshion;
mainScene::mainScene(){
}
mainScene::~mainScene(){
}
CCScene* mainScene::scene(){
// 'Scene' is an autorelease object
CCScene *scene = new CCScene();
// Add layer as a child to scene
CCLayer* layer = new mainScene();
scene->addChild(layer);
layer->release();
return scene;
}
Upvotes: 2
Views: 4678
Reputation: 3182
The reason is you replaced the scene even before your first scene is finished creating. Try to call the replace function in onEnter() or onTransitionDidfFinished()
Upvotes: 4