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

2015/12/12

how to spawn actor?

1
2
3
Blubberfish007 Blubberfish007

2015/12/12

#
im trying to make a game and a method that spawns an actor is a random number is equal to one. only problem is i dont know how to spawn the actor plz halp current code-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
        public void act()
    {
        Whentospawn();
        setLocation(getX(), getY() - 4);
    }
    public void Whentospawn()
    {
        if (Greenfoot.getRandomNumber(100) == 1)
        {
            
        }
    }
}
Super_Hippo Super_Hippo

2015/12/12

#
1
getWorld().addObject(new NameOfTheClass(), x, y); // x and y are the coordinates where it should be spawned
danpost danpost

2015/12/12

#
My concern is what class the code given is in and what type of actor is being spawned. Often new coders misplace where code should go and I wonder if that is the case here. Maybe supply a little background as to what these actors are for and what they are supposed to be doing.
Blubberfish007 Blubberfish007

2015/12/12

#
it is in the actor class and i was going to write the method then call it in the same class.
Blubberfish007 Blubberfish007

2015/12/12

#
Super_Hippo wrote...
1
getWorld().addObject(new NameOfTheClass(), x, y); // x and y are the coordinates where it should be spawned
it didn't work (and its not because the spawning number was not randomly generated because i changed it to 0-1 and put if it equals one and it still didnt work)
Super_Hippo Super_Hippo

2015/12/12

#
Do you have 'Greenfoot.getRandomNumber(1)' or 'Greenfoot.getRandomNumber(2)'?
danpost danpost

2015/12/12

#
Blubberfish007 wrote...
it is in the actor class and i was going to write the method then call it in the same class.
I could already tell it was in an actor class. I still would like to verify that you are doing things properly.
Blubberfish007 Blubberfish007

2015/12/12

#
Super_Hippo wrote...
Do you have 'Greenfoot.getRandomNumber(1)' or 'Greenfoot.getRandomNumber(2)'?
2 i know that it doesnt generate the limit number
Blubberfish007 Blubberfish007

2015/12/12

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
{
        public void act()
    {
        Whentospawn();
        setLocation(getX(), getY() - 1);
    }
    public void Whentospawn()
    {
        if (Greenfoot.getRandomNumber(2) == 1)
        {
           getWorld().addObject(new enemy1(), 401, 601);
        }
    }
}
Blubberfish007 Blubberfish007

2015/12/12

#
thats what it is
danpost danpost

2015/12/12

#
Blubberfish007 wrote...
thats what it is
Okay -- you are spawning enemies; but, from what class? Why are you spawning them from an Actor subclass?
Blubberfish007 Blubberfish007

2015/12/13

#
because enemies come from different directions and need to spawn in a different place from the other enemies. enemy1 spawns in the north, enemy2 in the east side, and so on.
Blubberfish007 Blubberfish007

2015/12/13

#
i thought to have the corresponding code in its respective actor would be easier; just copy and paste it and change the names
danpost danpost

2015/12/13

#
Blubberfish007 wrote...
i thought to have the corresponding code in its respective actor would be easier; just copy and paste it and change the names
Please show an example class.
fejfo fejfo

2015/12/13

#
I think you are spawning Actors for an actor which is bad. you should use something like this:
1
addObject(new NameOfTheClass(),Greenfoot.getRandomNumber(getWidth()),Greenfoot.getRandomNumber(getHeight()));
in the world if you only want enemies spawing for the corners you should use :
1
addObject(new NameOfTheClass(),Greenfoot.getRandomNumber(2)*getWidth(),Greenfoot.getRandomNumber(2)*getHeight());
There are more replies on the next page.
1
2
3