Hello, I needed help in changing the image of my Actor when he loses one health. I've tried a code that my teacher previously did and it says that it cannot find a symbol for getCount. I may have done this incorrectly.
And the next piece of code is the code I had put in when we were practising on greenfoot for the first time, and it works.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Pirate here. * * @author (your name) * @version (a version number or a date) */ public class Pirate extends Actor { int moveBy = 4 ; int iHurt = 1 ; Counter oCounter; boolean boolExplode = false ; public Pirate(Counter myCounter){ oCounter = myCounter; } /** * Act - do whatever the Pirate wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. checkKeys(); setImage( "pirate" +iHurt+ ".png" ); int iCounter = oCounter.getCount(); if ((iCounter % 10 )== 0 ) {iHurt = 1 ;} if (isTouching(Bomb. class ) && (boolExplode== false )){ boolExplode= true ; iHurt++; //Greenfoot.playSound(" if (iHurt> 4 ){ Greenfoot.stop(); } } } private void checkKeys(){ if (Greenfoot.isKeyDown( "Right" )){ this .setImage( "pirateright1.png" ); this .setLocation(getX()+moveBy,getY()); } if (Greenfoot.isKeyDown( "Left" )){ this .setImage( "pirateleft1.png" ); this .setLocation(getX()-moveBy,getY()); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.util.List; /** * Write a description of class Turtle here. * * @author (your name) * @version (a version number or a date) */ public class Turtle extends Actor { // Create a field of type integer named iCounter // with initial value of zero int iCounter = 0 ; int iHurt = 1 ; boolean bBitten = false ; Counter oCounter; public Turtle(Counter myCounter){ oCounter = myCounter; } /** * Act - do whatever the Turtle wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move( 5 ); // check the key state of the arrow left and arrow right checkKeys(); // Create a method that deals with touching an apple checkIfCanEatApple(); if ( this .getWorld().numberOfObjects()< 5 ){ MyWorld w = (MyWorld) getWorld(); w.randomApples(); } setImage( "turtle" +iHurt+ ".png" ); int iCounter = oCounter.getCount(); if ((iCounter % 10 )== 0 ) {iHurt = 1 ;} if (isTouching(Snake. class ) && (bBitten== false )){ // do something bBitten= true ; iHurt++; Greenfoot.playSound( "au.wav" ); if (iHurt> 5 ){ Greenfoot.stop(); } } if (!(isTouching(Snake. class ))){ bBitten = false ; } // Is the turtle near an object List<Brick> bricks = getObjectsInRange( 50 , Brick. class ); //now 10 is the radius and Brick.class is the class // you are searching for; // next test is there any objects in this list if (bricks.size()> 0 ){ // This means that the turtle is close to the wall move(- 4 ); } } private void checkIfCanEatApple(){ // do something // test for is an apple if (isTouching(Apple. class )){ // do something // Add one more apple to the iCounter iCounter++; // removeTouching(Apple.class); Greenfoot.playSound( "slurp.wav" ); // When Turtle touches Redapple earn 3, Greenapple earn 5 } } private void checkKeys(){ // do something // Turn the actor left or right based //on the key down state of the arrow keys if (Greenfoot.isKeyDown( "left" )){ // Do something turn(- 9 ); } if (Greenfoot.isKeyDown( "right" )){ turn( 9 ); } } } |