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

2016/11/30

Crab scenario switchImage (how?)

Noob4928 Noob4928

2016/11/30

#
I recently started with the Greenfoot book. I followed every step and followed every task but somehow I just can't get the crab image switching. I looked it up in crab scenario-5 which is a working example of how it should be. As far as I can see The code match so I don't understand why mine doesn't switch. Here my code: public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; public Crab() { image1 = new GreenfootImage("crab.png"); image2 = new GreenfootImage("crab2.png"); setImage(image1); } public void act() { checkKeyPress(); move(); canSeeWorm(); switchImage(); } public void canSeeWorm() { if (canSee(Worm.class)) { eat(Worm.class); Greenfoot.playSound("slurp.wav"); } } public void checkKeyPress() { if (Greenfoot.isKeyDown("left")) { turn(-4); } if (Greenfoot.isKeyDown("right")) { turn(4); } } public void turnAtEdge() { if (atWorldEdge()) { turn(4); } } public void switchImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } } ================================================================================================================== Here the code of the example: public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; /** * 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() { checkKeypress(); move(); lookForWorm(); 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); } }
danpost danpost

2016/11/30

#
The code looks to be okay. The only thing I can think is to check to ensure that the two images, "crab.png" and "crab2.png" are not copies of each other (right click on a crab and select 'SetImage...' and visually compare the two -- then click the 'Cancel' button).
Nosson1459 Nosson1459

2016/12/1

#
Does it compile (without syntax errors)? Because I don't see how it can if there is nothing in the parenthesis of the move method, in my crab scenario it has move(5) int the act not just move() (but my question is in the crab code that you said isn't the one you made, also just has move() so unless you didn't copy-paste it (which means you typed it, so it can have the same mistake), it shouldn't work either). I don't understand your canSeeWorm method, I never heard of the method canSee(java.lang.Class cls) or eat(java.lang.Class cls) unless you made them yourself in crab but didn't post them. The above problems would be syntax errors though so I don't see how you can run your scenario with these errors, if you can then it either didn't compile at all (so you should close your scenario and reopen it, then try again), or these aren't problems and the above methods are methods that I don't know about.
danpost danpost

2016/12/1

#
Nosson1459 wrote...
the above methods are methods that I don't know about.
Yes -- evidently. These methods are included in the Animal class which the classes given extend.
You need to login to post a reply.