Ios 加速计倾斜问题

Ios 加速计倾斜问题,ios,ios7,accelerometer,uiaccelerometer,Ios,Ios7,Accelerometer,Uiaccelerometer,这是我开发的第一个练习游戏,也是第一次使用加速度计,所以如果答案很明显,我会提前道歉。也希望回答与代码和解释 所以在游戏的一部分,我有一个球在屏幕上滚动。球根据倾斜度移动。此应用程序的设备方向仅为横向。当我将手机向上和向下倾斜,同时按住手机右侧时,球会相应移动。(顶端向下倾斜,球向上滚动,底端向下倾斜,球向下滚动)。所以这两个倾斜都很好。现在,当我将屏幕左侧向下倾斜时,球向右滚动,反之亦然。我想要的是屏幕向左倾斜,球向左移动,屏幕向右倾斜,球向右移动。如何用下面的代码修复此问题。我知道这很简单,

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

所以在游戏的一部分,我有一个球在屏幕上滚动。球根据倾斜度移动。此应用程序的设备方向仅为横向。当我将手机向上和向下倾斜,同时按住手机右侧时,球会相应移动。(顶端向下倾斜,球向上滚动,底端向下倾斜,球向下滚动)。所以这两个倾斜都很好。现在,当我将屏幕左侧向下倾斜时,球向右滚动,反之亦然。我想要的是屏幕向左倾斜,球向左移动,屏幕向右倾斜,球向右移动。如何用下面的代码修复此问题。我知道这很简单,大概就是换两行

//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);

}
//委托方法
-(无效)OutputAccelerationData:(cAcceleration)加速度
{
currentMaxAccelX=0;
currentMaxAccelY=0;
if(晶圆厂(加速度.x)>晶圆厂(当前最大加速度))
{
//对于那些认为这是解决方案的人来说,这需要是currentMaxAccelY而不是currentMaxAccelX
currentMaxAccelY=加速度.x;
}
if(晶圆厂(加速度.y)>晶圆厂(当前最大加速度))
{
//对于那些认为这是解决方案的人来说,这需要是currentMaxAccelX而不是currentMaxAccelY
currentMaxAccelX=加速度.y;
}
}
-(无效)更新:(CFTimeInterval)currentTime{
/*在渲染每个帧之前调用*/
浮点最大值=480;
float minY=0;
浮点最大值=320;
float minX=0;
float newY=0;
float newX=0;
//我很确定问题出在这个if语句中,因为这是处理左右倾斜的语句
如果(currentMaxAccelX>0.05){
newX=currentMaxAccelX*10;
}
否则如果(currentMaxAccelX<-0.05){
newX=currentMaxAccelX*10;
}
否则{
newX=currentMaxAccelX*10;
}
newY=当前最大加速度*10;
newX=MIN(MAX(newX+self.ball.position.x,minY),MAX);
newY=MIN(MAX(newY+self.ball.position.y,minX),maxX);
self.ball.position=CGPointMake(newX,newY);
}

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

if(currentMaxAccelX > 0.05){
        newX = currentMaxAccelX * -10;
    }
    else if(currentMaxAccelX < -0.05){
        newX = currentMaxAccelX*-10;
    }
    else{
        newX = currentMaxAccelX*-10;
    }
if(currentMaxAccelX>0.05){
newX=当前最大加速度*-10;
}
否则如果(currentMaxAccelX<-0.05){
newX=当前最大加速度*-10;
}
否则{
newX=当前最大加速度*-10;
}