2011-08-23 60 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

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