Cocos2d-x iPhone6 和 iPhone X 中的加速度计 x y 互换
Cocos2d-x Accelerometer x y interchanged in iPhone6 and iPhoneX
在 iPhoneX 中完美运行。在 iPhone6 中,当我在横向模式下倾斜时,加速度计值甚至在纵向游戏中也会出现。如何获得纵向倾斜的加速度计值?游戏是纵向的。但是加速度计把它当作风景..
代码如下:
Device::setAccelerometerEnabled(true);
mAccelerometerListener = EventListenerAcceleration::create(CC_CALLBACK_2( GBGameScene::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(mAccelerometerListener, this);
加速度计代码:
void GBGameScene::onAcceleration(Acceleration* acc, Event* event)
{
// Processing logic here
float deceleration = 1.0f;
float maxVelocity = 100;
playerVelocity.x = playerVelocity.x * deceleration + acc->x;
playerVelocity.y = playerVelocity.y * deceleration + acc->y;
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;
}
// Point pos = mGravityBall->getPosition();
// pos.x += playerVelocity.x;
// mGravityBall->setPosition(pos);
}
我认为代码没有问题,因为它在 iphoneX 中运行完美。为什么不倾斜 iPhone6?
加速度计API沿固定轴运行。如果您想将其视为始终处于横向状态,则需要改用 y 轴。
https://developer.apple.com/documentation/coremotion/getting_raw_accelerometer_events
在 iPhoneX 中完美运行。在 iPhone6 中,当我在横向模式下倾斜时,加速度计值甚至在纵向游戏中也会出现。如何获得纵向倾斜的加速度计值?游戏是纵向的。但是加速度计把它当作风景..
代码如下:
Device::setAccelerometerEnabled(true);
mAccelerometerListener = EventListenerAcceleration::create(CC_CALLBACK_2( GBGameScene::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(mAccelerometerListener, this);
加速度计代码:
void GBGameScene::onAcceleration(Acceleration* acc, Event* event)
{
// Processing logic here
float deceleration = 1.0f;
float maxVelocity = 100;
playerVelocity.x = playerVelocity.x * deceleration + acc->x;
playerVelocity.y = playerVelocity.y * deceleration + acc->y;
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;
}
// Point pos = mGravityBall->getPosition();
// pos.x += playerVelocity.x;
// mGravityBall->setPosition(pos);
}
我认为代码没有问题,因为它在 iphoneX 中运行完美。为什么不倾斜 iPhone6?
加速度计API沿固定轴运行。如果您想将其视为始终处于横向状态,则需要改用 y 轴。
https://developer.apple.com/documentation/coremotion/getting_raw_accelerometer_events