I'm still struggling to find a clean easy way to load some values in my onCreate method and pass them to another class in order that a game may load and save option settings.
I can successfully retrieve and change values from my other class but the problem is, no matter what I try, variable set in the onCreate method will not carry to the rest of the code.
I take the point that by only showing snipits of the code I may have obscured the problem but my original is far to massive and sprawling to post HOWEVER it was based on a great tutorial by Martin http://www.droidnova.com/android-3d-game-tutorial-part-i,312.html
So I've returned to first principles and just added the problematic portion to that tutorial code and my problem has been replicated. So here is the complete code:
Vortex.java **
package com.clockworkrobot.vortex;
import android.app.Activity;
import android.os.Bundle;
public class Vortex extends Activity {
private static final String LOG_TAG = Vortex.class.getSimpleName();
private VortexView _vortexView;
private float _red ; // these are the values that actually reach VortexRender class
private float _green = 1f ; // touch the screen, it will turn green.
private float _blue ; // but why don't the variable in my onCreate override these?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_red = 1f; // The test values I want to reach my Vortex Renderer class.
_green = 0f; // to help debug. they should make the screen magenta
_blue = 1f; // Eventually these value will be loaded during onCreate
setRed(_red); // I don't think these are needed
setGreen(_green); // But since the variable within this method
setBlue(_blue); // don't appear to be reaching their target..worth a try
_vortexView = new VortexView(this);
setContentView(_vortexView);
}
public float getRed() {
return _red;
}
public void setRed(float value) {
_red = value;
}
public float getGreen() {
return _green;
}
public void setGreen(float value) {
_green = value;
}
public float getBlue() {
return _blue;
}
public void setBlue(float value) {
_blue = value;
}
}
VortexRender.java**
package com.clockworkrobot.vortex;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;
import android.app.Activity;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
public class VortexRenderer extends Activity implements GLSurfaceView.Renderer {
private static final String LOG_TAG = VortexRenderer.class.getSimpleName();
// Vortex sw = new Vortex();
private Vortex sw;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sw = new Vortex();
}
private float _red = 0f;
private float _green = 0f;
private float _blue = 0f;
@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// Do nothing special.
}
@Override
public void onSurfaceChanged(GL10 gl, int w, int h) {
gl.glViewport(0, 0, w, h);
}
@Override
public void onDrawFrame(GL10 gl) {
// define the color we want to be displayed as the "clipping wall"
gl.glClearColor(_red, _green, _blue, 1.0f);
// clear the color buffer to show the ClearColor we called above...
gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
}
public void setColor(float r, float g, float b) {
_red = sw.getRed(); // Want these to grab the values from my onCreate method
_green = sw.getGreen(); // But instead it's getting the nul values
_blue = sw.getBlue(); // eg values as set above me onCreate.
// _red = r;
// _green = g;
// _blue = b;
}
}
VortexView.java**
package com.clockworkrobot.vortex;
import android.content.Context;
import android.opengl.GLSurfaceView;
import android.view.MotionEvent;
public class VortexView extends GLSurfaceView {
private static final String LOG_TAG = VortexView.class.getSimpleName();
private VortexRenderer _renderer;
public VortexView(Context context) {
super(context);
_renderer = new VortexRenderer();
setRenderer(_renderer);
}
public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable() {
public void run() {
_renderer.setColor(event.getX() / getWidth(), event.getY() / getHeight(), 1.0f);
}
});
return true;
}
}
AndroidManifest.xml**
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.clockworkrobot.vortex"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Vortex"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
In essence.. What I require is a way to save and recover the current screen colour when the game opens and closes. Thanks to everyone who has helped so far.
I'm still struggling to find a clean easy way to load some values in my onCreate method and pass them to another class in order that a game may load and save option settings.
Use SharedPreferences
-- that is why they are there.
Anyone got any helpful ideas?
Either:
Activity
object you think it is, orUPDATE
You are creating a Vortex
via new Vortex()
. Vortex
is an Activity
. Never create activities via the constructor. You do not access activities from other activities. Your red/green/blue values need to be in some data model accessible from all your components.
Given your code and comments, I strongly encourage you to first learn Java outside of Android, then learn Android.