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

2014/6/22

add object if it doesnt exist

davemib123 davemib123

2014/6/22

#
Hi, trying to add Enemy(2) object if Enemy(2) does not exist in the world. My Enemy class has:
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Enemies within the game
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Mover
{
    private static final GreenfootImage Drone = new GreenfootImage("Drone.png");
    private static final GreenfootImage MiniBoss = new GreenfootImage("MiniBoss.png");
    private static final GreenfootImage Boss = new GreenfootImage("Boss.png");
    private int droneSelection = Greenfoot.getRandomNumber(3);
    private int life;
 
    public Enemy(int selection)
    {
        this.selection = selection;
        if (selection == 1)
        
            setImage(Drone);
            this.outOfBoundary = 50;
            this.life = 1;
        }
        else if (selection == 2)
        
            setImage(MiniBoss); 
            this.speed = (Greenfoot.getRandomNumber(2) + 2);
        
        else if (selection == 3)
        
            setImage(Boss);
            this.speed = 2;
        }
    }
 
    /**
     * Act - do whatever the Enemy wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        if (selection == 1)
        {
            switch (droneSelection)
            {
                case 1: moveLeftPattern1();
                this.speed = (Greenfoot.getRandomNumber(3) + 1);
                break;
                case 2: moveLeftPattern2();
                this.speed = (Greenfoot.getRandomNumber(3) + 1);
                break;
                default: moveLeft();
                this.speed = (Greenfoot.getRandomNumber(3) + 3);
                break;
            }
            fireEnemyMissile1();
            enemyBoundary();
        }
    }   
 
    /**
     * Remove enemy when no longer needed
     */
    private void enemyBoundary()
    {
        if (isTouching(Hero.class))
        {
            getWorld().addObject(new Explosion(1), getX(), getY());
            takeLife();
        }
        else if (getX() <= - outOfBoundary)
        {
            getWorld().removeObject(this);
        }
    }
 
    /**
     * Fire enemy missile 1 type
     */
    private void fireEnemyMissile1()
    {
        shootingInterval++;
        if (this.shootingInterval > 40)
        {
            this.shootingInterval = 0;
            Missiles fire = new Missiles(2);
            getWorld().addObject(fire, getX() - 50, getY() + 4);
        }
    }
 
    public void takeLife()
    {
        this.life -= 1;
        if (this.life <= 0)
        {
            getWorld().removeObject(this);
        }
    }
}
I've had a look at list and getObjects but it doesnt do as I need as it checks how much of Enemy.class are available. I want to check how much of just Enemy(2) object are in the World:
1
2
List Enemy2 = getObjects(Enemy.class); 
if (Enemy2.size() == 0) addObject(new Enemy(2), 100, 100);
Any help will be much appreciated.
danpost danpost

2014/6/22

#
First, you will have to decide whether your drone selection will be set by way of the argument of the constructor (which is not being assigned to the 'droneSelection' field as I believe you want it to) or by the number assigned to it when the 'droneSelection' field is declared using 'getRandomNumber'. After that, you will need to iterate through the list of Enemy objects in the world to see how many have a 'droneSelection' value of 2. You will need a 'get' method to return the value of the field in the Enemy class.
davemib123 davemib123

2014/6/23

#
Danpost, I think you got the wrong idea on what I was intending on doing. This is roughly what I want doing, but not sure on how to check if Enemy(2) instance actually exists in the level, I have this in my world class:
1
2
3
4
5
6
7
8
9
if (!Enemy(2))
        {
            miniBossSpawn++;
        }
        if (miniBossSpawn > 300)
        {
            addObject(new Enemy(2), 500, Greenfoot.getRandomNumber(500) + 22);
            miniBossSpawn = 0;
        }
davemib123 davemib123

2014/6/23

#
ive got it done it this way: I've separated into different classes and used getObjects to do the check. Not sure if its the best method, but it does work.
Kartoffelbrot Kartoffelbrot

2014/6/23

#
If I understood correctly you want to check if an actor is in a world. You could use it's getWorld() method. If it returns null the actor isn't in any world.
Kartoffelbrot Kartoffelbrot

2014/6/23

#
Oh I think now I've got what you mean. Yes splitting them up is the cleanest way to do it I suppose. But you could have used getObjects like this:
1
2
3
4
5
List<Enemy> enemies = getWorld().getObjects(Enemy.class);
int numOfEnemy2 = 0;//here the number of Enemy2 objects is stored
for(Enemy e : enemies)
    if(e.getSelection() == 2)
        numOfEnemy2++;
You only have to write the getSelection method, that returns the selection defined in the constructor. Edit: This is what danpost has said, only in code.
davemib123 davemib123

2014/6/23

#
Thanks guys. I did it like this:
1
2
3
4
5
6
7
8
9
if (getObjects(MiniBoss.class).size() == 0)
       {
           miniBossSpawn++;
       }
       if (miniBossSpawn > 750)
       {
           addObject(new MiniBoss(), 1110, Greenfoot.getRandomNumber(469) + 46);
           miniBossSpawn = 0;
       }
You need to login to post a reply.