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

2017/12/1

How do I make enemies not spawn within a given range of my main character

1
2
Brantgarron Brantgarron

2017/12/1

#
I have a problem where the main actor is going throughout the world and as he scrolls up enemies randomly spawn in a given area. What I am trying to do is to in some sort delay the spawning of the enemy if the character is in close range to where the enemy might spawn. Any help is greatly appreciated. Thanks in advance.
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class doodleWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class doodleWorld  extends World
{
    private boolean bStarted, started = false;
    private int enemySpawnTimeMax= 1000*4, enemySpawnTimer=enemySpawnTimeMax;
    private int enemy1SpawnTimeMax= 1000*3, enemy1SpawnTimer=enemy1SpawnTimeMax;
    private int enemy2SpawnTimeMax= 1000*2, enemy2SpawnTimer=enemy2SpawnTimeMax;
    private boolean once;
    private int transparency = 0;
    public int scrollSpeed;
    public boolean scroll;
    public boolean fall;
    private boolean hitEnemy;
    public boolean ended;
    private int restart;
    //private int enemySpawnTimer;
    public int height = 0;
    public int doodleX;
    /**
     * Constructor for objects of class doodleWorld.
     *
     */
    public doodleWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(300,400, 1,false);
 
        addObject(new ground(), 56, 317);
        addObject(new doodler(false), 56,200);
        setPaintOrder(scoreKeeper.class, doodler.class, ground.class, ammo.class);
        setBackground("title.png");
        getBackground().setTransparency(255);
        once = true;
        started = false;
        height = 0;
        fall = false;
        hitEnemy = false;
        ended = false;
    }
 
    public void act()
    {
        if(bStarted==false & Greenfoot.mouseMoved(this))
        {
            bStarted = true;
        }
 
        if(started==false & bStarted==true)
        {
            MouseInfo mouse = Greenfoot.getMouseInfo();
 
            if(Greenfoot.mouseClicked(this))
            {
                if(mouse.getX()>=45 & mouse.getX()<=148
                && mouse.getY()>=104 & mouse.getY()<=138)
                {
                    started = true;
                }
            }
        }
        if(started==true & once==true)
        {
            setBackground("paper.png");
            cleanup();
            once = false;
            setLevel(1);
        }
        if(fall)
        {
            end();
         
        }
        if(hitEnemy)
        {
            end();
        }
        if (--enemySpawnTimer == 0)
        {
            enemySpawnTimer = enemySpawnTimeMax;
 
            addObject(new Enemy(), Greenfoot.getRandomNumber(290), 30);
        }
        if (--enemy1SpawnTimer == 0)
        {
            enemy1SpawnTimer = enemy1SpawnTimeMax;
 
            addObject(new enemy1(), Greenfoot.getRandomNumber(35)+30, 35);
        }
        if (--enemy2SpawnTimer == 0)
        {
            enemy2SpawnTimer = enemy2SpawnTimeMax;
 
            addObject(new enemy2(), Greenfoot.getRandomNumber(125)+100, 50);
        
        //runEnemySpawnTimer();
    }
     
    /*private void runEnemySpawnTimer()
    {
        enemySpawnTimer = (enemySpawnTimer+1)%500; // adjust '300' as desired
    if (enemySpawnTimer == 0)
    spawnEnemy();
    }
  
    private void spawnEnemy()
    {
        addObject(new Enemy(), 200,50);
         
    }*/
     
     
     
    public void cleanup()
    {
        removeObjects(getObjects(doodler.class));
        removeObjects(getObjects(ground.class));
        removeObjects(getObjects(Enemy.class));
         
    }
 
    public void setLevel(int level)
    {
        switch(level)
        {
            case 1: gamePlay(); break;
        }
    }
 
    public void gamePlay()
    {
        addObject(new doodler(), getWidth()/2, 300);
        addObject(new ground(false), 28,391);
        addObject(new ground(false), 83,391);
        addObject(new ground(false), 83+55,391);
        addObject(new ground(false), 83+55+55,391);
        addObject(new ground(false), 83+55+55+55,391);
        addObject(new ground(false), 83+55+55+55+55,391);
         
        addObject(new ground(), Greenfoot.getRandomNumber(300), 250);
        addObject(new ground(), Greenfoot.getRandomNumber(300), 150);
        addObject(new ground(), Greenfoot.getRandomNumber(300), 50);
 
        addObject(new scoreKeeper(), 253,385);
    }
     
    public void end()
    {
        cleanup();
        /*started=false;
        bStarted=false;
        addObject(new ground(), 56, 317);
        addObject(new doodler(false), 56,200);
 
        setBackground("title.png");
        getBackground().setTransparency(255);
        once = true;
        started = false;
        height = 0;
        fall = false;
        ended = false;*/
        addObject(new GameOver(),getWidth()/2,getHeight()/2);
         
         
         
         
    }
}
Here is the enemy code
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
   double ys, xs;
   int xSpeed = 3;
   int x_pos, y_pos;
 
    public void act()
    {
         
         if(((doodleWorld) getWorld()).scroll==true)
        {
            scroll(((doodleWorld) getWorld()).scrollSpeed);
        }
        if(((doodleWorld) getWorld()).fall==true)
        {
            fall(((doodleWorld) getWorld()).scrollSpeed);
        }
 
        if(y_pos < 0)
        {
            getWorld().removeObject(this);
            return;
        }
 
        move();
        checkEdges();
         
    }   
    public void fall(int speed)
    {
        if(((doodleWorld) getWorld()).ended==false)
        {
            y_pos = y_pos + speed;
            setLocation(x_pos, y_pos);
        }
    }
    protected void addedToWorld(World world)
    {
        x_pos = getX();
        y_pos = getY();
    }
    public void scroll(int speed)
    {
        if(speed>0)
        {
            y_pos = y_pos + speed;
            setLocation(getX(), y_pos);
        }
    }
    public void fall()
    {
        ((doodleWorld) getWorld()).fall = true;
        ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
         
    }
    /*private void objectTooClose(World world)
    {
        if ( ! getObjectsInRange(200, Enemy.class).isEmpty() || ! getObjectsInRange(200, doodler.class).isEmpty() )
        {
            if (Enemy.class != null)
            getWorld().removeObject(this);
 
        }
    }*/
     
    public void move()
    {
        move(xSpeed);
    }
     
    public void checkEdges()
    {
        if(getX() >= 260)
        {
            xSpeed = -3;
        }
        if(getX() <= 35)
        {
            xSpeed = 3;
        }
    }
}
danpost danpost

2017/12/1

#
1
2
3
4
5
6
7
8
protected void addedToWorld(World world)
{
    if (! getObjectInRange(100, doodler.class).isEmpty())
    {
        world.removeObject(this);
        world.addObject(this, Greenfoot.getRandomNumber(world.getWidth()), Greenfoot.getRandomNumber(world.getHeight()));
    }
}
Add the above method to the class of the actor you do not want to spawn near the doodler. You can change the range (the '100' on line 3) to suit your needs.
Brantgarron Brantgarron

2017/12/2

#
danpost wrote...
1
2
3
4
5
6
7
8
protected void addedToWorld(World world)
{
    if (! getObjectInRange(100, doodler.class).isEmpty())
    {
        world.removeObject(this);
        world.addObject(this, Greenfoot.getRandomNumber(world.getWidth()), Greenfoot.getRandomNumber(world.getHeight()));
    }
}
Add the above method to the class of the actor you do not want to spawn near the doodler. You can change the range (the '100' on line 3) to suit your needs.
would you mind explaining what that does just so that I can lern myself
Brantgarron Brantgarron

2017/12/2

#
@danpost It compiled but the enemy still spawned near my doodler (main character)
Brantgarron Brantgarron

2017/12/2

#
I believe it is something in my world class which is adding the enemy regardless of the range
danpost danpost

2017/12/2

#
Brantgarron wrote...
would you mind explaining what that does just so that I can lern myself
Greenfoot automatically calls the method one time when an actor of the class is added into a world. Line 3 checks to see if the list of doodlers within the given range is not empty. The block of code (lines 2 through 8 is executed if there is a doodler within the given range. Line 5 immediately removes the actor from the world and line 6 adds it back into the world at a new location. This should cause the method to be called again, checking to hopefully find that no doodler is within the given range. This will continue until no doodler is within the given range.
Brantgarron Brantgarron

2017/12/2

#
danpost wrote...
Brantgarron wrote...
would you mind explaining what that does just so that I can lern myself
Greenfoot automatically calls the method one time when an actor of the class is added into a world. Line 3 checks to see if the list of doodlers within the given range is not empty. The block of code (lines 2 through 8 is executed if there is a doodler within the given range. Line 5 immediately removes the actor from the world and line 6 adds it back into the world at a new location. This should cause the method to be called again, checking to hopefully find that no doodler is within the given range. This will continue until no doodler is within the given range.
Unfortunately, enemies are still being added regardless of where doodler is at. In my world class I have three different enemies spawning at various times. Instead what I think needs to be done is to restrict the time they spawn to be a random interval every 20 or thirty seconds I just don't know how to accomplish that
danpost danpost

2017/12/3

#
Brantgarron wrote...
what I think needs to be done is to restrict the time they spawn to be a random interval every 20 or thirty seconds I just don't know how to accomplish that
How does that help in where they are placed in the world? Please show your revised Enemy class code.
Brantgarron Brantgarron

2017/12/3

#
danpost wrote...
Brantgarron wrote...
what I think needs to be done is to restrict the time they spawn to be a random interval every 20 or thirty seconds I just don't know how to accomplish that
How does that help in where they are placed in the world? Please show your revised Enemy class code.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
   double ys, xs;
   int xSpeed = 3;
   int x_pos, y_pos;
 
    public void act()
    {
         
         if(((doodleWorld) getWorld()).scroll==true)
        {
            scroll(((doodleWorld) getWorld()).scrollSpeed);
        }
        if(((doodleWorld) getWorld()).fall==true)
        {
            fall(((doodleWorld) getWorld()).scrollSpeed);
        }
 
        if(y_pos < 0)
        {
            getWorld().removeObject(this);
            return;
        }
 
        move();
        checkEdges();
         
    }   
    public void fall(int speed)
    {
        if(((doodleWorld) getWorld()).ended==false)
        {
            y_pos = y_pos + speed;
            setLocation(x_pos, y_pos);
        }
    }
    protected void addedToWorld(World world)
    {
        x_pos = getX();
        y_pos = getY();
    }
    public void scroll(int speed)
    {
        if(speed>0)
        {
            y_pos = y_pos + speed;
            setLocation(getX(), y_pos);
        }
    }
    public void fall()
    {
        ((doodleWorld) getWorld()).fall = true;
        ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
         
    }
    private void objectTooClose(World world)
    {
        if ( ! getObjectsInRange(200, Enemy.class).isEmpty() || ! getObjectsInRange(200, doodler.class).isEmpty() )
        {
            if (Enemy.class != null)
            getWorld().removeObject(this);
 
        }
    }
     
    public void move()
    {
        move(xSpeed);
    }
    protected void dontAddToWorld(World world)
    {
    if (! getObjectsInRange(500, doodler.class).isEmpty())
    {
        world.removeObject(this);
        world.addObject(this, Greenfoot.getRandomNumber(world.getWidth()), Greenfoot.getRandomNumber(world.getHeight()));
    }
    }
    public void checkEdges()
    {
        if(getX() >= 260)
        {
            xSpeed = -3;
        }
        if(getX() <= 35)
        {
            xSpeed = 3;
        }
    }
}
Brantgarron Brantgarron

2017/12/3

#
What I think is the problem (correct if worng) is that in my world class - posted above, I have a timer that spawns enemies every couple of seconds. I think that I need to incorporate this code with the restrictions in pixels near to be in the world class or adjust "act method" in my world....what do you think
danpost danpost

2017/12/3

#
Remove the two methods 'dontAddToWorld' (lines 78 to 85) and 'objectTooClose' (lines 64 to 72). Then, change the 'addedToWorld' method (lines 45 to 49) to this:
1
2
3
4
5
6
7
8
9
10
11
12
protected void addedToWorld(World world)
{
    if (! getObjectsInRange(500, doodler.class).isEmpty())
    {
        world.removeObject(this);
    }
    else
   {
        x_pos = getX();
        y_pos = getY();
    }
}
Brantgarron Brantgarron

2017/12/3

#
danpost wrote...
Remove the two methods 'dontAddToWorld' (lines 78 to 85) and 'objectTooClose' (lines 64 to 72). Then, change the 'addedToWorld' method (lines 45 to 49) to this:
1
2
3
4
5
6
7
8
9
10
11
12
protected void addedToWorld(World world)
{
    if (! getObjectsInRange(500, doodler.class).isEmpty())
    {
        world.removeObject(this);
    }
    else
   {
        x_pos = getX();
        y_pos = getY();
    }
}
I now get this error
1
2
3
4
5
6
7
8
9
10
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
    at greenfoot.Actor.failIfNotInWorld(Actor.java:711)
    at greenfoot.Actor.getX(Actor.java:164)
    at Enemy.addedToWorld(Enemy.java:51)
    at greenfoot.World.addObject(World.java:428)
    at doodleWorld.act(doodleWorld.java:90)
    at greenfoot.core.Simulation.actWorld(Simulation.java:610)
    at greenfoot.core.Simulation.runOneLoop(Simulation.java:545)
    at greenfoot.core.Simulation.runContent(Simulation.java:221)
    at greenfoot.core.Simulation.run(Simulation.java:211)
danpost danpost

2017/12/3

#
Brantgarron wrote...
I now get this error < Error Trace Omitted >
Again, show the revised code for the Enemy class.
Brantgarron Brantgarron

2017/12/3

#
danpost wrote...
Brantgarron wrote...
I now get this error < Error Trace Omitted >
Again, show the revised code for the Enemy 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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class enemy here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
   double ys, xs;
   int xSpeed = 3;
   int x_pos, y_pos;
 
    public void act()
    {
         
         if(((doodleWorld) getWorld()).scroll==true)
        {
            scroll(((doodleWorld) getWorld()).scrollSpeed);
        }
        if(((doodleWorld) getWorld()).fall==true)
        {
            fall(((doodleWorld) getWorld()).scrollSpeed);
        }
 
        if(y_pos < 0)
        {
            getWorld().removeObject(this);
            return;
        }
 
        move();
        checkEdges();
         
    }   
    public void fall(int speed)
    {
        if(((doodleWorld) getWorld()).ended==false)
        {
            y_pos = y_pos + speed;
            setLocation(x_pos, y_pos);
        }
    }
    protected void addedToWorld(World world)
    {
        if (! getObjectsInRange(200, doodler.class).isEmpty())
        {
            world.removeObject(this);
        }
        //x_pos = getX();
        //y_pos = getY();
    }
    public void scroll(int speed)
    {
        if(speed>0)
        {
            y_pos = y_pos + speed;
            setLocation(getX(), y_pos);
        }
    }
    public void fall()
    {
        ((doodleWorld) getWorld()).fall = true;
        ((doodleWorld) getWorld()).scrollSpeed = (int) -ys;
         
    }
    /*private void objectTooClose(World world)
    {
        if ( ! getObjectsInRange(200, Enemy.class).isEmpty() || ! getObjectsInRange(200, doodler.class).isEmpty() )
        {
            if (Enemy.class != null)
            getWorld().removeObject(this);
 
        }
    }*/
    public void move()
    {
        move(xSpeed);
    }
    /*protected void dontAddToWorld(World world)
    {
    if (! getObjectsInRange(500, doodler.class).isEmpty())
    {
        world.removeObject(this);
        world.addObject(this, Greenfoot.getRandomNumber(world.getWidth()), Greenfoot.getRandomNumber(world.getHeight()));
    }
    }*/
    public void checkEdges()
    {
        if(getX() >= 260)
        {
            xSpeed = -3;
        }
        if(getX() <= 35)
        {
            xSpeed = 3;
        }
    }
}
Brantgarron Brantgarron

2017/12/3

#
I commented out some things instead of deleting....but I did add the new code
There are more replies on the next page.
1
2