2012-11-13 48 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

謝謝你,我知道我很接近,我試過很多的組合,以翻轉最終獲得自己亂七八糟的! –