Unable to get accelerometer values in version 3.0

even in 3.2 callback function is not getting called.
can anyone help?

Since more users are having this frustration I asked our dev team to take a look at the bug report that was mentioned above.

There is a test case in NewEventDispatcherTest.cpp for accelerometer. And it works as expected.
Have your enabled accelerometer

Device::setAccelerometerEnabled(true);

And i use cpp-empty-test to test it. It can work with Layer too.
What i did is:

  1. add virtual void onAcceleration(cocos2d::Acceleration* acc, cocos2d::Event* unused_event); in HelloWorldScene.h

  2. invoke setAccelerometerEnabled(true); in HelloWorld::init()

  3. implement onAcceleration like this

void HelloWorld::onAcceleration(cocos2d::Acceleration *acc, cocos2d::Event *unused_event)
{
    log("accelerometer");
}

@zhangxm thanks for the reply.
I got my code working by calling this->setAccelerometerEnabled(true);
instead of Device::setAccelerometerEnabled(true);
:smile:

Device::setAccelerometerEnabled(true); just calls the native functions, but does not set the listener, if you have never set them. If they wer set in the first place, you can just call Device::setAccelerometerEnabled(true);

The following code would remove the listeners, even when the accelerometer was never added before.
Shouldn’t the code test, if there is something to be removed in the first place, so that the code is consistent and not wasting unnecessary cycles?

On the other side, the code is not called many times throughout a game.

/// isAccelerometerEnabled setter
void Layer::setAccelerometerEnabled(bool enabled)
{
    if (enabled != _accelerometerEnabled)
    {
        _accelerometerEnabled = enabled;

        Device::setAccelerometerEnabled(enabled);

        _eventDispatcher->removeEventListener(_accelerationListener);
        _accelerationListener = nullptr;

        if (enabled)
        {
            _accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(Layer::onAcceleration, this));
            _eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);
        }
    }
}

@iQD I use a similar method to this. But find, I get random delays in acceleration…

I feel the acceleration was much easier/responsive under cocos2d-x v2.2.2 (my last version), and cocos2d.

I even output acc->x or acc->y to see the values, just seems very unresponsive at the best of times. Especially when you want to control the movement of an object. I can tilt the device to it’s maximum… it still takes ages to register. Especially when applying it to a Box2d body applyLinearImpulse for instance.

Some thing just doesn’t feel right.

Now all events(include accelerometer event) are dispatched by EventDispatcher. So it may have delay. We will try to find how to fix it.

I have have success getting the accelerometer data with this code called from the in the scene:

_accelerometerEnabled = true;
Device::setAccelerometerEnabled( true );
_accelerationListener = EventListenerAcceleration::create(CC_CALLBACK_2(GSFRPlayScene::onAcceleration, this));
_eventDispatcher->addEventListenerWithSceneGraphPriority(_accelerationListener, this);

which calls method:

void GSFRPlayScene::onAcceleration(Acceleration* acc, Event* unused_event)
{
  _lastAcceleration.x = acc->x;
  _lastAcceleration.y = acc->y;
  _lastAcceleration.z = acc->z;
}

But frequently the output is super slow to respond, seems like its almost 2-3 second delayed, and sometimes requires a restart of the app to get the data consistently flowing. It’s so inconsistent that I have to consider taking out this functionality which was a selling point to our client.

What device did you test on?
And how to reproduce it?

Well i have galaxy s4 running 4.4.2 and using cocos2dx-3.2 and No delay at all

@smitpatel88
Thanks for sharing.

@hawkwood
Will you checkout it in Cocos2dxAccelerometer.java.
Cocos2dxAccelerometer.onSensorChanged() will receive accelerometer event from system.
If it is slow, then may be you can invoke Cocos2dxAccelerometer.setInterval() to have a try.

I have the same and I get a delay/pause. Perhaps you are just taking the Accelerometer values as received?? I also get the same on iPad mini (non retina). I am however getting the acc->x value and then applying it to a Box2d Linear Impulse. Which in all fairness, shouldn’t matter.

Once you’ve set the dispatcher and listened to onAcceleration to get the direction. It’s simply applying this value to an impulse…

Actually I have been testing on an iPhone 5, not an Android, so the java files are irrelevant. I cannot consistently reproduce the effect, and is seems to happen more when I’m in debug mode running through Xcode. I will keep an eye on it and post if I can find a regularly reproducible issue.

Oh, i thought you met problem on Android.
I will try it on iOS device.

first of all i tried putting log and its printing constantly.
Yes, i am applying acc->x directly but with normal sprite to change angle.

I think for me it was an issue of too much data coming in so it was slowing down in processing. I set the interval to 0.025 and now it is behaving better. FYI this was more notable an issue on an iPad 4.

I think you are referring to this: Accelerometer data lagging issue ?

Where is the place to write above code?