2
所以这是我第一次开发和第一次使用加速度计的练习游戏,所以如果答案很明显,请事先道歉。还想用代码和解释来回答。加速度计倾斜问题
因此,游戏的一部分我有一个球在没有屏幕滚动。球根据倾斜移动。该应用的设备方向仅适用于横向。当我按住手机横向的同时上下倾斜手机时,球会相应移动。 (将球的顶端向下倾斜,将球的底端向下倾斜)。所以那些2倾斜很好。现在,当我将屏幕向左倾斜时,球向右滚动,反之亦然。我想要的是屏幕向左倾斜,球向左转,向右倾斜屏幕,球向右转。这是如何解决我的代码如下。我知道它很简单,可能更改两行。
//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);
}