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

2016/11/9

getObjects(Actor.class);

ZoeF ZoeF

2016/11/9

#
Hello i am at a loss in my program i need to create for school. i need to get a list of several actors running around on my screen. They spawn on my map at a fixed location and walk around a set path. These enemies are of 2 types (a soldier and a tank (can become more in a later stage). My first issue is that i can't use the getWorld().getObjects(Soldier.class); As there are curently none of these actors around (they spawn later). Then i need to be able to get all these diffrent actors into a new list or some sort where i can actualy sort them by location or spawn timer. Anyone who is able to help me get on the right think pad?
danpost danpost

2016/11/10

#
ZoeF wrote...
My first issue is that i can't use the getWorld().getObjects(Soldier.class); As there are curently none of these actors around (they spawn later).
I do not see any reason you cannot use 'getWorl().getObjects(Soldier.class). It will always return a List object. It may be empty, or have no Soldier objects listed, but that should not be a problem. You can check that before trying to get any soldiers from the list using the 'isEmpty' method on the list or by comparing zero to the value returned by using the 'size' method on the list. From what it sounds like, however, you do not need to try either of those methods as you can just use the list iterator 'for' loop structure to create the full list:
1
2
3
List<Actor> actors = new ArrayList<Actor>();
for (Object obj : getWorld().getObjects(Soldier.class)) actors.add((Actor)obj);
for (Object obj : getWorld().getObjects(Tank.class)) actors.add((Actor)obj);
or even more simply you can append the lists returned
1
2
List<Actor> actors = (List<Actor>)getObjects(Soldier.class);
actors.addall((List<Actor>)getObjects(Tank.class));
This combined list will still need to have an'isEmpty' check or a 'size' comparison before trying to get any actors from it.
ZoeF ZoeF

2016/11/10

#
Your first code seems to work in my tower class. the second however seems to run into a snitch. I firstly needed to add getWorld().getObjects() as i don't run it in the world class. But then the Soldier.class and Tank.class get a error. Incompatibel types: java.util.List<Soldier> cannot be converted to java.util.List<greenfoot.Actor>
ZoeF ZoeF

2016/11/10

#
I am seriously not getting it. I wil try to explain what i want to accomplish. 1. Make a list of all enemies running around over my path. 1.a Curently i have 2 sets of enemies who spawn at random 60% soldiers 40% tanks 1.b They spawn at a fixed location 2. Once the enemies get shot down they get removed from the world. 2.a So the list should be updated once that happens. 3. My turrets (who are at fixed locations) should fire at the 1st one in the list. 3.a So if a enemie gets removed they should jump to the next one in the list. Here is the code i am curently using. Enemies.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import greenfoot.*;
 
public abstract class Enemies extends SmoothMover
{
    private int tellerTurn;
     
    public Enemies()
    {
        tellerTurn = 0;
    }
     
    public void act()
    {
        turning();
        move(0.5);
    }
     
    /**
      * Als getX == turnCords colom 0 waarde teller && getY == turnCords colom 1 waarde teller
      * dan draai rightLeft array waarde van teller
      * tellerTurn +1
      * Dit zorgt dan daarna dat de waardes naar de volgende stap in de array springen.
      */
    private void turning()
    {
        int[] rightLeft = {90, -90, -90, 90, -90, -90, 90, 90, 90};
         
        int X = TurnLocation.getCordX(tellerTurn);
        int Y = TurnLocation.getCordY(tellerTurn);
 
        if (getX() == X && getY() == Y)
        {
            turn(rightLeft[tellerTurn]);
            if(tellerTurn == rightLeft.length-1)
            {
                deadEnemie();
            }
            tellerTurn++;
        }
    }
     
    private void deadEnemie()
    {
        Towers.removeEnemies(this);
        getWorld().removeObject(this);
    }
}
Towers.class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import greenfoot.*;
import java.util.*;
 
public abstract class Towers extends Actor
{
    private boolean isGrabbed;
    private int teller;
    private Enemies enemie;
    private int selectEnemie;
    private int box;
    private boolean release;
    private static List<Enemies> enemies = new ArrayList<Enemies>();
 
    public Towers()
    {
        isGrabbed = false;
        teller = 0;
        box = 10;
        release = true;
        // zet de instancie EnemiesHelper in enemie en roep de methode getEnemies op (voorlopig enkel enemie 0).
    }
 
    public void act()
    {
        if (!release)
        {
            addEnemies();
            Enemies enemie = enemies.get(0);
            turnTowards(enemie.getX(), enemie.getY());
        }
        else
        {
            dragObj();
        }
    }
 
    public void addEnemies()
    {
        for (Object obj : getWorld().getObjects(Soldier.class))
        {
            enemies.add((Enemies)obj);
        }
        for (Object obj : getWorld().getObjects(Tank.class))
        {
            enemies.add((Enemies)obj);
        }
    }
     
    public static void removeEnemies(Enemies deleteEnemie)
    {
        enemies.remove(deleteEnemie);
    }
 
    /**
     * dragObj is de methode voor het verplaatsen van dit object.
     */
    private void dragObj()
    {
        if (Greenfoot.mousePressed(this) && !isGrabbed)
        {
            MouseInfo mi = Greenfoot.getMouseInfo();
            // grab object
            isGrabbed = true;
            // Deze code dient om het object boven alle andere objecten te houden.
            World world = getWorld();
            world.removeObject(this);
            world.addObject(this, mi.getX(), mi.getY());
            return;
        }
 
        /**
         * Hier neem ik de values van de TowerHelper array en verminder en vermeer ze met 10
         * Dan bekijk ik of de waarde van de muis hier tussen valt.
         * Indien ja zet ik de locatie op de TowerHelper array values. (Center van de plaats vierkantjes op de map.)
         * Anders volg de muis.
         */
        if (Greenfoot.mouseDragged(this) && isGrabbed)
        {
            MouseInfo mi = Greenfoot.getMouseInfo();
            int x = TowerHelper.getCordX(teller);
            int y = TowerHelper.getCordY(teller);
 
            if (mi.getX() >= x - box && mi.getX() <= x + box &&
            mi.getY() >= y - box && mi.getY() <= y + box){
                setLocation(x, y);
            }
            else{
                setLocation(mi.getX(), mi.getY());
                teller++;
            }
 
            // als de TowerHelper array lengte gelijk is aan teller zet teller terug op 0.
            if(teller == TowerHelper.getSizeOfCords()){
                teller = 0;
            }
            return;
        }
 
        // Check of de muis word gereleased.
        if (Greenfoot.mouseDragEnded(this) && isGrabbed)
        {
            // release het object
            isGrabbed = false;
            release = false;
        }
    }
}
I hope someone can assist me.
ZoeF ZoeF

2016/11/10

#
Changed some things around. Don't know if this is the best way. I made a helper class to keep track of enemies.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.util.*;
 
public class EnemiesHelper {
     
    /**
     * Klasse instanciert zichzelf. Hij kent zichzelf. En zal nooit ergens een andere instancie van bestaan.
     * Dus de instancie zal overal hetzelfde zijn.
     * Singleton :)
     */
    private static List<Enemies> enemies = new ArrayList<Enemies>();
     
    private EnemiesHelper()
    {
         
    }
     
    /**
     * Voeg enemie toe tot de arraylist
     */
    public static void add(Enemies enemy)
    {
        EnemiesHelper.enemies.add(enemy);
        System.out.println("added enemie to list");
    }
     
    public static Enemies get(int number)
    {
        return EnemiesHelper.enemies.get(number);
    }
     
    public static void removeEnemie(Enemies enemy)
    {
        EnemiesHelper.enemies.remove(enemy);
        System.out.println("removed enemie to list");
    }
}
And in my map class (sub of world) i added the following.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (spawnCountDown == 0 && tellerSpawn < 10)
        {
            Enemies enemy;
            if (Greenfoot.getRandomNumber(10) < 6)
            {
                enemy = new Soldier(100);
            }
            else
            {
                enemy = new Tank("Green", 400);
            }
 
            addObject(enemy, 0, 448);
            EnemiesHelper.add(enemy);
            spawnCountDown = 800;
            tellerSpawn++;
        }
        else
        {
            spawnCountDown--;
        }
danpost danpost

2016/11/10

#
I was thinking of something similar to your last idea -- except with the code in your world class; but I was not sure if had more than one world, My idea was to override the 'addObject', 'removeObject' and 'removeObjects' methods to maintain the list. Having a static list in your Towers class would then make sense. You should make sure that, at the beginning of each level or world, you re-initialize or clear the list. After a little thought, I've come to the realization the a separate helper class is a bit much. All the methods in it are one-liners which can be called directly when needed. So, I would stick with the list in the Towers class and replace all 'EnemiesHelper' with 'Towers.enemies'.
ZoeF ZoeF

2016/11/10

#
I wil give this idee a go and keep you updated if i have any issues with it.
danpost danpost

2016/11/10

#
ZoeF wrote...
I wil give this idee a go and keep you updated if i have any issues with it.
You will have one, unless you change the access modifier of the list from 'private' to 'public'.
You need to login to post a reply.