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

2016/2/18

Make all instances of class do the same thing

1
2
Collin Collin

2016/2/18

#
Hi, i have a class and about 20 instances of it are spawned when the game starts. At a certain part of the game, I want all instances of this class to move to a specific location. How can I get all instances of the same class do the same thing? I would really appreciate your help!
Super_Hippo Super_Hippo

2016/2/18

#
1
2
3
4
5
6
//certain part of the game
for (Object obj : getObjects(NameOfTheClass.class)) //'getWorld().getObjects(...)' if called from an Actor subclass
{
    (NameOfTheClass) n = (NameOfTheClass) obj;
    n.setTarget(200, 300);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private int[] target = null;
 
public void act()
{
    if (target != null)
    {
        turnTowards(target[0], target[1]);
        move(2);
        return;
    }
    //do what ever it usually does when it is not this certain part of the game
}
 
public void setTarget(int x, int y)
{
    target = new int[2];
    target[0] = x;
    target[1] = y;
}
If all instances of this class should always share the same 'target' variables, you can make them static:
1
2
//certain part of the game
NameOfTheClass.setTarget(200, 300);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static int[] target = null;
 
public void act()
{
    if (target != null)
    {
        turnTowards(target[0], target[1]);
        move(2);
        return;
    }
    //do what ever it usually does when it is not this certain part of the game
}
 
public static void setTarget(int x, int y)
{
    target = new int[2];
    target[0] = x;
    target[1] = y;
}
Collin Collin

2016/2/18

#
Damn, you are a god, thank you sooo much!!!! This was exactly what I was looking for!
Collin Collin

2016/2/18

#
Super_Hippo wrote...
1
2
3
4
5
6
//certain part of the game
for (Object obj : getObjects(NameOfTheClass.class)) //'getWorld().getObjects(...)' if called from an Actor subclass
{
    (NameOfTheClass) n = (NameOfTheClass) obj;
    n.setTarget(200, 300);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private int[] target = null;
 
public void act()
{
    if (target != null)
    {
        turnTowards(target[0], target[1]);
        move(2);
        return;
    }
    //do what ever it usually does when it is not this certain part of the game
}
 
public void setTarget(int x, int y)
{
    target = new int[2];
    target[0] = x;
    target[1] = y;
}
If all instances of this class should always share the same 'target' variables, you can make them static:
1
2
//certain part of the game
NameOfTheClass.setTarget(200, 300);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
private static int[] target = null;
 
public void act()
{
    if (target != null)
    {
        turnTowards(target[0], target[1]);
        move(2);
        return;
    }
    //do what ever it usually does when it is not this certain part of the game
}
 
public static void setTarget(int x, int y)
{
    target = new int[2];
    target[0] = x;
    target[1] = y;
}
Oh wait, what if I want the instances to move to another location after that?
Super_Hippo Super_Hippo

2016/2/18

#
Which code did you use? Do you want them to move to another target location or just using the normal behaviour again?
danpost danpost

2016/2/19

#
Collin wrote...
Oh wait, what if I want the instances to move to another location after that?
At any time, you can call 'setTarget' to 'null' for them to resume normal movement or to another set of coordinates to make them all proceed to a new (or different) location.
Collin Collin

2016/2/19

#
danpost wrote...
At any time, you can call 'setTarget' to 'null' for them to resume normal movement or to another set of coordinates to make them all proceed to a new (or different) location.
That doesn't work, I then get the error "setTarget(int, int) cannot be applied to (<nulltype>)". The same thing happens with turnTowards()
danpost danpost

2016/2/19

#
Collin wrote...
That doesn't work, I then get the error "setTarget(int, int) cannot be applied to (<nulltype>)". The same thing happens with turnTowards()
I guess so. The 'setTarget' method was not set up with the intention of setting 'target' back to null. Add the following method and call it to have the actors resume normal movement with no particular target destination:
1
2
3
4
public static void clearTarget()
{
    target = null;
}
Collin Collin

2016/2/19

#
Oh wow, thanks! Now just one last thing. Is it possible to select all objects with the same picture with Super_Hippos for-loop? I have no idea how this would work but I assume something like this:
1
2
3
for (Object obj : getImage().toString().equals("image-file.png")) {
//code
}
Is something like that possible?
danpost danpost

2016/2/19

#
Collin wrote...
Is it possible to select all objects with the same picture with Super_Hippos for-loop? I have no idea how this would work but I assume something like this: < Code Omitted > Is something like that possible?
Not like that! You cannot get a String filename from a GreenfootImage object. You could test this with the following:
1
System.out.println(getImage().toString());
to see what is output. At any rate, one would need to know how you are setting the images of these actors. I am presuming that actors of the same class will have different images and you are attempting to sift out ones with a specific image. How you assign these images are pertinent to how you would proceed to sift out those actors.
Collin Collin

2016/2/19

#
Yes, you are right. The instances move around the world and collect objects. When they have collected one, the image changes, so you know which instance has already collected an object and which one has not. I want to get only the instances which have not collected an object yet.
danpost danpost

2016/2/19

#
Collin wrote...
Yes, you are right. The instances move around the world and collect objects. When they have collected one, the image changes, so you know which instance has already collected an object and which one has not. I want to get only the instances which have not collected an object yet.
I will then presume that all object of the class that have collected an object will have the same image. Keep a reference to that image in a class field. Set the image to the referenced image when collecting an object and compare its image to the referenced image when needed:
1
2
3
4
5
6
7
8
9
10
11
12
13
private static final GreenfootImage COLLECTED_IMAGE = new GreenfootImage("image-file.png");
 
// in action code
if (< collected object >)
{
    setImage(COLLECTED_IMAGE);
}
 
// method to test image
public boolean hasCollectedObject()
{
    return getImage() == COLLECTED_IMAGE;
}
Then you can use the loop with:
1
if (n.hasCollectedObject())
danpost danpost

2016/2/19

#
I uploaded a demo scenario to show simple code for selecting a target location for a class of actors. It is here.
Collin Collin

2016/2/19

#
danpost wrote...
Then you can use the loop with:
1
if (n.hasCollectedObject())
And it is not possible to select the instances with the specific image directly in the for-loop? So if I have 20 instances, and 10 of them have already collected an object, "n" would equal only the other 10 instances. Sorry for my pickiness.
danpost danpost

2016/2/19

#
The loop can only limit the actors to those of the class. The 'if' statement will seed out only those with that particular image. In other words 'n' will be only those object that have already collected an object only within the 'if' block.
There are more replies on the next page.
1
2