Hello,
I was wondering why this is not working.
I have a spriteA moving across spriteB and spirteC. spriteB is a child of a node.
And the intersection is only detected with sprite C, so it seem that intersectRect() doesn’t work with a sprite if is a child.
#include "HelloWorldScene.h"
#include "AudioEngine.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
auto scene = HelloWorld::create();
return scene;
}
bool HelloWorld::init()
{
if ( !Scene::init() )
return false;
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
spriteA = Sprite::create("spriteA.png");
spriteA->setAnchorPoint(Vec2(0,0)); // set anchor to (0, 0) to match spriteB when making child
addChild(spriteA);
nodeB = Node::create();
spriteB = Sprite::create("spriteB.png");
spriteB->setAnchorPoint(Vec2(0, 0)); // set anchor to (0, 0) to avoid any offsets and match spriteB
addChild(nodeB);
nodeB->addChild(spriteB);
spriteC = Sprite::create("spriteC.png");
spriteC->setAnchorPoint(Vec2(0,0));
addChild(spriteC);
spriteA->setPosition(visibleSize.width/4, visibleSize.height/2);
spriteC->setPosition(visibleSize.width/2 + 200, visibleSize.height/2);
nodeB->setPosition(visibleSize/2);
// Local and world pos
CCLOG("%f x local position of spriteB", spriteA->getPosition().x); // Result: 240
CCLOG("%f y local position of spriteB", spriteA->getPosition().y); // Result: 320
CCLOG("%f x local position of spriteB", spriteB->getPosition().x); // Result: 0
CCLOG("%f y local position of spriteB", spriteB->getPosition().y); // Result: 0
auto spriteBWorldPos = spriteB->convertToWorldSpace(Vec2());
CCLOG("%f x world position of spriteB", spriteBWorldPos.x); // Result: 480
CCLOG("%f y world position of spriteB", spriteBWorldPos.y); // Resutl: 320
// Create move to evaluation collision
auto moveAction = MoveTo::create(5, Vec2(800, 320 ));
spriteA->runAction(moveAction);
// Run the loop
scheduleUpdate();
return true;
}
void HelloWorld::update(float dt)
{
if ( spriteA->getBoundingBox().intersectsRect(spriteB->getBoundingBox()) )
CCLOG("Intersection happening with spriteB");
else
CCLOG("No interection with spriteB...");
CCLOG("");
if ( spriteA->getBoundingBox().intersectsRect(spriteC->getBoundingBox()) )
CCLOG("Intersection happening SpriteC");
else
CCLOG("No interection spriteC...");
CCLOG("------------");
CCLOG("");
}
thanks,
R