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

2018/4/16

AddObject programatically by time, help please.

TomazVDSN TomazVDSN

2018/4/16

#
I want the program put an object in the World every 5 seconds or whatever elapsed time I set. I created a method Act within the Dojo class but I cannot figure this out.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Karate fighter will kick watermelons
 *
 * @author Tom
 * @version 1.0
 */
public class Dojo extends World
{
 
    long initialTime = System.currentTimeMillis();
    long elapsedTime = 0;
    /**
     * Constructor for objects of class Dojo.
     *
     */
    public Dojo()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
         
        addObject(new Standing(), 250, 270);
         
        WaterMelon w1 = new WaterMelon();
        addObject(w1, 5, 180);
         
        WaterMelon w2 = new WaterMelon();
        addObject(w2, 5, 250);
 
    }
     
    public void Act(){
        addMelonToWorld(5000);
         
    }
    
    public void addMelonToWorld(long elipsedTimeSet){
     
        elapsedTime = System.currentTimeMillis() - initialTime;
         
        if(elapsedTime >elipsedTimeSet){
                        
            WaterMelon wm = new WaterMelon();
            addObject(wm, 5, 330);
             
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
             
        }
         
    }
}
danpost danpost

2018/4/16

#
TomazVDSN wrote...
I created a method Act within the Dojo class but I cannot figure this out.
Yeah, well you spelled act wrong (use lowercase only).
TomazVDSN TomazVDSN

2018/4/16

#
My gosh! - Thank you! , it is working fine now!
TomazVDSN TomazVDSN

2018/4/16

#
Well, now I tried to add three WaterMelon and each in different Y-position, but the method act reads only the first addMelonToWorld. Should I create a addMelonToWorld class instead and instanciate a object under the World act method?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public void act(){
        addMelonToWorld(5000,"top");
        addMelonToWorld(7000,"center");
        addMelonToWorld(9000,"bottom");
    }
    
    public void addMelonToWorld(long elipsedTimeSet,String melonWaterY)
    {
        elapsedTime = System.currentTimeMillis() - initialTime;
        if(elapsedTime >= elipsedTimeSet)
        {     
            if(melonWaterY=="top")   {addObject(new WaterMelon(), 5, 180);}             
            if(melonWaterY=="center"){addObject(new WaterMelon(), 5, 260);}
            if(melonWaterY=="bottom"){addObject(new WaterMelon(), 5, 330);}
             
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
        }
    }
danpost danpost

2018/4/16

#
TomazVDSN wrote...
Well, now I tried to add three WaterMelon and each in different Y-position, but the method act reads only the first addMelonToWorld. Should I create a addMelonToWorld class instead and instanciate a object under the World act method? << Code Omitted >>
No. adding another class just makes things more complicated. You fix what code you have. If you want random placement a WaterMelon object, each placed in the world at separate times, then you need to generate a random value to determine where one should go. Right now, once the time has elapsed and the act places the top one, the timer is reset, so the next the the timer expires, the act will place another top one and reset the time again. So only top ones will be placed.
TomazVDSN TomazVDSN

2018/4/17

#
Thank again for helping me out. Well, I wrote a random number generator from 1 to 5 and referred to 4 possible options. Good enough for the moment.
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
public void addMelonToWorld(long elipsedTimeSet){
         
        int y = (int)(Math.random()*4)+1;
                
        elapsedTime = System.currentTimeMillis() - initialTime;
         
        if(elapsedTime >= elipsedTimeSet && y==1){
             
            addObject(new WaterMelonLeft(), 5, 190);
            addObject(new WaterMelonRight(), 555, 235);
                         
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
         
        }
         
        if(elapsedTime >= elipsedTimeSet && y==2){
             
            addObject(new WaterMelonRight(), 555, 190);
            addObject(new WaterMelonLeft(), 5, 235);
                         
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
        }
         
        if(elapsedTime >= elipsedTimeSet && y==3){
             
            addObject(new WaterMelonLeft(), 5, 190);
            addObject(new WaterMelonRight(), 555, 330);
                         
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
        }
         
        if(elapsedTime >= elipsedTimeSet && y==4){
             
            addObject(new WaterMelonRight(), 555, 235);
            addObject(new WaterMelonLeft(), 5, 330);
                         
            elapsedTime =0;
            initialTime = System.currentTimeMillis();
        }
    }
danpost danpost

2018/4/17

#
Seems like a lot of code for a timer to add two objects.
1
2
3
4
5
6
7
8
9
10
addMelon()
{
    if (System.currentTimeMillis()-initialTime >= elipsedTimeSet)
    {
        java.util.List<Integer> ys = java.util.Arrays.asList(new Integer[]{ 190, 235, 330 };
        addObject(new WaterMelonLeft(), 5, ys.remove(Greenfoot.getRandomNumber(3)));
        addObject(new WaterMelonRight(), 555, ys.get(Greenfoot.getRandomNumber(2)));
        initialTime = System.currentTimeMillis();
    }
}
TomazVDSN TomazVDSN

2018/4/17

#
It is a great iteration which I need more time to understand. Let me study it and get back to you. Thanks.
danpost danpost

2018/4/17

#
TomazVDSN wrote...
It is a great iteration which I need more time to understand. Let me study it and get back to you. Thanks.
Line 3 determines if the timer has expired. Line 5 creates a list of the 3 possible y locations to spawn an actor at. Line 4 spawns an actor to the left at one of those y locations chosen at random, removing that y value from the list. Line 5 spawns a second actor to the right at one of the remaining y locations chosen at random. Finally, line 8 resets the timer.
TomazVDSN TomazVDSN

2018/4/17

#
thanks
You need to login to post a reply.