gpt4 book ai didi

java - 多个变量的返回和编辑[JAVA]

转载 作者:行者123 更新时间:2023-11-30 08:03:03 27 4
gpt4 key购买 nike

我的第一个游戏引擎有问题,所以请帮助我:(

有两部分,第一部分我将解释问题,第二部分我将解释我的问题。

第一部分:

i have an array (named "World") of object class

public Object World[] = new Object[500];

the object have many properties (name,x,y,animation,length,width ....)

i want to make condition of collession for example

if( Function_to_detect_collessions("object1_name","object2_name") ){
object2.Animation = "new value" ;
}

and with these lines you will understand me :

1- many object can get the same name

2- if more than one collession happened with more than two objects with the same names (object1_name and object2_name) then the modification for the object2.animation will be on all the touched objects

example :
if( collesion("ball","ground") ){
ball.movement = stop;
}
//Now imagine that there is two objects (two Balls) on the ground

第二部分:

i think that you understand me what i mean and now i will explain my question. questions :

1- if i can detect all the collessions how to make the modification on all the objects with one line like

object2.prop = "something"

2- is it possible in java to make modification on an object and with some functions make the same modification on more than one object automatically .

------------------------------------------------------------ ------

我为我的英语不好而抱歉,但我试图用我脑海中的所有单词来解释问题,我希望我这样做(任何答案都可以帮助我,即使有一个解决方案的一部分,因此请提供帮助)

最佳答案

您可以尝试像这样使用 Java 集合:

public ArrayList<Object> World= new ArrayList<>();

for (int i =0; i<500; i++)
World.add(new Object(i));

// Update all objects
for (Object myObject : World) myObject.prop = "Something";

编辑:

根据您接下来的问题。如果您只需要浏览特定列表,您可以这样做:

public ArrayList<Object> MoversAndShakers = new ArrayList<>();

MoversAndShakers.add(World.get(3));
MoversAndShakers.add(World.get(5));
MoversAndShakers.add(World.get(9));

// Update all MoversAndShakers
for (Object myObject : MoversAndShakers) myObject.prop = "Something";

或者更好的是,你可以将其封装在一个函数中来检测冲突(你可能想看看这个问题如何做到这一点Simple and fast collision algorithm in java for non-axis aligned boxes):

public ArrayList<Object> MoversAndShakers = new ArrayList<>();

ArrayList<Object> getMovingObjects(ArrayList<Object> World)
{
ArrayList<Object> MoversAndShakers = new ArrayList<>();

for(Object currentObj : World)
{
if (currentObj.velocity > 0)
MoversAndShakers.add(currentObj);
}
return MoversAndShakers;
}

然后上面就简化为这样

// Update all moving objects
for (Object myObject : getMovingObjects(World))
myObject.prop = "Something";

关于java - 多个变量的返回和编辑[JAVA],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31594403/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com