2012-09-12 32 views

回答

0

請參閱此解決方案。在風景遊戲中,我使用從左到右移動精靈的相同方式。:See answer here

只有橫向模式的更改是使用加速度計y代替x。

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 
    if(self.isGamePaused || self.isGameOver) 
     return; 

    float deceleration = 1.0f, sensitivity = 15.0f, maxVelocity = 450; 

    if(sGame.IsIpad) 
    { 
     sensitivity = 14.0f; 
     maxVelocity = 850; 
     deceleration = 1.5f; 
    } 

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

    AppController *app = (AppController*)[UIApplication sharedApplication].delegate; 

    float velocity_x = self.gameActor.velocity.x ; 

    self.gameActor.accelVal = ABS(acceleration.y); 

    if(app.orient == UIInterfaceOrientationLandscapeLeft) 
    { 
     velocity_x = velocity_x * deceleration + acceleration.y * sensitivity;   
    } 
    else 
    { 
     velocity_x = velocity_x * deceleration + (-acceleration.y) * sensitivity; 
    } 

    //limit the maximum velocity of the player sprite, in both directions (positive & negative values) 
    velocity_x = fmaxf(fminf(velocity_x, maxVelocity), -maxVelocity); 

    self.gameActor.velocity = ccp(velocity_x, self.gameActor.velocity.y); 

}