我想用CCMove移动身体我正在使用以下代码它就像一块平板我想将它从1px移动到100px然后从100px移回到1px其工作正常如果我只从1x移动到100px但是当我搬回来时,它的表现出乎意料
void MovableBlock::update(float dt) {
b2Vec2 position;
if(isForward) {
positionAnimationImg = ccpAdd(positionAnimationImg, animatableImage->getPosition());
position = point_to_vec(ccp(this->getPositionX()+positionAnimationImg.x,this->getPositionY()));
}
else {
positionAnimationImg = ccpSub(positionAnimationImg, animatableImage->getPosition());
position = point_to_vec(ccp(positionAnimationImg.x-this->getPositionX(),this->getPositionY()));
}
CCLog("%f:%f",this->getBody()->GetTransform().p.x,position.y);
//CCLog("position>> %f:%f",position.x,position.y);
this->getBody()->SetTransform(b2Vec2(position.x,
position.y),
this->getBody()->GetAngle());
animatableImage->setPosition(CCPointZero);
}
答案 0 :(得分:1)
如果你想将CCMove与Box2d体一起使用,那么你附加了一个带有body的ccsprite并将ccmove应用于sprite并根据update方法中的sprite位置变换body。
示例:在Cocos @ d-x
中PTM_RATIO 32;
CCSprite *ball = CCSprite::create("icon.png");
ball->setPosition(ccp(visibleSize.width/5, visibleSize.height/5));
this->addchild(ball);
b2BodyDef bdf ;
bdf.type = b2_dynamicBody;
bdf.position.Set((float)(visibleSize.width/(10*PTM_RATIO)), (float)(visibleSize.height/PTM_RATIO));
b2body hero = _world->CreateBody(&bdf);
hero->SetUserData(ball);
CCMoveBy *action = CCMoveBy::create(.5, CCPointMake(ball->getPositionX(), ball->getPositionY()-10));
CCMoveBy* action_back = (CCMoveBy*)action->reverse();
ball->runAction(CCSequence::create(action, action_back, NULL));
现在在Update方法
CCSprite *sp1 = (CCSprite*)hero->GetUserData();
hero->SetTransform(b2Vec2(sp1->getPositionX()/PTM_RATIO, sp1->getPositionY()/PTM_RATIO), 0);