为什么我的对象列表都一样?

时间:2014-02-23 20:23:01

标签: android object matrix arraylist opengl-es-2.0

我一直在制作对象列表一段时间,以便在我编程的游戏中制作多个对象。直到现在我还没有遇到过问题。我使用for循环创建3个对象,为每个对象提供自己的值,然后将它们添加到数组列表中。不幸的是,当我尝试这个时,每个对象都有相同的值,我通过日志计算出的是列表中最后一项的值。我不确定我做错了什么。这是我正在使用的代码(我很抱歉。它非常草率,我只是试图编写当前项目的核心。有很多无用的代码/编程不良的代码/错误的编程代码,我知道)< / p>

GLCircle.java(我想要的多个对象):

package com.background.gl.objects;
import static android.opengl.GLES20.GL_TRIANGLE_FAN;
import static android.opengl.GLES20.glDrawArrays;
import static android.opengl.Matrix.multiplyMM;
import static android.opengl.Matrix.setIdentityM;
import static android.opengl.Matrix.translateM;
import static com.background.gl.glcirclebackgroundanimation.Constants.BYTES_PER_FLOAT;

import java.util.Random;

import android.opengl.Matrix;

import com.background.gl.data.VertexArray;
import com.background.gl.glcirclebackgroundanimation.CircleHandler;
import com.background.gl.helper.TextureShaderProgram;

public class GLCircle {
    private static final int POSITION_COMPONENT_COUNT = 2;
    private static final int TEXTURE_COORDINATES_COMPONENT_COUNT = 2;
    private static final int STRIDE = (POSITION_COMPONENT_COUNT
    + TEXTURE_COORDINATES_COMPONENT_COUNT) * BYTES_PER_FLOAT;

    public static float x;
    public static float y;
    protected static float[] wallBounds;
    protected static boolean positiveX, positiveY;
    public static boolean nullify;
    protected static float xCounter = 0f;
    protected static float yCounter = 0f;
    public static float[] bounds;
    protected Random ran;

    private static final float[] VERTEX_DATA = {
        // Order of coordinates: X, Y, S, T
        // Triangle Fan
        0f, 0f, 0.5f, 0.5f,
        -0.25f, -0.25f, 0f, 0.9f,
        0.25f, -0.25f, 1f, 0.9f,
        0.25f, 0.25f, 1f, 0.1f,
        -0.25f, 0.25f, 0f, 0.1f,
        -0.25f, -0.25f, 0f, 0.9f };

    private final VertexArray vertexArray;

    public GLCircle(float x, float y) {
        vertexArray = new VertexArray(VERTEX_DATA);
        ran = new Random();
        wallBounds = new float[4];
        nullify = false;
        this.x = x;
        this.y = y;
    }

    public void bindData(TextureShaderProgram textureProgram) {
        //Bind the position data to the shader attribute
        vertexArray.setVertexAttribPointer(
            0,
            textureProgram.getPositionAttributeLocation(),
            POSITION_COMPONENT_COUNT,
            STRIDE);
        //Bind the texture coordinate data to the shader attribute
        vertexArray.setVertexAttribPointer(
                POSITION_COMPONENT_COUNT,
                textureProgram.getTextureCoordinatesAttributeLocation(),
                TEXTURE_COORDINATES_COMPONENT_COUNT,
                STRIDE);
        }

    public void drawCircle() {
        glDrawArrays(GL_TRIANGLE_FAN, 0, 6);
    }


    public float getX() {
        return this.x;
    }

    public float getY() {
        return this.y;
    }



    public static boolean isPositiveX() {
        return positiveX;
    }



    public static boolean isPositiveY() {
        return positiveY;
    }


    public float[] getBounds(float ranX, float ranY) {
        if(!positiveX) {
            /*if(ranX >= 0f) {
                wallBounds[0] = 1.05f + ranX;
            } else {*/
                this.wallBounds[0] = 1.05f + ranX;
            //}
        } else {
            /*
            if(ranX >= 0f) {
                wallBounds[0] = 1.05f - ranX;
            } else {*/
                this.wallBounds[1] = 1.05f - ranX;
            //}
        }
        if(!positiveY) {
            this.wallBounds[2] = 1.75f + ranY;
        } else {
            this.wallBounds[3] = 1.75f - ranY;
        }

        return this.wallBounds;
    }

    public void setPos(float[] modelMatrix, 
            float[] projectionMatrix, TextureShaderProgram textureProgram,
            int texture, float x, float y) {
        setIdentityM(modelMatrix, 0);
        if(!nullify) 
            translateM(modelMatrix, 0, 0f, 0.01f, 0f);
        else
            translateM(modelMatrix, 0, 0f, 0f, 0f);
        final float[] temp = new float[16];
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
        System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);

        textureProgram.useProgram();
        textureProgram.setUniforms(projectionMatrix, texture);
        bindData(textureProgram);

        drawCircle();
    }

    public void draw(float velocity, float[] modelMatrix, 
            float[] projectionMatrix, TextureShaderProgram textureProgram,
                int texture) {
        xCounter+=0.001f;
        setIdentityM(modelMatrix, 0);
        if(-x < -1.75f) {
            translateM(modelMatrix, 0, 0f, 0.001f, 0f);
        } else {
        translateM(modelMatrix, 0, 0f, -0.001f, 0f);
        }
        final float[] temp = new float[16];
        multiplyMM(temp, 0, projectionMatrix, 0, modelMatrix, 0);
        System.arraycopy(temp, 0, projectionMatrix, 0, temp.length);

        textureProgram.useProgram();
        textureProgram.setUniforms(projectionMatrix, texture);
        bindData(textureProgram);

        drawCircle();
    }

    public void scaleCircle(float[] modelMatrix, float x, float y, float z) {
        Matrix.scaleM(modelMatrix, 0, x, y, z);
    }

    public void storeResults(float[] results) {
        this.x = results[0];
        this.y = results[1];
    }
}

这就是我创建多个对象的方式:

for(int i = 0; i < 3; i++) {
            GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);
            circles.add(circle);
            /*circle[i].x = circle[i].getX();
            circle[i].y = circle[i].getY();
            circle[i].bounds = circle[i].getBounds();*/
        }

这是generateranFloats()方法:

public float[] generateRanFloats() {
        ranSignX = ran.nextFloat();
        ranSignY = ran.nextFloat();
        ranSignX = ranSignX > 0.5f? -1:1;
        ranSignY = ranSignY > 0.5f? -1:1;
        ranSignVeloX = ran.nextFloat();
        ranSignVeloY = ran.nextFloat();
        ranX = ran.nextFloat() * 1.05f;
        ranY = ran.nextFloat() * 1.75f;  
        ranX = ranSignX > 0.5? -ranX:ranX;
        ranY = ranSignY > 0.5? -ranY:ranY;
        Log.d("Generated", Float.toString(ranX));
        return new float[] {ranX, ranY};
    }

为什么所有对象都包含相同的值(例如x和y)?

1 个答案:

答案 0 :(得分:1)

答案很简单

您的x,y变量被声明为static,这意味着您的对象的所有实例共享相同的变量。

顺便说一句,这一行

GLCircle circle = new GLCircle(generateRanFloats()[0], generateRanFloats()[1]);

正在进行双重工作,你正在调用generateRanFloats两次,并且每次都使用一半生成的信息。