首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在iPhone6和iPhoneX中互换的Cocos2d-x加速度计x,y

在iPhone6和iPhoneX中互换的Cocos2d-x加速度计x,y
EN

Stack Overflow用户
提问于 2020-03-13 23:06:52
回答 1查看 37关注 0票数 0

在iPhoneX中工作完美。在iPhone6中,当我在横向模式下倾斜时,加速度计的值甚至在肖像游戏中也会出现。如何在纵向倾斜中获得加速度计的值?游戏在肖像里。但是加速度计把它当做风景..

以下是代码:

代码语言:javascript
复制
    Device::setAccelerometerEnabled(true);

    mAccelerometerListener = EventListenerAcceleration::create(CC_CALLBACK_2( GBGameScene::onAcceleration, this));
    _eventDispatcher->addEventListenerWithSceneGraphPriority(mAccelerometerListener, this);

加速度计代码:

代码语言:javascript
复制
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中倾斜呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-13 23:19:42

加速度计API沿固定的轴线工作。如果你想把它当作总是在风景中,你需要使用y轴来代替。

https://developer.apple.com/documentation/coremotion/getting_raw_accelerometer_events

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60672834

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档