2012-11-13 45 views
2

我无法让加速度计在使用cocos2d的横向模式下正常工作。一切都完美的肖像,但无法弄清楚如何翻转它正确的工作在风景。这里是我的代码:Cocos2d加速度计风景

-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration 
{ 

float deceleration = 0.0f; 
float sensativity = 15.0f; 
float maxVelocity = 100; 

playerVelocity.x = playerVelocity.x * deceleration + acceleration.x * sensativity; 
playerVelocity.y = playerVelocity.y * deceleration + acceleration.y * sensativity; 

if (playerVelocity.x > maxVelocity) 
{ 
    playerVelocity.x = maxVelocity; 
} 
else if (playerVelocity.x < - maxVelocity) 
{ 
    playerVelocity.x = - maxVelocity; 
} 

if (playerVelocity.y > maxVelocity) 
{ 
    playerVelocity.y = maxVelocity; 
} 
else if (playerVelocity.y < - maxVelocity) 
{ 
    playerVelocity.y = - maxVelocity; 
} 

} 

-(void) update:(ccTime) delta 
{ 
CGPoint pos = player.position; 

pos.x += playerVelocity.x; 
pos.y += playerVelocity.y; 

CGSize screeSize = [[CCDirector sharedDirector]winSize]; 

float imageHalf = [player texture].contentSize.width * 0.5f; 

float leftLimit = imageHalf; 
float rightLimit = screeSize.width - imageHalf -20; 

float bottomLimit = imageHalf; 
float topLimit = screeSize.height - imageHalf - 20; 

if (pos.y < bottomLimit) { 
    pos.y = bottomLimit; 
} 
else if (pos.y > topLimit){ 
    pos.y = topLimit; 
} 

if (pos.x < leftLimit) { 
    pos.x = leftLimit; 
} 
else if (pos.x > rightLimit){ 
    pos.x = rightLimit; 
} 

player.position = pos; 
} 

回答

3

在横向方向上,加速计x和y值被翻转。此外,在界面方向景观权利中,x是负数。在景观离开,y为负:

// landscape right: 
position.x = -acceleration.y 
position.y = acceleration.x 

// landscape left: 
position.x = acceleration.y 
position.y = -acceleration.x 
+0

谢谢你,我知道我很接近,我试过很多的组合,以翻转最终获得自己乱七八糟的! –