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.
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());
}
}
}
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);
}
}
}


