Forum > C++ > Accelerometer Thread

Accelerometer Thread

By pyrosphere Posted 2012-05-13 03:19 Comments 1
  • Posts: 3


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?

  • Posts: 28

#1 RE: 2012-07-11 18:53

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

Loggin to reply

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps