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

2019/9/19

Random spawn at edge of screen across screen

1
2
GreenHead456 GreenHead456

2019/9/19

#
Hello there, I am trying to figure out how to an object(Enemy) to spawn in randomly on the edge of the screen and move in a direction that does not make it immediately go off screen(i.e. object spawns on right side of screen and immediately goes to the right). I also want the object to keep spawning in until the user pauses the program. All I have is this:
public void randomSpawn()
{
    if(Greenfoot.getRandomNumber(300) ==1)
    {
        int spawnX = 100;
        int spawnY = 100;
        int rotate = 0;
        if(Greenfoot.getRandomNumber(100) ==1)
        {
            spawnX += Greenfoot.getRandomNumber(500);
            spawnY += Greenfoot.getRandomNumber(300); 
         } 
        rotate = Greenfoot.getRandomNumber(360);
       setRotation(rotate);

       getWorld().addobject(new Enemy(), spawnX, spawnY);
}
 
Keep in mind that my world size is (700,500) and the object is named Enemy and is constantly moving. If anyone knows how to do what I am trying to make it do, then please tell me.
danpost danpost

2019/9/19

#
I would think that the movement code of the Enemy objects would be relevant information to be given. Please provide Enemy class codes. I am curious as to why your randomSpawn method is within an Actor subclass, also. Seems to me, that would be a basic game action (not something an actor does).
GreenHead456 GreenHead456

2019/9/20

#
The move code is just a simple move(4); just in case i'll provide the entire act class:
public void act()
{
randomSpawn();
move(4);
die();
}

public void randomSpawn()
{
    if(Greenfoot.getRandomNumber(300) ==1)
    {
        int spawnX = 100;
        int spawnY = 100;
        int rotate = 0;
        if(Greenfoot.getRandomNumber(100) ==1)
        {
            spawnX += Greenfoot.getRandomNumber(500);
            spawnY += Greenfoot.getRandomNumber(300); 
         } 
        rotate = Greenfoot.getRandomNumber(360);
       setRotation(rotate);
 
       getWorld().addobject(new Enemy(), spawnX, spawnY);
}
As for why the the randomSpawn() method is in its own subclass, I am quite new to Greenfoot and so I am just looking up code on the internet and and plugging in what I find and trying to see what works while also keeping common coding themes among what I find. I had no idea you could do what you just said.
danpost danpost

2019/9/20

#
GreenHead456 wrote...
The move code is just a simple move(4); just in case i'll provide the entire act class: << Code Omitted >> As for why the the randomSpawn() method is in its own subclass, I am quite new to Greenfoot and so I am just looking up code on the internet and and plugging in what I find and trying to see what works while also keeping common coding themes among what I find. I had no idea you could do what you just said.
Using move(4), your enemies will move in the direction they are facing -- generally (maybe 40 different possible angles of movement due to pixel restraints). That is not bad, but can be improved on. As far as moving through the screen, I will give a suggestion later. By having the spawning code where you have it, your chances of spawning will have exponential growth. That is, the more enemies you have in the world, the more chance of spawning one. It will not take long before you will be inundated with enemies. Add an act method to your World subclass (MyWorld) and move the spawning code (method and method call from act) there. Now for, the aforementioned suggestion for movement. One idea would be to limit the addition of the enemies to a central location in your world (not necessarily right in the center) and immediately have it move backward to the edge of the world. That way, it will move normally toward and beyond that initial placement location. You will probably need to determine which edge it will start at as well as how far to move to initially place it at that edge. Make use of the addedToWorld method to perform initial set-up of enemies in the world.
danpost danpost

2019/9/20

#
Another way is to just choose a random point along the edge for spawn location and then randomly choose another point between 750 to 1650 pixels around the edge for a target point to turn towards (using the turnTowards method).
GreenHead456 GreenHead456

2019/9/20

#
I think I had an idea like that before where the enemy would spawn in a corner of the screen and based on which corner it spawned in, it would have its own set of angles it could choose from as not to go off screen. should I do something like that?
danpost danpost

2019/9/20

#
GreenHead456 wrote...
I think I had an idea like that before where the enemy would spawn in a corner of the screen and based on which corner it spawned in, it would have its own set of angles it could choose from as not to go off screen. should I do something like that?
It is your choice. If you do not mind them all starting from a corner, go with it.
GreenHead456 GreenHead456

2019/9/20

#
Just two more things: first is you said that this should a basic game action, does that mean it should be in the world class, or water as I have called it? Second question is should be addedToWorld() in the Water(world) class right?, under private void prepare()?
GreenHead456 GreenHead456

2019/9/20

#
Nevermind on the sceond question, I found addedtoWorld() on Greenfoot api in the actor section, so it goes in the actor then.
danpost danpost

2019/9/20

#
GreenHead456 wrote...
Just two more things: first is you said that this should a basic game action, does that mean it should be in the world class, or water as I have called it?
Yes.
GreenHead456 GreenHead456

2019/9/20

#
So now I have my addedToWorld() code written up:
protected void addedToWorld(water world)
    {
        int directionX = 0;
        int directionY = 0;
        int gambler = 0;
        
        do
        {
            gambler += Greenfoot.getRandomNumber(5);
            
            if(gambler == 1)
            {
                
                setLocation(70,50);
                
                directionX += Greenfoot.getRandomNumber(500) + 70;
                directionY += Greenfoot.getRandomNumber(300) + 50;
                turnTowards(directionX,directionY);
            }
            
            else if(gambler == 2)
            {
                
                setLocation(630,50);
                
                directionX += Greenfoot.getRandomNumber(500) + 70;
                directionY += Greenfoot.getRandomNumber(300) + 50;
                turnTowards(directionX,directionY);
            }
            
            else if(gambler == 3)
            {
                
                setLocation(630,450);
                
                directionX += Greenfoot.getRandomNumber(500) + 70;
                directionY += Greenfoot.getRandomNumber(300) + 50;
                turnTowards(directionX,directionY);
            }
            
            else if(gambler == 4)
            {
                
                setLocation(70,450);
                
                directionX += Greenfoot.getRandomNumber(500) + 70;
                directionY += Greenfoot.getRandomNumber(300) + 50;
                turnTowards(directionX,directionY);
            }
            
            else
            {
                setLocation(70,50);
                
                directionX += Greenfoot.getRandomNumber(500) + 70;
                directionY += Greenfoot.getRandomNumber(300) + 50;
                turnTowards(directionX,directionY);
            }
        }while(gambler > 0);    
         
    }
I also know that the world(Water) class should call the object into existence, so my question is how would I call it into the world class
danpost danpost

2019/9/20

#
GreenHead456 wrote...
So now I have my addedToWorld() code written up: << Code Omitted >> I also know that the world(Water) class should call the object into existence, so my question is how would I call it into the world class
Looks like an infinite loop waiting to happen. If line 9 assigns any number other than zero on the first time through the do loop, the condition to repeat will never be false. I am not sure how your code will prevent the actors from moving toward the corner they are placed in at times, although the chance is somewhat low. I was thinking you should have the world determine which corner to place each one. It could also immediately rotate them accordingly, thereby not needing to use the addedToWorld method.
// in Water class
public void act()
{
    randomSpawn();
}

private void randomSpawn()
{
    if (Greenfoot.getRandomNumber(300) == 1) // spawn chance
    {
        Actor enemy = new Enemy();
        addObject(enemy, 70, 50);
        enemy.setRotation(45); // face away from corner
        if (Greenfoot.getRandomNumber(2) == 1) // right side corner chance
        {
            enemy.setLocation(630, 50);
            enemy.turrn(90); // to 135
        }
        if (Greenfoot.getRandomNumber(2) == 1) // lower corner chance
        {
            enemy.setLocation(enemy.getX(), 450);
            enemy.setRotation(-enemy.getRotation());
        }
        enemy.turn(Greenfoot.getRandomNumber(71)-35); // expand angle range
    }
}
GreenHead456 GreenHead456

2019/9/21

#
So I plugged in that code you gave and and it gave me exactly what I want, thank you so much for your time and help. You helping with this has taught me that I can put stuff like this into the world class, so now I can refer back to this and play around it with on potential future projects. One more question, is the world class a parent class, a tester(child) class, or something else?
danpost danpost

2019/9/21

#
GreenHead456 wrote...
One more question, is the world class a parent class, a tester(child) class, or something else?
Not sure what you are referring to -- your Water class describes a specific type of world; and then there is the World class, which is a base class for all world classes.
GreenHead456 GreenHead456

2019/9/21

#
Oh okay I was thinking in terms of just plain Java then, my bad.
There are more replies on the next page.
1
2