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

2013/12/17

Random moving code?

tomw tomw

2013/12/17

#
Hi i was wondering if anyone can help me with a random moving code however i would like it so that if my actor crashes into edge of the world then it will get a random number (Random number is for turn) if anyone can help me that would be very helpfull.
askgriff askgriff

2013/12/17

#
Hi Tom -- I just posted this code today. Does it help? My Scenario
tomw tomw

2013/12/17

#
Well i was wanting it so my actors just turn rather than going of the screen and coming back on a diffrent side can you help me with that?
danpost danpost

2013/12/17

#
Please show the code for the class of your actor.
tomw tomw

2013/12/17

#
I havent added any code expect form move(3); im just starting with greenfoot and help would be very helpfull import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class fish2 here. * * @author (your name) * @version (a version number or a date) */ public class fish2 extends Actor { /** * Act - do whatever the fish2 wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(3); } }
danpost danpost

2013/12/17

#
You will need a method to check encountering world edges (like the 'atWorldEdge' method of the Animal class). Call it from the act method and, when true, turn a random amount.
tomw tomw

2013/12/17

#
Im not that sure what to type in for that random code and i dont know were to start with the edge of world part.
danpost danpost

2013/12/17

#
That would depend on the range you wish to 'turn' the object when an edge is encountered. Import the 'Animal' class into your scenario and look at the 'atWorldEdge' method (you can remove the class once you are done with it). To import it: from the menubar 'Edit'>'Import class...', select the 'Animal' class and click the 'Import' button.
tomw tomw

2013/12/17

#
The range is any were from 90right and 90 left can you give me that part of the code?
danpost danpost

2013/12/17

#
int turnAmount = Greenfoot.getRandomNumber(180)-90;
turn(turnAmount);
// or just
turn(Greenfoot.getRandomNumber(180)-90);
tomw tomw

2013/12/17

#
Thnaks now what about the edge of world part can you please give me that?
danpost danpost

2013/12/17

#
I stated where you can find that code.
danpost wrote...
You will need a method to check encountering world edges (like the 'atWorldEdge' method of the Animal class). Call it from the act method and, when true, turn a random amount. ***************************** To import it: from the menubar 'Edit'>'Import class...', select the 'Animal' class and click the 'Import' button.
tomw tomw

2013/12/17

#
Im still not sure can you please explain it in more detil? and if possible give me that small section of the code?
danpost danpost

2013/12/17

#
I cannot be any more detailed as to how to acquire that code from the Animal class, as I was fairly specific as to how to get it (and it is not that difficult -- actually fairly simple).
You need to login to post a reply.