diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java index fe2f676ce4..76df8e0c0c 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java @@ -116,9 +116,10 @@ public class Cocos2dxAccelerometer implements SensorEventListener { final float tmp = x; x = y; y = -tmp; - } - - Cocos2dxAccelerometer.onSensorChanged(x, y, z, pSensorEvent.timestamp); + } + + Cocos2dxGLSurfaceView.queueAccelerometer(x,y,z,pSensorEvent.timestamp); + /* if(BuildConfig.DEBUG) { Log.d(TAG, "x = " + pSensorEvent.values[0] + " y = " + pSensorEvent.values[1] + " z = " + pSensorEvent.values[2]); @@ -132,9 +133,10 @@ public class Cocos2dxAccelerometer implements SensorEventListener { // =========================================================== // Methods + // Native method called from Cocos2dxGLSurfaceView (To be in the same thread) // =========================================================== - - private static native void onSensorChanged(final float pX, final float pY, final float pZ, final long pTimestamp); + + public static native void onSensorChanged(final float pX, final float pY, final float pZ, final long pTimestamp); // =========================================================== // Inner and Anonymous Classes diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index fc1ae21f5d..514c8d5e9d 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -117,6 +117,16 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { // Getter & Setter // =========================================================== + + public static void queueAccelerometer(final float x, final float y, final float z, final long timestamp) { + mCocos2dxGLSurfaceView.queueEvent(new Runnable() { + @Override + public void run() { + Cocos2dxAccelerometer.onSensorChanged(x, y, z, timestamp); + } + }); + } + public void setCocos2dxRenderer(final Cocos2dxRenderer renderer) { this.mCocos2dxRenderer = renderer; this.setRenderer(this.mCocos2dxRenderer); diff --git a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java index 8ae3476f59..7a4e74e2ba 100644 --- a/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java +++ b/cocos2dx/platform/android/java/src/org/cocos2dx/lib/Cocos2dxRenderer.java @@ -79,23 +79,32 @@ public class Cocos2dxRenderer implements GLSurfaceView.Renderer { @Override public void onDrawFrame(final GL10 gl) { + /* + * FPS controlling algorithm is not accurate, and it will slow down FPS + * on some devices. So comment FPS controlling code. + */ + + /* final long nowInNanoSeconds = System.nanoTime(); final long interval = nowInNanoSeconds - this.mLastTickInNanoSeconds; + */ // should render a frame when onDrawFrame() is called or there is a // "ghost" Cocos2dxRenderer.nativeRender(); + /* // fps controlling if (interval < Cocos2dxRenderer.sAnimationInterval) { try { // because we render it before, so we should sleep twice time interval - Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) * 2 / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND); + Thread.sleep((Cocos2dxRenderer.sAnimationInterval - interval) / Cocos2dxRenderer.NANOSECONDSPERMICROSECOND); } catch (final Exception e) { } } this.mLastTickInNanoSeconds = nowInNanoSeconds; + */ } // =========================================================== diff --git a/scripting/javascript/bindings/cocos2d_specifics.cpp b/scripting/javascript/bindings/cocos2d_specifics.cpp index a1ab3e63f3..ce3369563c 100644 --- a/scripting/javascript/bindings/cocos2d_specifics.cpp +++ b/scripting/javascript/bindings/cocos2d_specifics.cpp @@ -790,7 +790,9 @@ void JSScheduleWrapper::scheduleFunc(float dt) const return; } - if(!JSVAL_IS_VOID(jsCallback) && !JSVAL_IS_VOID(jsThisObj)) { + if(!jsCallback.isNullOrUndefined() || !jsThisObj.isNullOrUndefined()) { + JSAutoCompartment ac(cx, JSVAL_TO_OBJECT(jsThisObj)); + JS_CallFunctionValue(cx, JSVAL_TO_OBJECT(jsThisObj), jsCallback, 1, &data, &retval); }