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

2013/3/19

Seaweed

Frewt_Ninja2 Frewt_Ninja2

2013/3/19

#
We were told to create a Crab scenario for IT where the crab only eats seaweed. The part that I'm having trouble with is getting the seaweed to look like it is waving in with the tide. I'm able to get it to switch between 2 images but it switches too fast to be seen, can anyone help with coding please? This is what I have so far: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Seaweed extends Animal { private GreenfootImage image1; private GreenfootImage image2; public Seaweed() { image1 = new GreenfootImage("seaweed.png"); image2 = new GreenfootImage("seaweed2.png"); setImage(image1); } public void act() { wave(); } public void wave() { if ( getImage() == image1 ) { setImage(image2); } else { setImage(image1); } } } I also need to give it a 15% chance of it moving every turn so if you could help with that too it would be great, thanks
danpost danpost

2013/3/20

#
You need a timer to count a few cycles between switching of the images. To give something a 15 in 100 change of happening:
if (Greenfoot.getRandomNumber(100)<15)
{
    // do whatever
}
Frewt_Ninja2 Frewt_Ninja2

2013/3/20

#
Thanks, how do I make it move in a random direction?
danpost danpost

2013/3/20

#
Which directions are allowable? any angular direction? the four cardinal directions? the four cardinal directions plus the diagonal directions? only left/right? only up/down? Does the last direction it moved have any influence on what direction it can move next? if so, how?
Frewt_Ninja2 Frewt_Ninja2

2013/3/22

#
Any direction, random each time and it isn't allowed to go off the edge of the map
danpost danpost

2013/3/22

#
Then, where I said '// do whatever', put
int rot = Greenfoot.getRandomNumber(360);
setRotation(rot);
move(4);
You can change the speed of movement (4) to whatever (move(1) gives 4 possible directions; move(2) gives 16 possibles; move(3) gives 32; move(4) gives 40). Check out my Radial Graphs Demo using Radial movement and see the possible directions for each distance.
You need to login to post a reply.