-
Emirhan YılmazLikes 0Problem Description
I set physics body to drawNode when touch ended but it does not fall down and it does not collide with the edge.
It’s my code:
#include “MainScene.h” #include “SimpleAudioEngine.h” USING_NS_CC; Scene* MainScene::createScene() { auto scene = Scene::createWithPhysics(); scene->getPhysicsWorld()->setDebugDrawMask(PhysicsWorld::DEBUGDRAW_ALL); auto layer = MainScene::create(); layer->setPhysicsWorld(scene->getPhysicsWorld()); scene->addChild(layer); return scene; } bool MainScene::init() { if ( !Layer::init() ) { return false; } auto visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); auto touchListener = EventListenerTouchOneByOne::create(); touchListener->onTouchBegan = CC_CALLBACK_2(MainScene::myTouchBegan, this); touchListener->onTouchMoved = CC_CALLBACK_2(MainScene::myTouchMoved, this); this->_eventDispatcher->addEventListenerWithSceneGraphPriority(touchListener, this); drawNode = DrawNode::create(); this->addChild(drawNode); auto edge = Node::create(); edge->setPosition(Vec2(origin.x + visibleSize.width / 2, origin.y + visibleSize.height / 2)); auto edgeBody = PhysicsBody::createEdgeBox(visibleSize, PHYSICSBODY_MATERIAL_DEFAULT, 3); edge->setPhysicsBody(edgeBody); this->addChild(edge); return true; } bool MainScene::myTouchBegan(cocos2d::Touch *touch, cocos2d::Event *event) { MainScene::lastTouchPos = drawNode->convertTouchToNodeSpace(touch); return true; } void MainScene::myTouchMoved(cocos2d::Touch *touch, cocos2d::Event *event) { Vec2 touchPos = drawNode->convertTouchToNodeSpace(touch); drawNode->drawSegment(lastTouchPos, touchPos, 4, Color4F::WHITE); lastTouchPos = touchPos; } void MainScene::myTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event) { body = PhysicsBody::createBox(drawNode->getContentSize(), PhysicsMaterial(0, 0, 1)); drawNode->setPhysicsBody(body); }
-
Sonar Systems adminLikes 0
Do you have gravity enabled?
-
Emirhan YılmazLikes 0
Yes I have enabled but still don’t work.
-
Emirhan YılmazLikes 0
Which I should write PhysicsBody::createBox, PhysicsBody::createCircle??
-
Sonar Systems adminLikes 0
Have you watched our phsyics series?
-
Emirhan YılmazLikes 0
Yes
-
Emirhan YılmazLikes 0
But I don’t see anything about this topic
-
Sonar Systems adminLikes 0
Try using the code form the series when the gravity worked and modify it to adapt to your node.
-
Emirhan YılmazLikes 0
But I cannot add gravity to body because physics is not active.there is no relation with gravity.But when I add sprite instead drawNode it’s work?
-
Sonar Systems adminLikes 0
You need to attached a physics body to your object.
Login to reply