在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中倾斜呢?
发布于 2020-03-13 23:19:42
加速度计API沿固定的轴线工作。如果你想把它当作总是在风景中,你需要使用y轴来代替。
https://developer.apple.com/documentation/coremotion/getting_raw_accelerometer_events
https://stackoverflow.com/questions/60672834
复制相似问题