【问题标题】:Accelerometer Tilt Issue加速度计倾斜问题
【发布时间】:2014-02-11 22:22:48
【问题描述】:

所以这是我正在开发的第一个练习游戏,也是第一次使用加速度计,所以如果答案很明显,请提前道歉。还想用代码和解释来回答。

所以在游戏的一部分,我有一个球在没有屏幕的情况下滚动。球根据倾斜移动。此应用程序的设备方向仅为横向。当我在保持手机横向的同时上下倾斜手机时,球会相应地移动。 (将顶端向下倾斜,球会向上滚动,将底端向下倾斜,球会向下滚动)。所以这两个倾斜很好。现在,当我将屏幕左侧向下倾斜时,球会向右滚动,反之亦然。我想要的是屏幕向左倾斜,球向左倾斜,屏幕向右倾斜,球向右倾斜。这是如何用我下面的代码解决的。我知道它可能就像更改两条线一样简单。

//delegate method
-(void)outputAccelertionData:(CMAcceleration)acceleration
{
    currentMaxAccelX = 0;
    currentMaxAccelY = 0;


    if(fabs(acceleration.x) > fabs(currentMaxAccelX))
    {
        // this needs to be currentMaxAccelY not currentMaxAccelX for those of you thinking this is the solution
        currentMaxAccelY = acceleration.x; 
    }
    if(fabs(acceleration.y) > fabs(currentMaxAccelY))
    {
        // this needs to be currentMaxAccelX not currentMaxAccelY for those of you thinking this is the solution
        currentMaxAccelX = acceleration.y;
    }
}

-(void)update:(CFTimeInterval)currentTime {

    /* Called before each frame is rendered */


    float maxY = 480;
    float minY = 0;


    float maxX = 320;
    float minX = 0;

    float newY = 0;
    float newX = 0;

    //Im pretty sure the problem is in this if statement as this is what deals with the left and right tilt
    if(currentMaxAccelX > 0.05){
        newX = currentMaxAccelX * 10;
    }
    else if(currentMaxAccelX < -0.05){
        newX = currentMaxAccelX*10;
    }
    else{
        newX = currentMaxAccelX*10;
    }

    newY = currentMaxAccelY *10;

    newX = MIN(MAX(newX+self.ball.position.x,minY),maxY);
    newY = MIN(MAX(newY+self.ball.position.y,minX),maxX);


    self.ball.position = CGPointMake(newX, newY);

}

【问题讨论】:

    标签: ios ios7 accelerometer uiaccelerometer


    【解决方案1】:

    所以我解决了这个问题。问题出在我的数学上。而不是乘以 10,我应该乘以负 10 以获得相反的效果。

    if(currentMaxAccelX > 0.05){
            newX = currentMaxAccelX * -10;
        }
        else if(currentMaxAccelX < -0.05){
            newX = currentMaxAccelX*-10;
        }
        else{
            newX = currentMaxAccelX*-10;
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-28
      • 1970-01-01
      • 2017-09-19
      相关资源
      最近更新 更多