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

2012/11/10

How can I spawn an actor in a random time and let him disappear after ten seconds time?

1
2
Yellowfoot Yellowfoot

2012/11/10

#
Hey guys. I've got a problem with programming PacMan: I want to implement the cherry. But I don't have any idea of how the cherry can spawn after a random time between 1 and 120 seconds and then disappears after 10 seconds. I tried to do it with "currentTimeMillis" but it didn't work. Hope you can help me.
SimpleJack SimpleJack

2012/11/10

#
you can use the Greenfoot.getRandomNumber (int num) which will create a number from 0 to whatever you input, so a random number between 1 and 120 would be :
int randomNum = 1 + Greenfoot.getRandomNumber(119);
and as for dissapearing, one method I usually use is to create a counter:
int counter = 0;

public void act()
{
counter++;
removeCherry();
}

public void removeCherry()
{
    if (counter == 300) // the act method is called 30 times a second, 300 = 10 seconds.
    {
       getWorld().removeObject(yourobject);
    }
}
SPower SPower

2012/11/10

#
@SimpleJack about this:
// the act method is called 30 times a second, 300 = 10 seconds.
isn't true at all, it's called 62 times per second in an average scenario, so instead of this:
if (counter == 300)
this is better
if (counter == 600)
and this is even more precise
if (counter == 620)
Yellowfoot Yellowfoot

2012/11/10

#
Thank you for quick answering! But i still have some problems: I still don't know how to spawn the object after this random time. And when i wrote your code "getWorld().removeObject(Kirsche.class);" the message "Cannot find symbol - method "getWorld()" appears. Hope you can help me.
SPower SPower

2012/11/10

#
In which class did you put the code? You have to put it in a class which is a subclass of Actor.
Yellowfoot Yellowfoot

2012/11/10

#
Thank you for quick answering again :) I put it in the Subclass "Kirsche" (Kirsche = Cherry in German). Then the message "method removeObject in class greenfoot.World cannot be applied to given types; required: greenfoot.Actor; found: java.lang.Class<Kirsche>; reason: actual argument java.lang.Class<Kirsche> cannot be converted to greenfoot.Actor by method invocation conversation" appeared. After that i tried to put the code in the world and the other message (in the post before) appeared.
SPower SPower

2012/11/10

#
Is Kirsche a subclass of Actor?
Yellowfoot Yellowfoot

2012/11/10

#
Yes, "Kirsche" is a subclass of Actor.
SPower SPower

2012/11/10

#
Can you share the code?
SimpleJack SimpleJack

2012/11/10

#
@ SPower My bad you were right, it has been a while since I used this program.
Yellowfoot Yellowfoot

2012/11/10

#
@SPower Of course I can :)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Kirsche here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Kirsche extends Actor
{
    private int counter;
    /**
     * Act - do whatever the Kirsche wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void Kirsche()
    {
         counter = 0;
    }
     public void act()
    { 
        counter++;
        removeCherry();  
        int randomNum = 1 + Greenfoot.getRandomNumber(119);
    }
     
     public void removeCherry()  
    {  
        if (counter == 620) // the act method is called 30 times a second, 300 = 10 seconds.  
        {  
             getWorld().removeObject(Kirsche.class);
        }  
    } 
    

}
SPower SPower

2012/11/10

#
Well, maybe changing it to this helps:
getWorld().removeObject(this);
since I'm not very comfortable with this :) :
getWorld().removeObject(Kirsche.class);
Yellowfoot Yellowfoot

2012/11/10

#
No syntax errors, thank you very much ;) My next problem is that I don't know how to let the Cherry spawn after that random time I (or better SimpleJack :) ) generated with
int randomNum = 1 + Greenfoot.getRandomNumber(119);  
Hope you can help me.
SPower SPower

2012/11/10

#
To put it at a new location, you can do something like this:
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
setLocation(x,y);
note that it's also possible for the Cherry to be on another object, to prevent it from doing that, use this instead:
int x = Greenfoot.getRandomNumber(getWorld().getWidth());
int y = Greenfoot.getRandomNumber(getWorld().getHeight());
if (getObjects(Actor.class, x, y).isEmpty())
    setLocation(x,y);
danpost danpost

2012/11/10

#
The statement you are concerned with, namely
int randomNum = 1 + Greenfoot.getRandomNumber(119);
should not be in the Kirsche class, as that code will not execute until the cherry is in the world. Best would be to have it in your sub-class of World. Have its value decremented each act cycle and check its value for zero right afterwards. When it equals zero, add the cherry into the world. Only run this code segment if the value of randomNumber is currently greater than zero.
if (randomNumber > 0)
{
    randomNumber--;
    if (randomNumber == 0)
    {
        addObject(new Kirsche(), 'x-location', 'y-location');
    }
}
A better name for 'randomNumber' might be something like 'cherryTimer' and to include 120 in the range of possibilities, change 119 to 120. EDIT: this field should be declared in the class as an instance variable. Its value can be assigned in the declaration of the variable or in the world constructor.
There are more replies on the next page.
1
2