2014-02-23 59 views
0

我已經制作了一段時間的對象列表,以便在我編程的遊戲中製作多個對象。直到現在我還沒有問題。我使用for循環來創建3個對象,給每個對象自己的值,然後將它們添加到數組列表中。不幸的是,當我嘗試這樣做時,每個對象都具有相同的值,我通過日誌計算出的值是列表中最後一項的值。我不確定我做錯了什麼。這裏是我正在使用的代碼(我很抱歉,這是非常草率的,我只是試圖編程這個項目的核心目前有很多無用的代碼/編程不好的代碼/編程錯誤的代碼,我知道)爲什麼我的物品清單都是一樣的?

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

+0

你所有的ranXXX變量都是浮動的嗎? – Merlevede

+0

是的,他們都是花車。 – user2082169

+0

如果你逐步調試'generateRanFloats()'函數會發生什麼? – Merlevede

回答

1

答案很簡單

你的X,Y變量聲明爲static,這意味着相同的變量的對象共享的所有實例。

順便說一句,這條線

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

做雙重的工作,你打電話generateRanFloats兩次,你每次都使用所生成的信息的一半。

+0

我不知道爲什麼我把它作爲靜態...謝謝。 :D是的,我意識到我調用了生成方法兩次,但只使用x或y。我只是想看看它是否會起作用。再次感謝。 – user2082169

相關問題