Accelerometer Thread

Accelerometer Thread
0.0 0



I was having an issue in a scene, on Android, with the function didAccelerate(). On some devices it sometimes crashed when changing sprites or nodes, as it was running on a different thread as cocos2d-x. I worked out an improvised solution by editing, replacing the call:

        onSensorChanged(x, y, z, event.timestamp);

To this:

        final float fx = x;
        final float fy = y;
        final float fz = z;
        final long ftimestamp = event.timestamp;
        Cocos2dxActivity activity = ((Cocos2dxActivity)mContext);
        Cocos2dxGLSurfaceView surfaceView = (Cocos2dxGLSurfaceView) activity.findViewById(;
        surfaceView.queueEvent(new Runnable() {
            public void run() {
                onSensorChanged(fx, fy, fz, ftimestamp);

It seems to be working but I’m not sure this is a good solution. Did anyone came across this issue and is there a better way to solve it?


EDIT: it’s working fine on all android version I tested on. Thanks a lot.