0

我正在开发我想在加速度计倾斜方向移动 CCSprite 的游戏。我参考了本教程http://www.raywenderlich.com/457/intro-to-box2d-with-cocos2d-tutorial-bouncing-balls但我不清楚。请任何人帮助我。

提前致谢。

4

1 回答 1

0

参考这个解决方案。我在横向游戏中将精灵从左向右移动时使用了相同的方法。:请参阅此处的答案

横向模式的唯一变化是使用加速度计 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);

}
于 2012-09-12T09:51:16.480 回答