2012-06-17 22 views
1

我一直在讀一本書上的cocos2d框架的ios5了幾天,並開發了一個小遊戲,這本書通過引導您。要控制在這個遊戲中的精靈,你使用加速度計:移動與觸摸精靈開始,在OBJ鑄造和參數 - C嗎?

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 

    float deceleration = 0.4f; 
    float sensitivity = 6.0f; 
    float maxVelocity = 100; 

    // adjust velocity based on current accelerometer acceleration 
    playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensitivity; 

    // we must limit the maximum velocity of the player sprite, in both directions (positive & negative values) 
    if (playerVelocity.x > maxVelocity) 
    { 
     playerVelocity.x = maxVelocity; 
    } 
    else if (playerVelocity.x < -maxVelocity) 
    { 
     playerVelocity.x = -maxVelocity; 
    } 

    // Alternatively, the above if/else if block can be rewritten using fminf and fmaxf more neatly like so: 
    // playerVelocity.x = fmaxf(fminf(playerVelocity.x, maxVelocity), -maxVelocity); 
} 

現在我想知道如果我可以改變這個代碼,允許精靈仍然加速/沿x軸有所放緩,但寧願使用觸摸輸入比加速度計更快,觸摸所需的時間越長,速度越快?因此,一個觸摸向右慢慢地將精靈移動到這個位置,如果觸摸被釋放,其停止移動,該點。觸摸持續時間越長,精靈移動的速度越快。

是那裏的框架,讓我實現一個旋轉機構,使我的精靈轉動到位置,觸摸是什麼,所以它看起來像它面臨的問題這就是被感動?

回答

1

那麼,afaik theres沒有方法可以確定觸摸的角度,然後相應地旋轉精靈,但是如果你有精靈和觸摸的x和y座標,你可以很容易地自己計算它。

CGPoint spriteCenter; // this should represent the center position of the sprite 
CGPoint touchPoint; //location of touch 

float distanceX = touchPoint.x - spriteCenter.x; 
float distanceY = touchPoint.y - spriteCenter.y; 

float angle = atan2f(distanceY,distanceX); // returns angle in radians 

// do whatever you need to with the angle 

一旦你有角度,你可以設置精靈的旋轉。