如何设置速度恒定
spriteBody->setVelocity(Vec2(100,300)); 一段时间后速度就会变慢
最佳答案
有很多解决方案可以满足您的需求。您可以使用操作移动:
auto action = MoveBy::create (duration, Vec2(100,300));
spriteBody->runAction (action);
当您想稍后停止时:
sprite->stopAction (action);
您可以在更新功能中手动控制速度:
void update (float dt)
{
//slow down speed
//velocityVector is a Vec2 of velocity
//slowAmount is a float which contains amount to slow in 1 second
unitVector = velocityVector;
unitVector.normalise();
velocityVector -= unitVector * slowAmount * dt;
//adjust position
sprite->setPosition (getPosition () + velocityVector * dt);
}
或者你可以使用物理体:
sprite->getPhysicsBody()->applyForce(Vec2(100, 300));
如果你想让物理体减速,请设置物理体 Material 的摩擦力
auto material = PHYSICSBODY_MATERIAL_DEFAULT;
material.density = 1.0f;
material.restitution = 0.7f;
material.friction = 0.5f; //set friction here
sprite->setPhysicsBody(PhysicsBody::createBox(sprite->getContentSize(), material));
有关如何处理物理和设置的更多详细信息: http://www.cocos2d-x.org/wiki/Physics
关于cocos2d-iphone - 如何设置为等速速度? (Cocos2d-x),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36826247/