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

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

问题描述

我已经作出对象列表,而现在,使在游戏我计划多个对象。我还没有一个问题,直到现在。我用一个for循环创建3个对象,为每个对象自己的价值观,然后将它们添加到数组列表。不幸的是,当我尝试这样每个对象有相同的价值观,我想通了,通过日志是在列表中最后一个项目的价值。我不知道我在做什么错。这里是code我使用的(我道歉。这是非常草率的,我只是想给这个项目目前的核心编程。有很多无用的code /编程不良code的/编程不正确code,我知道)

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变量声明为静态,这意味着相同的变量你的对象共享的所有实例。

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天全站免登陆