为什么我的对象列表都一样? [英] Why is my list of objects all the same?

查看:42
本文介绍了为什么我的对象列表都一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been making lists of objects for a while now to make multiple objects in the games I program. I haven't had an issue until now. I use a for loop to create 3 objects, give each object its own values, and then add them to the array list. Unfortunately, when I tried this each object had the same values, which I figured out through logs were the values of the last item in the list. I'm not sure what I'm doing wrong. Here is the code I'm using(I apologize. It's very sloppy, I'm just trying to program the core of this project currently. There is a lot of useless code/poorly programmed code/incorrectly programmed code, I know)

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

GLCircle.java(the object I want to make multiple of):

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];
    }
}

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

This is how I create the multiple objects:

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() 方法:

And this is the generateranFloats() method:

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)?

Why do all of the objects contain the same values(such as x and y)?

推荐答案

答案很简单

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

Your x, y variables are declared as static, meaning all instances of your object share the same variables.

顺便说一下,这条线

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

正在做双重工作,您调用 generateRanFloats 两次,并且每次使用一半的生成信息.

is doing double work, you're calling generateRanFloats twice, and you use half of the generated information every time.

这篇关于为什么我的对象列表都一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆