I have a weird bug where switching scenes causes my app to crash in android. It works fine in windows and only happens in android when I add labels. When the app opens the labels load fine but then when I change scenes the app eventually crashes, sometimes on the first scene transition and sometimes in the second transition going back to the first scene. The app is basically two scenes each with a button and a label, the button transitions between the scenes. The code for the two scenes is shown below:
HelloWorldScene.cpp:
#include "HelloWorldScene.h"
#include "AnotherScene.h"
Scene* HelloWorldScene::createScene()
{
auto scene = Scene::create();
auto layer = HelloWorldScene::create();
scene->addChild(layer);
return scene;
}
bool HelloWorldScene::init()
{
if ( !Layer::init() )
{
return false;
}
auto label = Label::createWithTTF("Hello Scene", "fonts/arial.ttf", 30);
label->setPosition(Point(500, 500));
this->addChild(label);
ui::Button* switchSceneButton = ui::Button::create("CloseNormal.png", "CloseSelected.png");
switchSceneButton->setPosition(Point(700, 500));
switchSceneButton->addTouchEventListener(CC_CALLBACK_2(HelloWorldScene::switchScene, this));
this->addChild(switchSceneButton);
return true;
}
void HelloWorldScene::switchScene(cocos2d::Ref* sender, ui::Widget::TouchEventType type) {
auto scene = AnotherScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
}
AnotherScene.cpp:
#include "AnotherScene.h"
#include "HelloWorldScene.h"
Scene* AnotherScene::createScene()
{
auto scene = Scene::create();
auto layer = AnotherScene::create();
scene->addChild(layer);
return scene;
}
bool AnotherScene::init()
{
if ( !Layer::init() )
{
return false;
}
auto label = Label::createWithTTF("Another Scene", "fonts/arial.ttf", 30);
label->setPosition(Point(500, 500));
this->addChild(label);
ui::Button* switchSceneButton = ui::Button::create("CloseNormal.png", "CloseSelected.png");
switchSceneButton->setPosition(Point(700, 500));
switchSceneButton->addTouchEventListener(CC_CALLBACK_2(AnotherScene::switchScene, this));
this->addChild(switchSceneButton);
return true;
}
void AnotherScene::switchScene(cocos2d::Ref* sender, ui::Widget::TouchEventType type) {
auto scene = HelloWorldScene::createScene();
Director::getInstance()->replaceScene(TransitionFade::create(1, scene));
}
The font is saved in my resources folder under fonts/arial.ttf. Android studio also displays the fonts under the assets directory so I’m pretty sure the files are getting found. The error message that comes out in android studio is: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x43c8c02c in tid 11723 (GLThread 44794). Also I’m using a Samsung S4 to test. Any ideas?
EDIT: it also crashes for me when using a Nexus 5, so I’m pretty sure the error is not device specific. Also I’m using cocos2d-x version 3.12.