0

我有一个 CCSprite(其中我有一个 b2body);范围是创建一个随身体移动到一个位置并返回的精灵;

我以这种方式创建 b2body:

 if (_body) 
    {
        _world->DestroyBody(_body);
    }


    b2BodyDef bd;
    bd.type = b2_kinematicBody;


    _body = _world->CreateBody(&bd);

    b2PolygonShape shape;

    b2Vec2 _p1, _p2 , _p1sub,_p2sub;

    _p1 = b2Vec2(point1.x/PTM_RATIO,point1.y/PTM_RATIO);
    _p2 = b2Vec2(point2.x/PTM_RATIO,point2.y/PTM_RATIO);


    shape.SetAsEdge(_p1, _p2);
    _body->CreateFixture(&shape, 0);

创建主体后,我初始化动作:

id a1 = [CCMoveBy actionWithDuration:1 position:ccp(0,-self.position.y+destinationPoint.y)];
        id action2 = [CCRepeatForever actionWithAction:
                      [CCSequence actions: [[a1 copy] autorelease], [a1 reverse], nil]
                      ];
        [self runAction:action2];

在每一帧上,我都会更新身体位置

 _body->SetTransform(b2Vec2(self.position.x/PTM_RATIO,self.position.y/PTM_RATIO), 0);

问题是它们都被绘制了,但是精灵在正确的位置,而身体却没有;动作是连贯的

仅供参考,我在整个游戏中都使用 PTM_RATIO,所以这肯定不是问题

4

1 回答 1

1

请记住,精灵的位置将是其父位置的偏移量。如果父节点不是 CCNode、CCScene 或 CCLayer(或它们的位置不是 0,0),则精灵将被父节点的位置偏移。

For example, a sprite (let's call it S) with dimensions (contentSize) of 100,100 is located at position 0,0. Now you add a child node to S. The child node's position (unless modified) will be at -50,-50 due to the fact that child nodes orient themselves on their parent's lower left corner instead of the parent's actual position. This frequently throws even the most experienced cocos2d programmers off guard.

于 2012-05-11T15:40:14.333 回答