return 和编辑多个变量 [JAVA]
return and edit of multiple variables [JAVA]
我的第一个游戏引擎有问题所以请帮助我:(
有两个部分,第一部分我将解释问题,第二部分我将解释我的问题。
第一部分:
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 .
-------------------------------------------- --------
我抱歉我的英语不好,但我试图用我脑海中的所有词来解释这个问题,我希望我做到了(any答案甚至可以帮助我解决部分问题,所以请帮助)
您可以像这样尝试使用 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";
我的第一个游戏引擎有问题所以请帮助我:(
有两个部分,第一部分我将解释问题,第二部分我将解释我的问题。
第一部分:
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 .
-------------------------------------------- --------
我抱歉我的英语不好,但我试图用我脑海中的所有词来解释这个问题,我希望我做到了(any答案甚至可以帮助我解决部分问题,所以请帮助)
您可以像这样尝试使用 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";