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

2017/12/25

Object should spawn randomly

1
2
MetinPlays MetinPlays

2017/12/25

#
Hello, i need help with my program... I want one object of my game to spawn randomly on the field an remove after 1 second and spawn again somewhere random. Thanks.
xbLank xbLank

2017/12/25

#
Some context code would be great.
xbLank xbLank

2017/12/25

#
I can leave you with a RandomSpawn Method that I am currently using to spawn Monsters randomly around the Y-axis tho. Just to give you an idea.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void act()
{
    if(spawnTime == 0)
            RandomSpawn();
    else spawnTime--;
}
private void RandomSpawn()
{
    addObject(new Mob(GetSpeed()),999,getRandomNumber(35,765));
    spawnTime = 50;
}
private int getRandomNumber(int start, int range)
{
    return start + (int)Greenfoot.getRandomNumber(range-start+1);
}
To remove the mob(your object) after one second I would go into the Mob(your object) class and put a timer in the act method. After 1 second it should just remove itself. Set the Spawn Timer after the first Spawn to 1 second as well and voilà, you did it.
danpost danpost

2017/12/25

#
You probably just need one actor in the world from the following class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class Peekaboo extends Actor
{
    private int timer;
 
    public void act()
    {
        timer = (timer+1)%60;
        if (timer == 0)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            setLocation(x, y);
        }
    }
}
The only behavior given in this class is what you had indicated was needed. Any other behavior will have to be added as needed. And, of course, the class name can be modified as desired.
MetinPlays MetinPlays

2017/12/25

#
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
  
 * Die Welt besteht aus 29 * 17 Feldern.
 */
 
public class Ozean extends World
{
    private static int zellenGroesse = 50;
    private static int xmax = 29;
    private static int ymax = 17;
 
    /** p = plain, U = Uboot, T = Schatztruhe, W = Wasserstrudel, S = Seestern R = UbootRivale, N = Piranha */    
    private char[][] planetAufbau =
        {
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','U','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','R','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
        };
 
    public Uboot uboot = new Uboot();
    public UbootRivale ubootrivale = new UbootRivale();
 
    /** Erschaffe eine Welt mit 29 * 17 Zellen. */
    public Ozean()
    {
        super(xmax, ymax, zellenGroesse);
        setBackground("images/wet-blue.jpg");
        setPaintOrder(String.class, Uboot.class, UbootRivale.class, Schatztruhe.class, Seestern.class, Eisberg.class, Piranha.class);
        setActOrder(Uboot.class, UbootRivale.class,Schatztruhe.class, Seestern.class,Eisberg.class, Piranha.class);
        Greenfoot.setSpeed(28);      
 
        /** Die Welt mit Objekten bevölkern */               
        removeObjects(getObjects(null));
        for (int py = 0; py <= ymax-1; py++)
        {
            for (int px = 0; px <= xmax-1; px++)
            {           
                if (planetAufbau[py][px] == 'U')
                {
                    Uboot uboot = new Uboot();
                    addObject(uboot, px, py);
                }
                if (planetAufbau[py][px] == 'R')
                {
                    UbootRivale ubootrivale = new UbootRivale();
                    addObject(ubootrivale, px, py);
                }
                if (planetAufbau[py][px] == 'T')
                {
                    Schatztruhe schatztruhe = new Schatztruhe();
                    addObject(schatztruhe, px, py);
                }
                if (planetAufbau[py][px] == 'S')
                {
                    Seestern seestern = new Seestern();
                    addObject(seestern, px, py);
                }
 
                 
                 
                if (planetAufbau[py][px] == 'E')
                {
                    Eisberg eisberg = new Eisberg();
                    addObject(eisberg, px, py);
                }
                if (planetAufbau[py][px] == 'N')
                {
                    Piranha piranha = new Piranha();
                    addObject(piranha, px, py);
                }      
            }
        }
 
    }
 
     
    }
So this is my world code, it is a bit different from the standard one.. when i paste any of your codes in my "Seestern" Class( german for seastar) it doesn't happen anything. Do I have to change something in my World class?
xbLank xbLank

2017/12/25

#
danpost wrote...
You probably just need one actor in the world from the following class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;
 
public class Peekaboo extends Actor
{
    private int timer;
 
    public void act()
    {
        timer = (timer+1)%60;
        if (timer == 0)
        {
            int x = Greenfoot.getRandomNumber(getWorld().getWidth());
            int y = Greenfoot.getRandomNumber(getWorld().getHeight());
            setLocation(x, y);
        }
    }
}
The only behavior given in this class is what you had indicated was needed. Any other behavior will have to be added as needed. And, of course, the class name can be modified as desired.
You are checking for 60 act iterations, not 60 seconds. Check this Post for further information.
MetinPlays MetinPlays

2017/12/25

#
So I don't have to change anything in my World class? I'm a bit confused now :D Do I have to change anything in the code of danpost?
xbLank xbLank

2017/12/25

#
You are talking about a "Seestern" Class like you are assuming we know the code. You should provide us with information. Post your "Seestern" Class please. danpost's code is correct. Just his timer is not.
MetinPlays MetinPlays

2017/12/25

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Seestern here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Seestern extends Actor
 
{
       
    public Seestern()
    {
        setImage("images/Seestern3.png");
        this.getImage().scale(50,50);
         
      
  
    }
  
 
}
Of course, sorry. I just added these two codes in so what do I have to do now?
xbLank xbLank

2017/12/25

#
Something like this in your World Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public class Ozean extends World
{
    private int spawnTime = 60,
                wait = 0;
    public void act()
    {
        if(wait == 0)
            RandomSpawn();
        else wait--;
    }
    private void RandomSpawn()
    {
        addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
    }
    private int getRandomNumber(int start, int range)
    {
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
    }
}
And something like this in your Actor Class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
  
public class Seestern extends Actor
{
    private int wait = 60
    public Seestern()
    {
        setImage("images/Seestern3.png");
        this.getImage().scale(50,50);
    }
    public void act()
    {
        if(wait == 0)
            getWorld().removeObject(this);
        else wait--;
    }
}
xbLank xbLank

2017/12/25

#
Keep in mind that we are talking about actor iterations here. We are not talking about 60 seconds. To use seconds instead of iterations please Check this post.
MetinPlays MetinPlays

2017/12/25

#
Yes it worked! but there are too much sea stars which spawn.. how can I set a max count of sea stars?
xbLank xbLank

2017/12/25

#
Do you really mean too many or just too fast? As I mentioned earlier: You are working with act iterations, not seconds. Assuming you actually mean that there are too many, you can run a counter for each 'Seestern' added to the World.
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
public class Ozean extends World
{
    private int spawnTime = 60,
                wait = 0,
                counter = 0,
                maxSeaStars = <yourLimit>;
    public void act()
    {
        if(wait == 0)
            RandomSpawn();
        else wait--;
    }
    private void RandomSpawn()
    {
        if(counter < maxSeaStars)
        {
            addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
            counter++;
        }
    }
    private int getRandomNumber(int start, int range)
    {
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
    }
}
MetinPlays MetinPlays

2017/12/25

#
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
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
  
 * Die Welt besteht aus 29 * 17 Feldern.
 */
 
public class Ozean extends World
{
    private static int zellenGroesse = 50;
    private static int xmax = 29;
    private static int ymax = 17;
    private int spawnTime = 60,
                wait = 0;
   private int counter = 0;
   private int maxSeesterne = 10;
                 
 
    /** p = plain, U = Uboot, T = Schatztruhe, W = Wasserstrudel, S = Seestern R = UbootRivale, N = Piranha */    
    private char[][] planetAufbau =
        {
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','U','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','S','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E','E'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','T'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','R','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
            {'p','p','p','p','p','p','p','p','p','p','p','S','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','N'},
            {'p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p','p'},
        };
 
    public Uboot uboot = new Uboot();
    public UbootRivale ubootrivale = new UbootRivale();
 
    /** Erschaffe eine Welt mit 29 * 17 Zellen. */
    public Ozean()
    {
        super(xmax, ymax, zellenGroesse);
        setBackground("images/wet-blue.jpg");
        setPaintOrder(String.class, Uboot.class, UbootRivale.class, Schatztruhe.class, Seestern.class, Eisberg.class, Piranha.class);
        setActOrder(Uboot.class, UbootRivale.class,Schatztruhe.class, Seestern.class,Eisberg.class, Piranha.class);
        Greenfoot.setSpeed(28);      
 
        /** Die Welt mit Objekten bevölkern */               
        removeObjects(getObjects(null));
        for (int py = 0; py <= ymax-1; py++)
        {
            for (int px = 0; px <= xmax-1; px++)
            {           
                if (planetAufbau[py][px] == 'U')
                {
                    Uboot uboot = new Uboot();
                    addObject(uboot, px, py);
                }
                if (planetAufbau[py][px] == 'R')
                {
                    UbootRivale ubootrivale = new UbootRivale();
                    addObject(ubootrivale, px, py);
                }
                if (planetAufbau[py][px] == 'T')
                {
                    Schatztruhe schatztruhe = new Schatztruhe();
                    addObject(schatztruhe, px, py);
                }
                if (planetAufbau[py][px] == 'S')
                {
                    Seestern seestern = new Seestern();
                    addObject(seestern, px, py);
                }
                 
 
                 
                 
                if (planetAufbau[py][px] == 'E')
                {
                    Eisberg eisberg = new Eisberg();
                    addObject(eisberg, px, py);
                }
                if (planetAufbau[py][px] == 'N')
                {
                    Piranha piranha = new Piranha();
                    addObject(piranha, px, py);
                }      
            }
        }
 
    }
    public void act()
    {
        if(wait == 0)
            RandomSpawn();
        else wait--;
    }
    private void RandomSpawn()
    {
        if (counter < maxSeesterne)
        {
        addObject(new Seestern(),getRandomNumber(0,xmax),getRandomNumber(0,ymax));
        counter++;
    }
     
    }
    private int getRandomNumber(int start, int range)
    {
        return start + (int)Greenfoot.getRandomNumber(range-start+1);
    }
 
     
    }
I've changed it. Now there are 10 seastars but now they are removing themselves very slowly and do not spawn again..
xbLank xbLank

2017/12/25

#
Mind uploading your whole project somewhere so I can take a look at it?
There are more replies on the next page.
1
2