2

我无法让加速度计在 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;
}
4

2 回答 2

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
于 2012-11-13T17:58:47.230 回答
1

这个问题的答案

cocos2d 游戏中加速度计使用的最佳方式。

于 2012-11-14T11:49:40.023 回答