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

2014/4/15

How can i make enemies spawn

1
2
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
Need help with spawning enemies, how can i do this....i want the enemies to spawn in a certain location after one has been shot....possibly spawn in the upper part but i don't want them to all spawn in the same location, what code should i write
danpost danpost

2014/4/15

#
It is much easier to relocate the shot enemy instead of removing it and creating a new one (this could be looked at as recycling the actor). The x and y coordinates of the new location can be randomly determined using the 'Greenfoot.getRandomNumber(int)' method. Refer to the Greenfoot API documentation for its use.
GamesGrinder1998 GamesGrinder1998

2014/4/15

#
danpost wrote...
It is much easier to relocate the shot enemy instead of removing it and creating a new one (this could be looked at as recycling the actor). The x and y coordinates of the new location can be randomly determined using the 'Greenfoot.getRandomNumber(int)' method. Refer to the Greenfoot API documentation for its use.
So what could i write??
danpost danpost

2014/4/15

#
Complete and put the following in the block of code where it has been determined that the actor has been shot:
1
2
3
int x = xMinimum+Greenfoot.getRandomNumber(/** xRange */);
int y = yMinimum+Greenfoot.getRandomNumber(/** yRange */);
setLocation(x, y);
You will have to supply the 'xMinimum', 'yMinimum', 'xRange' and 'yRange' values. The 'xRange' and 'yRange' values can be determined by the following: xRange = xMaximum - xMinimum yRange = yMaximum - yMinimum These determine where you want the 'new' (recycled) actor to be 'spawned' (re-located) at.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
Complete and put the following in the block of code where it has been determined that the actor has been shot:
1
2
3
int x = xMinimum+Greenfoot.getRandomNumber(/** xRange */);
int y = yMinimum+Greenfoot.getRandomNumber(/** yRange */);
setLocation(x, y);
You will have to supply the 'xMinimum', 'yMinimum', 'xRange' and 'yRange' values. The 'xRange' and 'yRange' values can be determined by the following: xRange = xMaximum - xMinimum yRange = yMaximum - yMinimum These determine where you want the 'new' (recycled) actor to be 'spawned' (re-located) at.
thanks i put in random values and it didn't seem to work, here is my code for the fireball that kills the enemy. i tried it on one of the type of enemies...i'm not sure if i put it right
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
import greenfoot.*; 
 
public class Fireball extends Player
{
    private int direction, speed;
 
    public Fireball(int dir)
    {
        direction = 270;
        speed = 7;
    }
 
    public void act()
    {
        setRotation(direction);
        move(speed);
        disappear();
    }   
 
    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 200+Greenfoot.getRandomNumber(500);
            int y = 200+Greenfoot.getRandomNumber(500); 
            setLocation(x, y); 
            return;
        }   
 
        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            return;
        }
 
        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            return;
        }
 
        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
                return;
            }
        }   
    }
}   
danpost danpost

2014/4/16

#
You do not want to set the location of the fireball at line 41; you want to re-locate the BugEnemy object. Use
1
BugEnemy.setLocation(x, y);
Your x and y values will have your BugEnemy 're-spawn' anywhere except for a 200x200 rectangular area at the top-left corner of your world window. I am not sure that is what you wanted.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
You do not want to set the location of the fireball at line 41; you want to re-locate the BugEnemy object. Use
1
BugEnemy.setLocation(x, y);
Your x and y values will have your BugEnemy 're-spawn' anywhere except for a 200x200 rectangular area at the top-left corner of your world window. I am not sure that is what you wanted.
thanks, what is the number in the xrange and yrange for?
danpost danpost

2014/4/16

#
Let us say you had a horizon across the width of your world at a y coordinate of 400 (that is, ground below the 400 mark, where y > 400, and sky above the 400 mark, where y < 400) and you wanted to random place stars in the sky. Your minimum x coordinate value would be zero and the maximum x coordinate value would be the width of the world (that is the most basic situation). Your minimum y coordinate value would be, again, zero and the maximum y coordinate value would be 400, at the horizon line (still pretty basic). The x and y values would be, in code (coding in the world class):
1
2
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(400);
The code above can be written in pseudo-code as follows:
1
2
x = minX+random(maxX-minX);
y = minY+random(maxY-minY);
where 'maxX-minX' and 'maxY-minY' are the size of the random ranges. With minx and minY being zero, it simplifies a lot when coding to what I have before it. Now, let us say that you have a GUI panel across the top of your world with buttons, stats, graphs, information, etc. and it goes all the way across the top and is 100 pixels high. You would not want to place stars in this area; so your minY will now be 100. and the code then becomes:
1
2
3
int y = 100+Greenfoot.getRandomNumber(300);
// in pseudo-code
y = 100+random(400-100);
The '400-100' is the size of the random range of y.
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
Let us say you had a horizon across the width of your world at a y coordinate of 400 (that is, ground below the 400 mark, where y > 400, and sky above the 400 mark, where y < 400) and you wanted to random place stars in the sky. Your minimum x coordinate value would be zero and the maximum x coordinate value would be the width of the world (that is the most basic situation). Your minimum y coordinate value would be, again, zero and the maximum y coordinate value would be 400, at the horizon line (still pretty basic). The x and y values would be, in code (coding in the world class):
1
2
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(400);
The code above can be written in pseudo-code as follows:
1
2
x = minX+random(maxX-minX);
y = minY+random(maxY-minY);
where 'maxX-minX' and 'maxY-minY' are the size of the random ranges. With minx and minY being zero, it simplifies a lot when coding to what I have before it. Now, let us say that you have a GUI panel across the top of your world with buttons, stats, graphs, information, etc. and it goes all the way across the top and is 100 pixels high. You would not want to place stars in this area; so your minY will now be 100. and the code then becomes:
1
2
3
int y = 100+Greenfoot.getRandomNumber(300);
// in pseudo-code
y = 100+random(400-100);
The '400-100' is the size of the random range of y.
ok thankyou i get it now....though i'm still having trouble with the powerup code and i'm starting to stress out cos i don't know anything, im such a NOOB!
danpost danpost

2014/4/16

#
Simply put, if you want to randomly place objects within a rectangle described by the two point (minX, minY)-(maxX, maxY), you would use the following for a random location (x, y):
1
2
int x = minX+Greenfoot.getRandomNumber(maxX-minX);
int y = minY+Greenfoot.getRandomNumber(maxY-minY);
GamesGrinder1998 GamesGrinder1998

2014/4/16

#
danpost wrote...
Simply put, if you want to randomly place objects within a rectangle described by the two point (minX, minY)-(maxX, maxY), you would use the following for a random location (x, y):
1
2
int x = minX+Greenfoot.getRandomNumber(maxX-minX);
int y = minY+Greenfoot.getRandomNumber(maxY-minY);
it's not working
danpost danpost

2014/4/16

#
GamesGrinder1998 wrote...
< Quote omitted > it's not working
What is not working? Show the code you are using.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
GamesGrinder1998 wrote...
< Quote omitted > it's not working
What is not working? Show the code you are using.
for some reason i tried it on one of thee enemies (bugenemy) but it doesn't spawn, i put in the fireball code cos thats where it declares that the enemy has been shot
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
import greenfoot.*; 
 
public class Fireball extends Player
{
    private int direction, speed;
 
    public Fireball(int dir)
    {
        direction = 270;
        speed = 7;
    }
 
    public void act()
    {
        setRotation(direction);
        move(speed);
        disappear();
    }   
 
    public void disappear()
    {
        Actor BugEnemy;
        BugEnemy = getOneObjectAtOffset(0, 0, BugEnemy.class);
        Actor InsectEnemy;
        InsectEnemy = getOneObjectAtOffset(0, 0, InsectEnemy.class);
        Actor Bee;
        Bee = getOneObjectAtOffset(0, 0, Bee.class);
        Actor DarkPower;
        DarkPower = getOneObjectAtOffset(0, 0, DarkPower.class);
        if (BugEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BugEnemy);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            int x = 100+Greenfoot.getRandomNumber(500); 
            int y = 100+Greenfoot.getRandomNumber(500); 
            BugEnemy.setLocation(x, y);
            return;
        }   
 
        if (InsectEnemy != null)
        {
            World world;
            world = getWorld();
            world.removeObject(InsectEnemy);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            return;
        }
 
        if (Bee != null)
        {
            World world;
            world = getWorld();
            world.removeObject(Bee);
            MyGame mygame = (MyGame)world;
            Counter counter = mygame.getCounter();
            counter.addScore();
            world.removeObject(this);
            return;
        }
 
        if (DarkPower != null)
        {
            World world;
            world = getWorld();
            world.removeObject(DarkPower);
            world.removeObject(this);
            return;
        } else {
            if(atWorldEdge())
            {
                getWorld().removeObject(this);
                return;
            }
        }   
    }
}   
danpost danpost

2014/4/17

#
Try removing line 34.
GamesGrinder1998 GamesGrinder1998

2014/4/17

#
danpost wrote...
Try removing line 34.
thanks it did work but the enemy respawned right on top of my starfighter, what would i change my numbers to considering that my world is 600 x 600, i have my score counter of the left top corner at the top that covers about 70-100 pixels down and my starfighter covers about 80-100 pixels from the bottom up
There are more replies on the next page.
1
2