convertToNodeSpace混乱

时间:2015-02-07 17:09:35

标签: cocos2d-x collision-detection

我尝试捕捉2个精灵之间的intersectsRect 看完之后:
http://www.cocos2d-x.org/wiki/Coordinate_System#convertToNodeSpace
我有这种精灵的层次结构

+Root 
|-Child_1
|     |--Child_1_of_Child_1
|-Child_2

在代码中简单

Sprite* root = Sprite::create();
Sprite* Child_1 = Sprite::create();
Sprite* Child_1_of_Child_1 = Sprite::create();
Sprite* Child_2 = Sprite::create();

root->addChild(Child_1);
Child_1->addChild(Child_1_of_Child_1 );
root->addChild(Child_2);

我想捕捉Child_2和Child_1_of_Child_1精灵之间的碰撞。 但我所做的每件事都从未在Child_1_of_Child_1中捕获它 这就是我在更新循环中所拥有的。

        Rect r = Child_2->getBoundingBox();
        Vec2 vr = Child_2->getPosition();
        Vec2 newNodeSpaceVec =  Child_1->convertToNodeSpace(vr);
//not working also 
// Vec2 newNodeSpaceVec =  Child_1->Child_1_of_Child_1->convertToNodeSpace(vr);
        Rect NewRec(newNodeSpaceVec.x,newNodeSpaceVec.y,r.size.width,r.size.height);

        //THIS iS ALLWAYS FALSE 
        if(Child_1->Child_1_of_Child_1->getBoundingBox().intersectsRect(NewRec))
    {
    }

现在我的工作很重要,即使我看到2个精灵碰撞了if if is never true。

1 个答案:

答案 0 :(得分:1)

对于您提供的结构,请尝试使用:

Vec2 child1Pos = Child_1->getParent()->convertToWorldSpace(Child_1->getPosition());