2011-10-27 31 views
0

我试图制作一个游戏,用户应该在屏幕上上下拖动一个精灵,避免传入障碍物。最后一个回答here帮助我在屏幕上拖动精灵,但是我想设置精灵可以移动的最大速度(希望看起来自然而然的加速/减速),所以它不会太容易避免物体。在cocos2d中为iPhone拖动精灵 - 最大速度为

有没有人知道我可以如何修改代码来实现这一点,或者有没有另一种方式呢?

谢谢:)

回答

2

你需要保持CGPoint destinationPosition变量,它是你的手指的位置和使用更新循环改变它的位置:

-(void) update:(ccTime) dt 
{ 
    CGPoint currentPosition = draggableObject.position.x; 
    if (destination.x != currentPosition.x) 
    { 
     currentPosition.x += (destination.x - currentPosition.x)/5.0f; // This 5.0f is how fast you want the object to move to it's destination 
    } 
    if (destination.y != currentPosition.y) 
    { 
     currentPosition.y += (destination.y - currentPosition.y)/5.0f; 
    } 
    draggableObject.postion = currentPosition; 
} 

if S,你可能想要检查对象是否彼此靠近,而不是完全相同的数字以允许舍入错误。

+0

甜!像魅力一样工作,谢谢! :) – Jambaman

0

你只需要有一个if语句在你正在使用的任何时间表更新,如时间或触摸,或其他。

我假设你有x/y速度?就在您的更新声明中,无论您的加速度在哪里 -

if(acceleration.x > 20){ 
acceleration.x = 20; 
} 

if(acceleration.y > 20){ 
acceleration.y = 20; 
}