Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/357.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中添加许多对象_Java_Class_Lwjgl - Fatal编程技术网

在java中添加许多对象

在java中添加许多对象,java,class,lwjgl,Java,Class,Lwjgl,如果我错了,请纠正我。我对java非常陌生,正在尝试使用lwjgl创建一个简单的物理引擎。目前代码有点凌乱,但它启动和工作正常,物理工作完美。我试图想出一种方法,允许用户单击鼠标并添加物理对象。目前,这是可能的,但有一个限制,我必须在所有的对象名代码 添加物理对象: phys_square rec1 = new phys_square(100, 100, 10.0f, -1.0f, 10, 0.0f, 0.5f, 0.0f); 以及phys_square类的代码: package quad;

如果我错了,请纠正我。我对java非常陌生,正在尝试使用lwjgl创建一个简单的物理引擎。目前代码有点凌乱,但它启动和工作正常,物理工作完美。我试图想出一种方法,允许用户单击鼠标并添加物理对象。目前,这是可能的,但有一个限制,我必须在所有的对象名代码

添加物理对象:

phys_square rec1 = new phys_square(100, 100, 10.0f, -1.0f, 10, 0.0f, 0.5f, 0.0f);
以及phys_square类的代码:

package quad;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;

public class phys_square {

float velocityY;
float velocityX;
int x;
int y;
int particleSize;
float red;
float green;
float blue;
public phys_square(int xpos, int ypos, float velocity_x, float velocity_y, int size, float red, float green, float blue) { this.x = xpos; this.y = ypos; this.velocityY = velocity_y; this.velocityX = velocity_x; this.particleSize = size; this.red = red; this.green = green; this.blue = blue;} 
public void updatePos() {
    if ( this.y == 0 | this.y < 0) {
        this.velocityY *= -0.825f;
    }
    if (this.y > 0 | this.y >= this.velocityY) {
        this.y -= this.velocityY;
        this.velocityY += 0.2f;
    }

    if (this.x <= 0 | this.x + quad_main.particleSize >= 800) {
        this.velocityX *= -0.5f;
    }
    if (this.x > 0 | this.x >= this.velocityX) {
        this.x -= this.velocityX;
        if (Math.abs(this.velocityX) <= 0){
            this.velocityX -= 0.2f;
        }
    }
    if (this.x < 0) {
        this.x = 0;
    }
    if (this.x > 800 + quad_main.particleSize) {
        this.x = 800 + quad_main.particleSize; 
    }
    if (this.y < 0){
        this.y = 0;
    }
}
public void render() {
    window.particle(this.x, this.y, this.red, this.green, this.blue, this.particleSize);
}
public static void main(String[] args[]){

}
}
package;
导入org.lwjgl.LWJGLException;
导入org.lwjgl.input.Keyboard;
导入org.lwjgl.input.Mouse;
导入org.lwjgl.opengl.Display;
导入org.lwjgl.opengl.DisplayMode;
导入org.lwjgl.opengl.GL11;
公共级体育广场{
浮动速度y;
浮动速度x;
int x;
int-y;
int粒子化;
飘红;
浮绿;
飘蓝;
公共物理平方(intxpos,intypos,float velocity,float velocity,float velocity,float red,float green,float blue){this.x=xpos;this.y=ypos;this.velocityY=velocity\u y;this.velocityX=velocity\u x;this.particleSize=size;this.red=red;this.green=green;this.blue=blue;}
公共void updatePos(){
if(this.y==0 | this.y<0){
这是0.velocityY*=-0.825f;
}
如果(this.y>0 | this.y>=this.velocityY){
this.y-=this.velocityY;
这是0.velocityY+=0.2f;
}
如果(此.x=800){
这是0.velocityX*=-0.5f;
}
如果(this.x>0 | this.x>=this.velocityX){
this.x-=this.velocityX;
if(Math.abs(this.velocityX)800+quad_main.particleSize){
这个.x=800+四个主粒子;
}
如果(此.y<0){
这个。y=0;
}
}
公共无效呈现(){
window.particle(this.x,this.y,this.red,this.green,this.blue,this.particleSize);
}
公共静态void main(字符串[]参数[]){
}
}

有没有一种方法可以创建多个对象而无需手动分配所有名称?我听说在PHP和其他一些语言中,可以使用字符串的值作为另一个对象的名称。

注意:这个答案假设您根本不需要名称,而不是希望在运行时创建名称。使用哈希映射

您可以将对象存储在列表中,例如
ArrayList
。这类似于对象数组,但可以调整大小,并使您能够随意添加对象、使用索引访问对象以及对其进行迭代


将其与您的导入内容放在一起:

import java.util.ArrayList;  
以下代码创建您的
ArrayList

ArrayList<phys_square> squaresList = new ArrayList<phys_square>();
当你想处理这些对象时,你可以遍历你的列表

for (phys_square sq : squaresList) {
    processPhysicsStuff(sq);
    processGraphicsStuff(sq);
}
您可以查看
Arraylist
的文档。请仔细阅读,这是一个非常有用的类

for (phys_square sq : squaresList) {
    processPhysicsStuff(sq);
    processGraphicsStuff(sq);
}