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

2016/12/22

getObjectsInRange check vs ArrayList

ZoeF ZoeF

2016/12/22

#
Small explenation of my issue i have enemies who spawn on my world threw the map class (sub of World) There i make a array list with the enemies as they spawn adding them one by another. i have method here to send threw the latest list. Then i have my towers class where my enemies around my tower go in the list from getObjectsInRange Then i want to go about getting a check for wich enemies was spawnd first and if that one is in my Range. If not check if the 2nd one that spawnd is in the range and so on and forth. Until i have a enemie to shoot at.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
List<Enemies> enemies = getObjectsInRange(200, Enemies.class);
 
                // Als enmies niet leeg is.
                if (!enemies.isEmpty())
                {
                    emptyList = false;
                    for(int i = 0; i < myEnemies.size(); i++)
                    {
                        for(int j = 0; j < enemies.size(); j++)
                        {
                            if(myEnemies.get(i) == enemies.get(j))
                            {
                                enemie = enemies.get(j);
                            }
                        }
                    }
                    turnTowards(enemie.getX(), enemie.getY());
                }
                else
                {
                    emptyList = true;
                }
danpost danpost

2016/12/22

#
It might be easier to just assign a spawning number to each Enemies object. Then, the tower can just get objects in range and determine the lowest spawn value:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/** in Enemies class */
// class field
public static int spawnId; // spawn value to assign next
// instance field
public int id = spawnId++;
 
/** in map class */
// in constructor
Enemies.spawnId = 0;
 
/** in tower class */
// to turn towards first spawned in range
Enemies firstActor = null;
for (Object obj : getObjectsInRange(200, Enemies.class))
{
    Enemies enemy = (Enemies)obj;
    if (firstActor == null || enemy.id < firstActor.id) firstActor = enemy;
}
if (firstActor != null)
{
    turnTowards(firstActor.getX(), firstActor.getY());
    // etc.
}
ZoeF ZoeF

2016/12/22

#
Solved it, tnx
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
private void targetShoot()
    {
        // Lijst van klasse Enemies in de radius van 200
        List<Enemies> enemies = getObjectsInRange(200, Enemies.class);
        Enemies toShoot = null;
        // Als enmies niet leeg is.
        if (!enemies.isEmpty())
        {
            // Ga door de lijst enemies van 0 naar ... En steek het huidig object van het type Enemies in target
            for(Enemies target : enemies)
            {
                // als toshoot null is of de nummer van target < dan de nummer van toshoot
                if(toShoot == null || (target.getMyNumber() < toShoot.getMyNumber()))
                {
                    toShoot = target;
 
                    emptyList = false;
                    //enemie = enemies.get(0);
                    turnTowards(toShoot.getX(), toShoot.getY());
                }
            }
        }
        else
        {
            emptyList = true;
        }
    }
You need to login to post a reply.