在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