This site requires JavaScript, please enable it in your browser!
Greenfoot back
Beamo
Beamo wrote ...

2017/10/10

Is there a way to add many of one actor, in a small amount of code?

Beamo Beamo

2017/10/10

#
In the world class, I am trying to add around 70 instances of one class, however, to do this I must keep writing
object object = new object();
addObject(object,0,0)
object object2 = new object();
addObject(object,100,100)
and so on. I'm trying to optimize it so it won't take 140 lines of code to make. Any suggestions? Thanks in advance!
Super_Hippo Super_Hippo

2017/10/10

#
The first two lines can be done in one:
addObject(new object(),0,0);
For 70, you can write a for loop. It depends on where the objects should be added.
for (int i=0; i<70; i++) addObject(new object(), i*100, i*100);
But your world probably isn't 7000x7000 large, so maybe you will end up with this:
for (int i=0; i<10; i++) for (int j=0; j<7; j++) addObject(new object(), i*100, j*100);
Beamo Beamo

2017/10/10

#
That's perfect! Thank you so much! You may have just saved me hours of writing code!
Beamo Beamo

2017/10/11

#
Hi! Sorry this is quite an old post, but I'm just wondering if there's a way to change how close the objects are to eachother?
Super_Hippo Super_Hippo

2017/10/11

#
Change the 100 to something smaller.
Beamo Beamo

2017/10/11

#
Ah I see! I kept adding to that value, while I should have been subtracting from it. Thanks again!
You need to login to post a reply.