-
Emirhan YılmazLikes 0Problem Description
I check collision and if the sprite body collided each other I run this line: arrowBody->setDynamic(false); but I have a error:
Aborting due to Chipmunk error: This operation cannot be done safely during a call to cpSpaceStep() or during a query. Put these calls into a post-step callback.
Failed condition: !space->locked
Source:/Users/riq/progs/cocos2d-x-3rd-party-libs-src/contrib/ios-x86_64/chipmunk/src/cpBody.c:174
How can I fix it ? Please Help Me!
-
Sonar Systems adminLikes 1
What version of Cocos2d-x are you using?
-
Emirhan YılmazLikes 0
I’m using 3.17
-
Emirhan YılmazLikes 0
\(#include "MainScene.h" #include "MyBodyParser.h" #include "SimpleAudioEngine.h" USING_NS_CC; Scene* MainScene::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_NONE); auto layer = MainScene::create(); layer->setPhysicsWorld(scene->getPhysicsWorld()); scene->addChild(layer); return scene; } bool MainScene::init() { if ( !Scene::init() ) { return false; } LayerColor *_bgColor = LayerColor::create(Color4B(0, 191, 255,230)); this->addChild(_bgColor, -10); auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(MainScene::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(MainScene::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); auto edgeNode = Node::create(); edgeNode->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); edgeNode->setPhysicsBody(edgeBody); this->addChild(edgeNode); auto circle = Sprite::create("circle.png"); circle->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); circle->runAction(RepeatForever::create(RotateBy::create(3.0f, 360))); MyBodyParser::getInstance()->parseJsonFile("circle.json"); auto circleBody = MyBodyParser::getInstance()->bodyFormJson(circle, "circle", PhysicsMaterial(0, 0, 1)); if(circleBody != nullptr) { circleBody->setDynamic(false); circle->setPhysicsBody(circleBody); } //this->addChild(circle); auto apple = Sprite::create("apple.png"); apple->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); auto appleBody = PhysicsBody::createCircle(apple->getContentSize().width / 2, PhysicsMaterial(0, 0, 0)); appleBody->setCollisionBitmask(1); appleBody->setContactTestBitmask(true); appleBody->setDynamic(false); apple->setPhysicsBody(appleBody); this->addChild(apple); auto cloud1 = Sprite::create("cloud.png"); cloud1->setPosition(Vec2(origin.x + 25, origin.y + visibleSize.height / 2)); this->addChild(cloud1); auto cloud2 = Sprite::create("cloud.png"); cloud2->setPosition(Vec2(origin.x + 145, origin.y + 300)); this->addChild(cloud2); arrow = Sprite::create("arrow.png"); arrow->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 6)); arrowBody = PhysicsBody::createBox(arrow->getContentSize(), PhysicsMaterial(0, 0, 0)); arrowBody->setCollisionBitmask(2); arrowBody->setContactTestBitmask(true); arrowBody->setDynamic(false); arrow->setPhysicsBody(arrowBody); this->addChild(arrow); return true; } bool MainScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { action = MoveBy::create(1.0f, Vec2(0, 200)); arrowBody->setDynamic(true); arrow->runAction(action); return true; } bool MainScene::onContactBegin(cocos2d::PhysicsContact &contact) { PhysicsBody *a = contact.getShapeA()->getBody(); PhysicsBody *b = contact.getShapeB()->getBody(); if ( (1 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask() ) || ( 2 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask() ) ) { arrowBody->setDynamic(false); } return true; } \)What is meant by local scope? I did not fully understand. Can you explain via code file please. This is my full .cpp file:
#include "MainScene.h" #include "MyBodyParser.h" #include "SimpleAudioEngine.h" USING_NS_CC; Scene* MainScene::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_NONE); auto layer = MainScene::create(); layer->setPhysicsWorld(scene->getPhysicsWorld()); scene->addChild(layer); return scene; } bool MainScene::init() { if ( !Scene::init() ) { return false; } LayerColor *_bgColor = LayerColor::create(Color4B(0, 191, 255,230)); this->addChild(_bgColor, -10); auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(MainScene::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(MainScene::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); auto edgeNode = Node::create(); edgeNode->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); edgeNode->setPhysicsBody(edgeBody); this->addChild(edgeNode); auto circle = Sprite::create("circle.png"); circle->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); circle->runAction(RepeatForever::create(RotateBy::create(3.0f, 360))); MyBodyParser::getInstance()->parseJsonFile("circle.json"); auto circleBody = MyBodyParser::getInstance()->bodyFormJson(circle, "circle", PhysicsMaterial(0, 0, 1)); if(circleBody != nullptr) { circleBody->setDynamic(false); circle->setPhysicsBody(circleBody); } //this->addChild(circle); auto apple = Sprite::create("apple.png"); apple->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); auto appleBody = PhysicsBody::createCircle(apple->getContentSize().width / 2, PhysicsMaterial(0, 0, 0)); appleBody->setCollisionBitmask(1); appleBody->setContactTestBitmask(true); appleBody->setDynamic(false); apple->setPhysicsBody(appleBody); this->addChild(apple); auto cloud1 = Sprite::create("cloud.png"); cloud1->setPosition(Vec2(origin.x + 25, origin.y + visibleSize.height / 2)); this->addChild(cloud1); auto cloud2 = Sprite::create("cloud.png"); cloud2->setPosition(Vec2(origin.x + 145, origin.y + 300)); this->addChild(cloud2); arrow = Sprite::create("arrow.png"); arrow->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 6)); arrowBody = PhysicsBody::createBox(arrow->getContentSize(), PhysicsMaterial(0, 0, 0)); arrowBody->setCollisionBitmask(2); arrowBody->setContactTestBitmask(true); arrowBody->setDynamic(false); arrow->setPhysicsBody(arrowBody); this->addChild(arrow); return true; } bool MainScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { action = MoveBy::create(1.0f, Vec2(0, 200)); arrowBody->setDynamic(true); arrow->runAction(action); return true; } bool MainScene::onContactBegin(cocos2d::PhysicsContact &contact) { PhysicsBody *a = contact.getShapeA()->getBody(); PhysicsBody *b = contact.getShapeB()->getBody(); if ( (1 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask() ) || ( 2 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask() ) ) { arrowBody->setDynamic(false); } return true; }
And my Header file:
#ifndef __MAIN_SCENE_H__ #define __MAIN_SCENE_H__ #include "cocos2d.h" #include "MyBodyParser.h" class MainScene : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(MainScene); bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event); cocos2d::Sprite *arrow; cocos2d::MoveBy *action; cocos2d::PhysicsBody *arrowBody; cocos2d::PhysicsWorld *physicsWorld; void setPhysicsWorld(cocos2d::PhysicsWorld *world) {physicsWorld = world;}; bool onContactBegin(cocos2d::PhysicsContact &contact); }; #endif // __MAIN_SCENE_H__
-
Emirhan YılmazLikes 0
What is meant by local scope? I did not fully understand. Can you explain via code file please. This is my full .cpp file:
#include "MainScene.h" #include "MyBodyParser.h" #include "SimpleAudioEngine.h" USING_NS_CC; Scene* MainScene::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_NONE); auto layer = MainScene::create(); layer->setPhysicsWorld(scene->getPhysicsWorld()); scene->addChild(layer); return scene; } bool MainScene::init() { if ( !Scene::init() ) { return false; } LayerColor *_bgColor = LayerColor::create(Color4B(0, 191, 255,230)); this->addChild(_bgColor, -10); auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto listener = EventListenerTouchOneByOne::create(); listener->onTouchBegan = CC_CALLBACK_2(MainScene::onTouchBegan, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this); auto contactListener = EventListenerPhysicsContact::create(); contactListener->onContactBegin = CC_CALLBACK_1(MainScene::onContactBegin, this); this->getEventDispatcher()->addEventListenerWithSceneGraphPriority(contactListener, this); auto edgeNode = Node::create(); edgeNode->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); edgeNode->setPhysicsBody(edgeBody); this->addChild(edgeNode); auto circle = Sprite::create("circle.png"); circle->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); circle->runAction(RepeatForever::create(RotateBy::create(3.0f, 360))); MyBodyParser::getInstance()->parseJsonFile("circle.json"); auto circleBody = MyBodyParser::getInstance()->bodyFormJson(circle, "circle", PhysicsMaterial(0, 0, 1)); if(circleBody != nullptr) { circleBody->setDynamic(false); circle->setPhysicsBody(circleBody); } //this->addChild(circle); auto apple = Sprite::create("apple.png"); apple->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height - 80)); auto appleBody = PhysicsBody::createCircle(apple->getContentSize().width / 2, PhysicsMaterial(0, 0, 0)); appleBody->setCollisionBitmask(1); appleBody->setContactTestBitmask(true); appleBody->setDynamic(false); apple->setPhysicsBody(appleBody); this->addChild(apple); auto cloud1 = Sprite::create("cloud.png"); cloud1->setPosition(Vec2(origin.x + 25, origin.y + visibleSize.height / 2)); this->addChild(cloud1); auto cloud2 = Sprite::create("cloud.png"); cloud2->setPosition(Vec2(origin.x + 145, origin.y + 300)); this->addChild(cloud2); arrow = Sprite::create("arrow.png"); arrow->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 6)); arrowBody = PhysicsBody::createBox(arrow->getContentSize(), PhysicsMaterial(0, 0, 0)); arrowBody->setCollisionBitmask(2); arrowBody->setContactTestBitmask(true); arrowBody->setDynamic(false); arrow->setPhysicsBody(arrowBody); this->addChild(arrow); return true; } bool MainScene::onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { action = MoveBy::create(1.0f, Vec2(0, 200)); arrowBody->setDynamic(true); arrow->runAction(action); return true; } bool MainScene::onContactBegin(cocos2d::PhysicsContact &contact) { PhysicsBody *a = contact.getShapeA()->getBody(); PhysicsBody *b = contact.getShapeB()->getBody(); if ( (1 == a->getCollisionBitmask() && 2 == b->getCollisionBitmask() ) || ( 2 == a->getCollisionBitmask() && 1 == b->getCollisionBitmask() ) ) { arrowBody->setDynamic(false); } return true; }
and my header file:
#ifndef __MAIN_SCENE_H__ #define __MAIN_SCENE_H__ #include "cocos2d.h" #include "MyBodyParser.h" class MainScene : public cocos2d::Scene { public: static cocos2d::Scene* createScene(); virtual bool init(); CREATE_FUNC(MainScene); bool onTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event); cocos2d::Sprite *arrow; cocos2d::MoveBy *action; cocos2d::PhysicsBody *arrowBody; cocos2d::PhysicsWorld *physicsWorld; void setPhysicsWorld(cocos2d::PhysicsWorld *world) {physicsWorld = world;}; bool onContactBegin(cocos2d::PhysicsContact &contact); }; #endif // __MAIN_SCENE_H__
-
Sonar Systems adminLikes 0
Scene variable has a local scope as it is only usable in that method not outside of it.
Login to reply