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

2016/5/30

Changing the image of the Actor when losing health

1
2
3
KrystalLo KrystalLo

2016/5/30

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

2016/5/30

#
KrystalLo wrote...
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. < Code Omitted > 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. < Code Omitted >
If it worked in one, it should work in the other. Please post your current code to the Counter class.
KrystalLo KrystalLo

2016/5/30

#
1
int iCounter = oCounter.getCount();
for the getCount, it says that it cannot find symbol - method getCount().. Maybe I'm missing a line of code?
danpost danpost

2016/5/30

#
danpost wrote...
Please post your current code to the Counter class.
KrystalLo wrote...
1
int iCounter = oCounter.getCount();
for the getCount, it says that it cannot find symbol - method getCount().. Maybe I'm missing a line of code?
I saw that at line 28 in your Pirate class above. I asked for the Counter class code.
KrystalLo KrystalLo

2016/5/30

#
Sorry, i'm not understanding what you mean. Do you want me to put the code that I put in for pirate class
1
2
3
4
5
6
7
8
9
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();
Into counter class? All the code I used.
danpost danpost

2016/5/30

#
KrystalLo wrote...
Do you want me to put the code that I put in for pirate class < Code Omitted > Into counter class? All the code I used.
No. Above you provided the code to the Pirate class followed by the code to the Turtle class. Now, I am asking for the code to the Counter class.
KrystalLo KrystalLo

2016/5/30

#
oh... Sorry. I just realised I haven't put anything in the code for my game for Counter class....... this is the code I have for when we learnt how to code though...
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
 
/**
 * A simple counter with graphical representation as an actor on screen.
 *
 * @author mik
 * @version 1.0
 */
public class Counter extends Actor
{
    private static final Color transparent = new Color(0,0,0,0);
    private GreenfootImage background;
    private int value;
    private int target;
    private int iCount;
    /**
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        updateImage();
    }
     
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act()
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }
 
    /**
     * Add a new score to the current counter value.
     */
    public void add(int score)
    {
        target += score;
        iCount++;
    }
 
    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return value;
    }
    /**
     * Set a new counter value.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
        public int getCount(){
        return iCount;
    }
    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 22, Color.BLACK, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2,
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
KrystalLo KrystalLo

2016/5/30

#
I'm terribly sorry, i'm so embarrassed now.
danpost danpost

2016/5/30

#
It would probably work if you copied that code into the Counter class of your project as a total replacement. However, the class was complete before you added any code to it (when learning). You probably have the original version of the Counter class in your project and you should be able to just replace 'getCount' with 'getValue'.
danpost danpost

2016/5/30

#
KrystalLo wrote...
I'm terribly sorry, i'm so embarrassed now.
Do not be embarrassed. Take it as a learning experience. When you get a message that says it cannot find something, determine where it would be looking for it and see if it is there. In this case, it could not find a method that was being called on 'oCounter'. Now, 'oCounter' is a field of type 'Counter', which is where you would check to make sure that the method was located there or in one of its parent classes.
KrystalLo KrystalLo

2016/5/30

#
Okay thank you, I'll finish off the counter class code when I finish getting back home.
KrystalLo KrystalLo

2016/5/30

#
Hmm, I've putted the code from the previous one to my game. But now theirs an error in myWorld, and it is saying that
1
Pirate pirate = new Pirate();
"constructor pirate in class pirate cannot be applied to given types;"
danpost danpost

2016/5/30

#
KrystalLo wrote...
Hmm, I've putted the code from the previous one to my game. But now theirs an error in myWorld, and it is saying that
1
Pirate pirate = new Pirate();
"constructor pirate in class pirate cannot be applied to given types;"
Line 15 of your Pirate class shows that a Counter object is required to be passed when creating a new Pirate object. You need to create the Counter object before creating the Pirate object and at some point also add it (the counter) into the world.
KrystalLo KrystalLo

2016/5/31

#
Sorry I'm back again, it seems now after putting in myCounter into the world theirs an error with,
1
setImage("pirate"+iHurt+".png");
at Pirate.act(Pirate.java:27) at greenfoot.core.Simulation.actActor(Simulation.java:594) at greenfoot.core.Simulation.runOneLoop(Simulation.java:552) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) Caused by: java.io.FileNotFoundException: Could not find file: pirate1.png at greenfoot.util.GreenfootUtil.getURL(GreenfootUtil.java:544) at greenfoot.GreenfootImage.loadFile(GreenfootImage.java:278) ... 7 more From what I should know, it cannot fine the image name is that correct? Well, my actor can move left and right and when he moves left and right the character turns right and left as well. And I named it pirateright1.png pirateleft1.png and so on (number ascends). And so, how can I put both of the image files name into the code?
Super_Hippo Super_Hippo

2016/5/31

#
Since 'iHurt' is an int, it tries to set "pirate1.png" (and higher numbers) as the actor's image. This can't work if the names of the images are different (additional right or left).
There are more replies on the next page.
1
2
3