diff --git a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java index 2978066f0e..a3a7a7f46f 100644 --- a/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java +++ b/HelloLua/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java @@ -1,10 +1,13 @@ package org.cocos2dx.lib; import android.content.Context; +import android.content.res.Configuration; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.util.Log; +import android.view.WindowManager; /** * @@ -37,11 +40,27 @@ public class Cocos2dxAccelerometer implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) + if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){ return; + } - onSensorChanged(event.values[0], event.values[1], event.values[2], event.timestamp); - + float x = event.values[0]; + float y = event.values[1]; + float z = event.values[2]; + + /* + * Because the axes are not swapped when the device's screen orientation changes. + * So we should swap it here. + */ + int orientation = mContext.getResources().getConfiguration().orientation; + if (orientation == Configuration.ORIENTATION_LANDSCAPE){ + float tmp = x; + x = -y; + y = tmp; + } + + onSensorChanged(x, y, z, event.timestamp); + // Log.d(TAG, "x = " + event.values[0] + " y = " + event.values[1] + " z = " + event.values[2]); } @Override diff --git a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java index 2978066f0e..a3a7a7f46f 100644 --- a/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java +++ b/HelloWorld/android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java @@ -1,10 +1,13 @@ package org.cocos2dx.lib; import android.content.Context; +import android.content.res.Configuration; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.util.Log; +import android.view.WindowManager; /** * @@ -37,11 +40,27 @@ public class Cocos2dxAccelerometer implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) + if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){ return; + } - onSensorChanged(event.values[0], event.values[1], event.values[2], event.timestamp); - + float x = event.values[0]; + float y = event.values[1]; + float z = event.values[2]; + + /* + * Because the axes are not swapped when the device's screen orientation changes. + * So we should swap it here. + */ + int orientation = mContext.getResources().getConfiguration().orientation; + if (orientation == Configuration.ORIENTATION_LANDSCAPE){ + float tmp = x; + x = -y; + y = tmp; + } + + onSensorChanged(x, y, z, event.timestamp); + // Log.d(TAG, "x = " + event.values[0] + " y = " + event.values[1] + " z = " + event.values[2]); } @Override diff --git a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java index 2978066f0e..a3a7a7f46f 100644 --- a/tests/test.android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java +++ b/tests/test.android/src/org/cocos2dx/lib/Cocos2dxAccelerometer.java @@ -1,10 +1,13 @@ package org.cocos2dx.lib; import android.content.Context; +import android.content.res.Configuration; import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; +import android.util.Log; +import android.view.WindowManager; /** * @@ -37,11 +40,27 @@ public class Cocos2dxAccelerometer implements SensorEventListener { @Override public void onSensorChanged(SensorEvent event) { - if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) + if (event.sensor.getType() != Sensor.TYPE_ACCELEROMETER){ return; + } - onSensorChanged(event.values[0], event.values[1], event.values[2], event.timestamp); - + float x = event.values[0]; + float y = event.values[1]; + float z = event.values[2]; + + /* + * Because the axes are not swapped when the device's screen orientation changes. + * So we should swap it here. + */ + int orientation = mContext.getResources().getConfiguration().orientation; + if (orientation == Configuration.ORIENTATION_LANDSCAPE){ + float tmp = x; + x = -y; + y = tmp; + } + + onSensorChanged(x, y, z, event.timestamp); + // Log.d(TAG, "x = " + event.values[0] + " y = " + event.values[1] + " z = " + event.values[2]); } @Override diff --git a/tests/tests/RenderTextureTest/RenderTextureTest.cpp b/tests/tests/RenderTextureTest/RenderTextureTest.cpp index c7726b31ee..edb0d9f7e4 100644 --- a/tests/tests/RenderTextureTest/RenderTextureTest.cpp +++ b/tests/tests/RenderTextureTest/RenderTextureTest.cpp @@ -322,7 +322,9 @@ void RenderTextureSave::ccTouchesMoved(CCSet* touches, CCEvent* event) m_pBrush->setRotation(rand() % 360); float r = (float)(rand() % 50 / 50.f) + 0.25f; m_pBrush->setScale(r); - m_pBrush->setColor(ccc3(CCRANDOM_0_1() * 127 + 128, 255, 255)); + /*m_pBrush->setColor(ccc3(CCRANDOM_0_1() * 127 + 128, 255, 255));*/ + // Use CCRANDOM_0_1() will cause error when loading libtests.so on android, I don't know why. + m_pBrush->setColor(ccc3(rand() % 127 + 128, 255, 255)); // Call visit to draw the brush, don't call draw.. m_pBrush->visit(); }