在我的应用程序中,我将游戏设置为横向模式,允许用户能够在游戏自动旋转的情况下翻转设备以匹配角度。当用户将设备向右倾斜时,英雄将向右移动,当向左倾斜时,英雄将向左移动。
我已经找到了这个部分,但问题出现在用户翻转iDevice时(因为耳机插孔或扬声器的位置),应用程序是自动旋转的,但是滚动设置英雄的位置.x要倒置。向左倾斜让英雄向右倾斜,向右倾斜让英雄向左倾斜。在翻转iDevice时查看accelY值的NSLogs后,旋转时似乎无法反转position.x?
我想这更像是一个数学问题。
这是我的代码:
-(void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
#define kFilteringFactor 0.75
static UIAccelerationValue rollingX = 0, rollingY = 0, rollingZ = 0;
rollingX = (acceleration.x * kFilteringFactor) + (rollingX * (1.0 - kFilteringFactor));
rollingY = (acceleration.y * kFilteringFactor) + (rollingY * (1.0 - kFilteringFactor));
rollingZ = (acceleration.z * kFilteringFactor) + (rollingZ * (1.0 - kFilteringFactor));
float accelX = rollingX;
float accelY = rollingY;
float accelZ = rollingZ;
NSLog(@"accelX: %f, accelY: %f, accelZ: %f", accelX, accelY, accelZ);
CGSize winSize = [CCDirector sharedDirector].winSize;
#define kRestAccelX 0.6
#define kShipxMaxPointsPerSec (winSize.height * 1.0)
#define kMaxDiffX 0.2
#define kRestAccelY 0.0
#define kShipyMaxPointsPerSec (winSize.width * 0.5)
#define kMaxDiffY 0.2
float accelDiffX = kRestAccelX - ABS(accelX);
float accelFractionX = accelDiffX / kMaxDiffX;
float pointsPerSecX = kShipxMaxPointsPerSec * accelFractionX;
float accelDiffY = -accelY;
float accelFractionY = accelDiffY / kMaxDiffY;
float pointsPerSecY = kShipyMaxPointsPerSec * accelFractionY;
_shipPointsPerSecX = pointsPerSecY;
_shipPointsPerSecY = pointsPerSecX;
CCLOG(@"accelX: %f, pointsPerSecX: %f", accelX, pointsPerSecX);
CCLOG(@"accelY: %f, pointsPerSecY: %f", accelY, pointsPerSecY);
}
-(void)updateShipPos:(ccTime)dt
{
CGSize winSize = [CCDirector sharedDirector].winSize;
float maxX = winSize.width - _ship.contentSize.width;
float minX = _ship.contentSize.width / 2;
float maxY = winSize.height - _ship.contentSize.height / 2;
float minY = _ship.contentSize.height / 2;
float newX = _ship.position.x + (_shipPointsPerSecX * dt);
float newY = _ship.position.y + (_shipPointsPerSecY * dt);
newX = MIN(MAX(newX, minX), maxX);
newY = MIN(MAX(newY, minY), maxY);
_ship.position = ccp(newX, newY);
}
我的想法是,我试图让它在向右倾斜时为pointsPerSecY为正,在向左倾斜时为负。问题是当用户翻转设备时,由于设备被翻转,它们被倒置。有没有一种方法可以使它在向右倾斜时保持正向,在向左倾斜时保持正向,无论方向如何,在横向翻转时都会翻转?
答案 0 :(得分:1)
如何做这样的事情?
if ([[UIApplication sharedApplication] statusBarOrientaion]==UIInterfaceOrientationLandscapeLeft) {
pointsPerSecY *= -1;
}