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
Super_Hippo Super_Hippo

2015/12/13

#
What do you mean with 'it doesn't work'? What I think would happen is that you create so many actors that the scenario visually stops because it is very slow due to all the actors.
Blubberfish007 Blubberfish007

2015/12/13

#
Super_Hippo wrote...
What do you mean with 'it doesn't work'? What I think would happen is that you create so many actors that the scenario visually stops because it is very slow due to all the actors.
i cant see any actors. i also tried it with 1/100 chance and waited for ages and nothing happened
Blubberfish007 Blubberfish007

2015/12/13

#
fejfo wrote...
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());
it did work but the actors need to spawn from fixed places (N,S,E,W) and move towards the middle and need to spawn when a certain number is randomly generated. (and plus it lagged)
Blubberfish007 Blubberfish007

2015/12/13

#
danpost wrote...
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.
example class?
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
I mean, post the entire class code (starting at line 1) of one of these actors that you are placing the corresponding code in.
fejfo fejfo

2015/12/13

#
Blubberfish007 wrote...
fejfo wrote...
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());
it did work but the actors need to spawn from fixed places (N,S,E,W) and move towards the middle and need to spawn when a certain number is randomly generated. (and plus it lagged)
the lag is probably because your gpu and cpu are bad what do you mean with it doesn't work ? (what is the error) and did you know a world has an act method to ? and you want to spawn it form fixed places so : place this in the world and please if you don't understand the code ask how it works
1
2
3
4
5
6
7
8
int[] x = {x1,x2,x3,...}
int[] y = {y1,y2,y3,...}
double chance = 0.01 // for a 1/100
public void act() {
    if(Math.random() < chance) {
         addObject(new NameOfTheClass(),x[Greenfoot.getRandomNumber(x.length)],y[Greenfoot.getRandomNumber(y.length)]);
    }
}
Blubberfish007 Blubberfish007

2015/12/13

#
fejfo wrote...
Blubberfish007 wrote...
fejfo wrote...
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());
it did work but the actors need to spawn from fixed places (N,S,E,W) and move towards the middle and need to spawn when a certain number is randomly generated. (and plus it lagged)
the lag is probably because your gpu and cpu are bad what do you mean with it doesn't work ? (what is the error) and did you know a world has an act method to ? and you want to spawn it form fixed places so : place this in the world and please if you don't understand the code ask how it works
1
2
3
4
5
6
7
8
int[] x = {x1,x2,x3,...}
int[] y = {y1,y2,y3,...}
double chance = 0.01 // for a 1/100
public void act() {
    if(Math.random() < chance) {
         addObject(new NameOfTheClass(),x[Greenfoot.getRandomNumber(x.length)],y[Greenfoot.getRandomNumber(y.length)]);
    }
}
its for a school assesment and if i give in that it will be suspicious
Blubberfish007 Blubberfish007

2015/12/13

#
basically the game is you are a player in the middle of the screen whos job is to stop enemies (who come from the north east south or west) from getting to a green orb in the middle. each enemy spawns when a random number equals 1. if it equals 1 in the enemy one class, it spawns an enemy1 in the north of the world, and then moves downwards towards the orb. the player's job is to turn in right angles and press space when it is pointing in the direction of an enemy, and it will move in that direction and remove the enemy if it touches it, and then get moved to the middle. the game ends when a n enemy gets to the centre and touches the orb
Blubberfish007 Blubberfish007

2015/12/13

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import greenfoot.*;
public class enemy1 extends objects
{
        public void act()
    {
        Whentospawn();
        setLocation(getX(), getY() - 1);
    }
    public void Whentospawn()
    {
        if (Greenfoot.getRandomNumber(2) == 1)
        {
            
        }
    }
}
fejfo fejfo

2015/12/13

#
you should never spawn an enemy for the object it self this will mean that it creates duplicates of each other. You can do 2 things : 1 have spawner objects in the world creating use the addObject to make the (invisebel) spawner objects you create special classes for the spawners :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import greenfoot.*;
public class Spawner extends objects
{
        public void act()
    {
        spawn();
    }
    public void spawn()
    {
        if (Greenfoot.getRandomNumber(1) == 0) //every act
        {
            getWorld().addObject(new enemy1(), getX(),getY());
        }
    }
}
and when the enemy moves you should use :
1
2
turnTowards(getWorld().getWidth()/2,getWorld().getHeight()/2); // turnTowards the centre
move(<speed>)
2 spawn form the world like I did : you could use simpeler code
danpost danpost

2015/12/13

#
You cannot call 'Whentospawn' from its own class (well, you can; but, there are issues with that: (a) if not enemy1 objects are in the world, the act method will not execute and none will be spawned; and (2) the more enemy1 objects in the world, the more, exponentially, objects will be spawned). Something you can do is to make the 'Whentospawn' method a class method by declaring the method as 'static'. Then you can call each class to run its spawn method from the World subclass act method:
1
2
3
enemy1.Whentospawn();
enemy2.Whentospawn();
// etc.
Blubberfish007 Blubberfish007

2015/12/14

#
danpost wrote...
You cannot call 'Whentospawn' from its own class (well, you can; but, there are issues with that: (a) if not enemy1 objects are in the world, the act method will not execute and none will be spawned; and (2) the more enemy1 objects in the world, the more, exponentially, objects will be spawned). Something you can do is to make the 'Whentospawn' method a class method by declaring the method as 'static'. Then you can call each class to run its spawn method from the World subclass act method:
1
2
3
enemy1.Whentospawn();
enemy2.Whentospawn();
// etc.
so where do i put this
Blubberfish007 Blubberfish007

2015/12/14

#
thanks guys the spawner class worked :))
Blubberfish007 Blubberfish007

2015/12/14

#
also, quick question, how would i make a player controlled actor turn without spinning?
Blubberfish007 Blubberfish007

2015/12/14

#
wait, no, i did it
There are more replies on the next page.
1
2
3