-
koko82Likes 0Problem Description
I am trying to get a edge physics boundary box working however when I touch the node and push it up it then disappears off the screen. Not sure why this is happening when using the following code. Is nodespace the wrong one to use for this. Any help would be greatly appreciated...
bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !Layer::init() ) { return false; } Size visibleSize = Director::getInstance()->getVisibleSize(); Vec2 origin = Director::getInstance()->getVisibleOrigin(); Size size = Director::getInstance()->getVisibleSize(); // Physics edge body auto edgeBody = PhysicsBody::createEdgeBox( visibleSize , PhysicsMaterial( 3, 1, 0 ), 3 ); auto edgeNode = Node::create(); edgeNode ->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y ) ); edgeNode->setPhysicsBody( edgeBody ); this->addChild( edgeNode ); // Test Sprite sprite2 = Sprite::create( "CloseNormal.png" ); sprite2->setPosition( Point( visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y + 200 ) ); auto spriteBody = PhysicsBody::createBox( sprite2->getContentSize( ), PhysicsMaterial( 0, 1, 0 ) ); spriteBody->setCollisionBitmask(555); spriteBody->setContactTestBitmask(true); sprite2->setPhysicsBody( spriteBody ); this->addChild( sprite2 ); auto listener1 = EventListenerTouchOneByOne::create(); // When "swallow touches" is true, then returning 'true' from the onTouchBegan method will "swallow" the touch event, preventing other listeners from using it. listener1->setSwallowTouches(true); listener1->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this); listener1->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this); listener1->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this); _eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, sprite2); } bool HelloWorld::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) { auto target = static_cast<Sprite*>(event->getCurrentTarget()); Point locationInNode = target->convertToNodeSpace(touch->getLocation()); Size s = target->getContentSize(); Rect rect = Rect(0, 0, s.width, s.height); if (rect.containsPoint(locationInNode) && target == sprite2) { // log("getContentSize... x = %f, y = %f", s., s.height); log("sprite two has been touched up"); log("sprite began... x = %f, y = %f", locationInNode.x, locationInNode.y); target->setOpacity(80); return true; } return false; }; void HelloWorld::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) { auto target = static_cast<Sprite*>(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); };
-
Sonar Systems adminLikes 0
At what point does it disappear off the screen,what exactly is happening.
This reply has been verified.
-
koko82Likes 0
When I drag sprite on red edge I can push it out of screen I guess its this code as no boundary is set on dragging on screen or there is not enough resistance on the edge border?!?
void HelloWorld::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) { auto target = static_cast<Sprite*>(event->getCurrentTarget()); target->setPosition(target->getPosition() + touch->getDelta()); };
This reply has been verified.
-
Sonar Systems adminLikes 0
You need checks inside the move function to prevent it from going outside.
This reply has been verified.
-
koko82Likes 0
I tried that if it goes beyond, visible size it then gets stuck. For example :
if ( (target->getPositionY() <= visibleSize.width) && (target->getPositionX() <= visibleSize.height) { target->setPosition(target->getPosition() + touch->getDelta()); }
This reply has been verified.
-
Sonar Systems adminLikes 0
Try setting the position to a little before the edge node so it doesn’t get stuck.
This reply has been verified.
-
koko82Likes 1
Thanks for the suggestion, but surprised cocos2dx doesn't have a ready made function for this prevention. Do you know of any?
This reply has been verified.
-
Sonar Systems adminLikes 0
Not that we know of sorry. Something we wish it had as well.
This reply has been verified.
-
koko82Likes 0
Oh ok! Even if I reduce it down it still gets stuck with above code :/
This reply has been verified.
-
Sonar Systems adminLikes 0
When you say stuck, do you mean you can’t move it anymore?
This reply has been verified.
-
koko82Likes 0
Yes sorry should have been more clear
This reply has been verified.
-
Sonar Systems adminLikes 0
Try not adding the edge node and see what happens?
This reply has been verified.
-
koko82Likes 0
Ok, but would need the edgenode for Physics?
This reply has been verified.
-
Sonar Systems adminLikes 0
Try it without the edge node so we can see how it goes and narrow down the problem and then come to a solution.
-
koko82Likes 0
Tried it still gets stuck
-
koko82Likes 0
I’ve added physics boundary and whats quite intresting is that intially the edgebox provides resistance and bounces the sprite back in however if you drag hard enough it pushes it outside the edge physics boundary box across the other side...
This reply has been verified.
-
Sonar Systems adminLikes 0
You will be able to push it through, what game scenario are you trying to achieve?
-
koko82Likes 0
Oh right? Any reason why and how to stop it. Trying to drag sprite to edge to let it bounce off the physics edge box.
This reply has been verified.
-
Sonar Systems adminLikes 0
Try making a thicker edge box.
This reply has been verified.
-
koko82Likes 0
Works a treat, tried setting it at 10 still had issues. But tried it at 80 and works well but had to pushout width/height. Many thanks for the help..
-
Sonar Systems adminLikes 0
Great to hear :D
They should have a option to prevent it from going out like a toggle method.
This reply has been verified.
-
koko82Likes 0
Totally would make sense!
Login to reply