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

2013/2/23

I need some guidance on this Crab scenario.

1
2
katebrown672 katebrown672

2013/2/23

#
Hello, I am stuck with my Crab scenario in Chapter 4. I went through exercise 4.18, but my professor asked us to include a penalty. My instructions are: Create a penalty for the crab. If the crab touches the edge of the world, another worm is added to the world. Again, this is Chapter 4 scenario with the crabs and worms. If anyone knows how to add an object when another hits the edge of the world that would be fantastic. Thanks!
danpost danpost

2013/2/23

#
You probably already have a check in the Crab class determining if the crab is touching the edge of the world. You just need to add the command to that block. If you do not know how to go about this, go through the documentation of the Greenfoot classes to determine what method(s) you might need. Clues to where to look are given in what you are looking to do (refer to the wording of your instructions).
katebrown672 katebrown672

2013/2/24

#
Okay, meaning addedToWorld or addObject? I've tried multiple codes with either one and I just can't seem to get it. I know I'm missing something somewhere, but can't seem to figure it out.
danpost danpost

2013/2/24

#
The 'addedToWorld' is a method that the system calls automatically when an object is added to the world. The 'addObject' method is the one that you call to add an object into a world. The thing is that you need a world to add the object into. The Actor class method 'getWorld' will return the world you want to add the worm into.
1
getWorld().addObject(new Worm(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight()));
This is basic 'object.method()' syntax done at multiple levels. 'getWorld().getWidth()' and 'getWorld().getHeight()' use a World object and two methods belonging to the World class; 'getRandomNumber' uses a static 'Greenfoot' object and a static method from that class; and 'addObject' method uses a World object and a method from that class. The 'new Worm()' creates the actor to put in the world.
danpost danpost

2013/2/24

#
To elaborate: 'getWorld' IS a method; however, it DOES return a World object that a World class method can be called with. The 'getWorld' method is also called on another object (which is not given explicitly). When an object is being 'act'ed on by the system (each active object has its 'act' method executed on it, once per 'act cycle', aka 'frame'), that object is by default the object of any method calls not explicitly naming an object. The object being 'act'ed on can also be referred in code with the keyword 'this'. So actually 'this.getWorld().getWidth()' would get the 'width' of the 'world' that 'this' object is in.
katebrown672 katebrown672

2013/2/24

#
Sorry, I'm just confused. I understand these codes and what they mean, but how do I know where to put them?
danpost danpost

2013/2/24

#
Your instructions said 'If the crab touches the edge of the world, another worm is added to the world.' I said 'You probably already have a check in the Crab class determining if the crab is touching the edge of the world. You just need to add the command to that block.'
katebrown672 katebrown672

2013/2/24

#
Okay, I got that. I put that with turnAtEdge() but it's not doing anything. It compiles, but nothing changes.
danpost danpost

2013/2/24

#
Please post your Crab class code.
katebrown672 katebrown672

2013/2/24

#
public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private int counter = 0; /** * Create a crab and initialize its two images. */ public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); wormsEaten = 0; } /** * Act - do whatever the crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { getWorld(); checkKeypress(); move(); lookForWorm(); switchImage(); counter++; if (counter == 3) { counter = 0; switchImage(); } } /** * Alternate the crab's image between image1 and image2. */ public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /** * Check whether a control key on the keyboard has been pressed. * If it has, react accordingly. */ public void checkKeypress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } if (Greenfoot.isKeyDown("up")) { turn(-4); } if (Greenfoot.isKeyDown("down")) { turn(4); } } /** * Check whether we have stumbled upon a worm. * If we have, eat it. If not, do nothing. If we have * eaten eight worms, we win. */ public void lookForWorm() { if ( canSee(Worm.class) ) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); wormsEaten = wormsEaten + 1; if (wormsEaten == 8) { Greenfoot.playSound("fanfare.wav"); Greenfoot.delay(5); Greenfoot.stop(); } } } public void turnAtEdge() { if ( atWorldEdge() ) { getWorld(); getWorld().addObject(new Worm(), Greenfoot.getRandomNumber(getWorld().getWidth()), Greenfoot.getRandomNumber(getWorld().getHeight())); } } }
katebrown672 katebrown672

2013/2/24

#
Oh, I got it! I didn't put turnAtEdge( ) in my act method. Thank you so much for all of your help!!
danpost danpost

2013/2/24

#
You can remove the first statement in the 'act' method and in the 'if (atWorldedge() )' block (the 'getWorld();' statements which do not do a thing). Also there is no 'turn' statement in the 'turnAtEdge' method.
katebrown672 katebrown672

2013/2/24

#
Okay, will do. Thanks again!
danpost danpost

2013/2/24

#
One other thing: you are calling the 'switchImage' method directly from the 'act' method and then again within the counter control in the 'act' method. Either remove the direct call to 'switchImage' or move the counter control to the 'switchImage' method putting the code that is currently in the method at the location where the counter control calls it.
katebrown672 katebrown672

2013/2/24

#
Okay, I'll remove that. I have run into one other problem. The other part of my assignment states that I am to export my scenario into an application. I know how to do this, and did, but when I go to open my jar file with the game, all that appears is the run and reset button. I don't know what is wrong. The game doesn't even show up.
There are more replies on the next page.
1
2