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

2016/5/26

Delay method creating problems with objects moving after spawning

CrimsonGuitarist CrimsonGuitarist

2016/5/26

#
Hi. I'm working on a project where I spawn objects that spawn at specific times and move afterwards, but when I use Greenfoot.delay() I end up creating all of the objects first and then they move. How can I fix this?
public void playSong()
    {
        pauseTime = 1000;
        World world = getWorld();
        noteA notea = new noteA();
        noteS notes = new noteS();
        noteD noted = new noteD();
        noteF notef = new noteF();
        noteG noteg = new noteG();

        Greenfoot.delay(50);
        world.addObject(notea, 50, 50);
        Greenfoot.delay(25);
        world.addObject(notes, 150, 50);
    }
Super_Hippo Super_Hippo

2016/5/26

#
Don't use the 'delay' method. Instead, use a variable as a timer. Increase it and add the objects when the variable reaches a certain value.
SPower SPower

2016/5/26

#
The reason why they don't move is because you're effectively 'pausing' the scenario in some way. It does take time for the object to be added (i.e. the user sees a time gap between them being added), but while the program waits for them to be added, it doesn't do anything at all: it's halting. What you need to do is keep a variable for delay as an instance variable:
private int delay = 0;
private boolean waiting = false; // this tells if we're using the timer
Then, in playSong, after you add the first object, do this:
waiting = true;
Then, in act, add the following:
if (waiting) {
    delay++;
    if (delay >= limit) { // you'll have to pick a number for limit
        // add new object
        delay = 0;
        // you may set waiting to true or false depending on if you want to add more objects or just one
    }
}
The way you pick the number for 'limit' is through simple trial and error until you get the result you want :) }
danpost danpost

2016/5/26

#
@SPower, if it is done the way you suggest, there will have to be a way to determine beforehand which note is to be added when one is to be added. That means the a representation of the order of notes and the wait time between them must be listed in a string or with the use of one or more arrays or lists. My suggestion for this is to use a song string that has letters for the notes and numbers for the times. The numbers can be reduced to one digit numbers by dividing the wait time by a common factor (in the code given by CrimsonGuitarist, that factor would be 25). Then the playSong code would look something like this:
// instance fields
private String songA = "a1s2";
private String songPlaying = "";
private int songTimer;

// to start song (unconditionally in constructor or by some method upon some condition)
songPlaying = songA;

// the playSong method (called unconditionally from act method)
public void playSong()
{
    if ("".equals(songPlaying)) return; // song playing?
    if (songTimer > 0) // between notes?
    {
        songTimer--;
        return;
    }
    int index = "asdfg".indexOf(songPlaying.charAt(0));
    Actor note = null;
    if (index == 0) note = new noteA();
    if (index == 1) note = new noteS();
    if (index == 2) note = new noteD();
    if (index == 3) note = new noteF();
    if (index == 4) note = new noteG();
    getWorld().addObject(note, 50+index*100, 50);
    songTimer = 25*"0123456789".indexOf(songPlaying.charAt(1));
    songPlaying = songPlaying.substring(2);
}
You need to login to post a reply.