2011-08-23 53 views
0

(我使用iphone cocos2d的)试图以恒定的速度平稳地旋转我的精灵。但它降低速度

首先,我将解释我的代码:(重要部件)

-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch * touch = [touches anyObject]; 

    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position))); 

    plane.dstAngle = dstAngle; 
    } 
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 
    UITouch * touch = [touches anyObject]; 

    CGPoint location = [touch locationInView:[touch view]]; 
    location = [[CCDirector sharedDirector] convertToGL:location]; 

    float dstAngle = CC_RADIANS_TO_DEGREES(-ccpToAngle(ccpSub(location, plane.position))); 

    plane.dstAngle = dstAngle; } 

如果我触摸屏幕,我计算角度在我的飞机和触摸位置之间。

现在我设置飞机的“目标”角度。 我的“飞机”是一个子类。

以我的头文件我有这些浮点值:

//this is in my plane subclass 
float diffAngle_; 
float startAngle_; 
float dstAngle_; 
float smoothRotationSpeed_; 

编辑:以我的层的初始化我设置“smoothRotationSpeed”的值。 “rotationTick”方法是预定的。 我有一个勾的方法来旋转平面(也在平面子类)

-(void)rotateTick:(ccTime)dt { 

    startAngle_ = [self rotation]; 
    if (startAngle_ > 0) 
     startAngle_ = fmodf(startAngle_, 360.0f); 
    else 
     startAngle_ = fmodf(startAngle_, -360.0f); 

    diffAngle_ =dstAngle_ - startAngle_; 
    if (diffAngle_ > 180) 
     diffAngle_ -= 360; 
    if (diffAngle_ < -180) 
     diffAngle_ += 360; 

    [self setRotation: startAngle_ + diffAngle_ * dt]; //smooth rotate 

} 
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed { 
    [self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed 
} 

现在的问题是,如果我正确地触摸精灵旋转的位置,但起初它旋转的有点快,比它失去它的速度..

越接近它的目标角度它的动作更慢......

但我想它以恒定速度移动。这个“常量”速度是在“smoothRotationSpeed”值中定义的,但是如何在我的代码中实现它?

如果我将间隔设置为“smoothRotationSpeed”值,它将不平滑。它会滞后。

有人愿意帮助我吗?

回答

2

根据目标和当前角度之间的差异,您正在增加精灵的旋转,所以当前角度越接近目标角度,您应用于当前旋转的增量越小。但从你所说的话来看,这不是你想要的。所以你可以简单地做这样的事情:

-(void)rotateTick:(ccTime)dt { 

    startAngle_ = [self rotation]; 
    if (startAngle_ > 0) 
     startAngle_ = fmodf(startAngle_, 360.0f); 
    else 
     startAngle_ = fmodf(startAngle_, -360.0f); 

    diffAngle_ =dstAngle_ - startAngle_; 
    if (diffAngle_ > 180) 
     diffAngle_ -= 360; 
    if (diffAngle_ < -180) 
     diffAngle_ += 360; 

    if (fabs(diffAngle_) < smoothRotationSpeed_*dt) // finished rotation 
    { 
     [self setRotation: dstAngle_]; // go to the end position 
    } 
    else if (diffAngle_ > 0) 
    { 
     [self setRotation: startAngle_ + smoothRotationSpeed_* dt]; //smooth rotate 
    } 
    else 
    { 
     [self setRotation: startAngle_ - smoothRotationSpeed_* dt]; //smooth rotate 
    } 
} 
-(void)setSmoothRotationSpeed:(float)smoothRotationSpeed 
{ 
    smoothRotationSpeed_ = smoothRotationSpeed; 
    [self schedule:@selector(rotateTick:)]; //this will be called if the "smoothRotationSpeed" value is changed 
} 
+0

谢谢...... :) – cocos2dbeginner

+0

救了我的日子谢谢你的好工作 –