0

我仍在努力寻找一种简洁的方法来在我的 onCreate 方法中加载一些值并将它们传递给另一个类,以便游戏可以加载和保存选项设置。

我可以成功地从我的其他类中检索和更改值,但问题是,无论我尝试什么,onCreate 方法中设置的变量都不会传递到代码的其余部分。

我的观点是,仅显示代码片段可能掩盖了问题,但我的原件非常庞大且庞大,但它是基于 Martin http://www.droidnova.com/android的一个很棒的教程-3d-game-tutorial-part-i,312.html

所以我回到了第一原则,只是将有问题的部分添加到该教程代码中,我的问题已被复制。所以这里是完整的代码:

漩涡.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> 

本质上..我需要的是一种在游戏打开和关闭时保存和恢复当前屏幕颜色的方法。感谢迄今为止提供帮助的所有人。

4

1 回答 1

0

我仍在努力寻找一种简洁的方法来在我的 onCreate 方法中加载一些值并将它们传递给另一个类,以便游戏可以加载和保存选项设置。

使用SharedPreferences——这就是它们存在的原因。

有人有任何有用的想法吗?

任何一个:

  • Activity您的其他班级没有与您认为的对象交谈,或者
  • 您没有调用调用您的 setter 的其他类中的代码,或者
  • 作为测试的一部分,正在重新创建您的活动(例如,配置更改),或者
  • 正如@Arnout Engelen 所指出的,您正在从多个地方调用设置器并覆盖您想要的内容,或者
  • 其他的,因为你上面的代码很可能不是你应用程序的代码,当你去寻求帮助时,像这样的假例子只会导致问题,因为你引入了你真正运行的代码和运行的代码之间的差异你声称你正在跑步

更新 您正在创建一个Vortexvia new Vortex()Vortex是一个Activity。永远不要通过构造函数创建活动。您不能从其他活动访问活动。您的红色/绿色/蓝色值需要位于所有组件都可以访问的某个数据模型中。

鉴于您的代码和评论,我强烈建议您先在 Android 之外学习 Java,然后再学习 Android。

于 2011-09-03T19:58:16.397 回答