[Solved] Got an error when replacescene in same target.

cocos2d-x 3.0 rc0
I use chipmunk, as cocos2d-x physics integration.
but I got an error, when I replace scene while two bodies have contacted.

File:CCScheduler.cpp
Line:470
Expression:hashElement->entry->markedForDeletion

How can I fix it?

Below simple test code

``` #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__

#include “cocos2d.h”

class HelloWorld : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();
virtual bool init();
CREATE_FUNC(HelloWorld);

bool onContactBegin(const cocos2d::PhysicsContact& contact);

};

#endif // HELLOWORLD_SCENE_H



<cpp>

#include “HelloWorldScene.h”

USING_NS_CC;

Scene* HelloWorld::createScene()
{

auto scene = Scene::createWithPhysics();

// Set Physics World
scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); // Set debug Draw
scene->getPhysicsWorld()->setGravity(Vect::ZERO);	//set Gravity

// Set Boundary as Screen Size
Size visibleSize = Director::getInstance()->getVisibleSize();
auto body = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 1);
body->setGroup(-100);
auto edgeNode = Node::create();
edgeNode->setPosition(Point(visibleSize.width/2,visibleSize.height/2));
edgeNode->setPhysicsBody(body);
scene->addChild(edgeNode,0);

//Layer
auto layer = HelloWorld::create();
scene->addChild(layer, -1);

return scene;

}

// on “init” you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}

// sprite1 
auto heroSpr = Sprite::create("test.png");
heroSpr->setPosition(100,160);
auto body = PhysicsBody::createCircle(heroSpr->getContentSize().width/2);
body->setGroup(100);
body->setContactTestBitmask(0xFFFFFFFF);
heroSpr->setPhysicsBody(body);
this->addChild(heroSpr,1);


// sprite2
auto targetSpr = Sprite::create("test.png");
targetSpr->setPosition(400,160);
auto body2 = PhysicsBody::createCircle(targetSpr->getContentSize().width/2);
body2->setGroup(100);
body2->setContactTestBitmask(0xFFFFFFFF);
targetSpr->setPhysicsBody(body2);
this->addChild(targetSpr,1);
targetSpr->runAction(MoveTo::create(3,Point(180,160)));


// contact
auto contactListener = EventListenerPhysicsContactWithGroup::create(100);
contactListener->onContactBegin = CC_CALLBACK_1(HelloWorld::onContactBegin, this);
Director::getInstance()->getEventDispatcher()->addEventListenerWithFixedPriority(contactListener,100);


return true;

}

bool HelloWorld::onContactBegin(const PhysicsContact& contact)
{
Director::getInstance()->replaceScene(HelloWorld::createScene());
return true;
}

Why you replace scene at HelloWorld callback? When the scene be removed, it will release all of the children, so you destruct the HelloWorld layer in it’s member function, that’s why you got an exception.

@boyu0

Yes. you’re right.
I guessed like that.

but, If I want to replace in same scene,
Do I have to make blank scene for transition and release member?
or is there an another trick?
(now, I just implement blank scene for transition. It works perfect.)

thank you.

Why you want to do that? I think it’s not a technic problem, I think it’s a design problem, I think you need a better design.
You can add a base layer for the scene, all the other layer added to this base layer, and use this base layer to control add/remove layers, like our cpp-test doing.
I don’t know what your demand is, I just make an example for you.

@boyu0
Oh. Yes! Yes!!!
You are right.
OMG… How stupid I was. oh.,my god…

You save my ass. I agonize this problem about 3 days… oh… jesus…
Thank you. I really appreciate your comment.
Now, I can figure it out how I can solve my problem.

My goal was make an casual game in 5 days.
But I really sick of check static var.
now, as you know… It was wrong design.
I think now I can make a game in 3 days. :slight_smile:
Thank you again.

@howlryu it’s my pleasure :slight_smile: