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

2019/12/10

Set actor to random location when hitting the border of the world

Mattzh Mattzh

2019/12/10

#
Hi, I was wondering how I can set my actors to random locations when they hit the borders of the world
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public void bounceAtEdges(int width,int height) // Turns when hits edge
    {
 
        int worldX = getWorld().getWidth();
        int worldY = getWorld().getHeight();
 
        if(getY()==height) //top border
        {
            setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
        }
        if(getX()==width) //left border
        {
            setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
        }
        if(getX()==worldX-width) //right border
        {
            setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
        }
        if(getY()==worldY-height)//bottom border
        {
            setLocation(getX() + Greenfoot.getRandomNumber(20), getY() + Greenfoot.getRandomNumber(20));
        }
    }
Thanks!
Mattzh Mattzh

2019/12/10

#
Also please ignore the description of the method, I forgot to change that!
danpost danpost

2019/12/10

#
What do width and height (the parameters) represent? What code are you using to turn and move this actor? What size is your world? And mostly, why are you using the current location of the actor as a starting point to set a new random location for the actor? Remove all getX and getY from the setLocation calls in the given code.
Vishnu19 Vishnu19

2019/12/10

#
Hi, First of all, I believe that width and height represent the actor width/height. (Not too sure though, sorry) This is the method that I'm using to turn the actor. So pretty much when I want it to turn randomly around the world, and when it happens touches the border, it will reset to a new random location
1
2
3
4
5
6
7
8
9
10
public void randomTurn2() // Turns randomly
  {
      move(1);
      //create condition of probability, occurs 1/20 times or 5%.
      if(Greenfoot.getRandomNumber(20)==1)
      {
          //generate degrees from -30 to 30 inclusive.
          turn(Greenfoot.getRandomNumber(61)-30);
      }
  }
The world size is: (800,600) I happened to be using current location in my code, as I really wasn't sure what I was doing at that point. I am relatively new to the world of code. Sorry for any confusion! So if I were to remove getX and getY, what would be appropriate to replace it with? I appreciate your time helping out!
danpost danpost

2019/12/10

#
Vishnu19 wrote...
width and height represent the actor width/height. (Not too sure though, sorry)
I find that highly unlikely as those values can easily be gotten using getImage().getWidth() and getImage().getHeight().
<< Code Omitted >> The world size is: (800,600)
I'm glad I asked about this. I was thinking your world might be a grid world (large sized cells) of size (20, 20) being that was the max random values acquired to set the change in the actors location. Use worldX and worldY instead for all 20.
if I were to remove getX and getY, what would be appropriate to replace it with?
No replacement is needed. Just use the random values. You should probably use <= and >= in the bounceAtEdge method's if conditions in case random placement is close to an edge.
Vishnu19 Vishnu19

2019/12/10

#
I just changed it to
1
2
setLocation(Greenfoot.getRandomNumber(worldX) , Greenfoot.getRandomNumber(worldY));
        }
It works, thank you so much!
You need to login to post a reply.