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

2012/2/22

Class parameters

Gazzzah Gazzzah

2012/2/22

#
Hello, I am trying to make a simple spawn method that includes the class in the parameters. I'm not really sure how to specify it though. It would go something like:
public void spawn([parameter name] [actor name],int x, int y)
    {
        addObject (new [actor name](), x, y);
        addObject (new flash(), x, y);
    }
What goes in parameter name, or how can I modify it to work.
Gazzzah Gazzzah

2012/2/22

#
Or better yet if I could associate classes with numbers and use that in the parameters instead. That way I could have it calculate what to spawn randomly or based on another integer.
danpost danpost

2012/2/22

#
If you want a spawning of a randomly chosen object type, there is no need for the extra parameter as the random choice can be done within the method itself
public void spawn(int x, int y)
{
    switch(Greenfoot.getRandomNumber(3))
    {
        case 0: addObject(new Type0(), x, y); break;
        case 1: addObject(new Type1(), x, y); break;
        case 2: addObject(new Type2(), x, y); break;
    }
    addObject(new flash(), x, y);
}
The above is a method that randomly chooses between three Actor types (Type0, Type1, and Type2) and adds it to the world. I did not know if the flash was for just one type of actor or all, but here, I added a flash no matter what type actor was added.
Gazzzah Gazzzah

2012/2/22

#
YES! That's what I was after. Thanks again danpost, you forum ninja :D
You need to login to post a reply.