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

2016/6/5

Counter Points is not updating

KrystalLo KrystalLo

2016/6/5

#
Hello, I've put in a counter code into my game from the previous code I've learnt from. But the counter is not increasing/updating the points after the pirate touches the coin. Here is the code for the coin and counter class.
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Coin here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Coin extends Actor
{
    private int iColor;
    private boolean boolRemove;
    int iPoint =1;
    Counter oCounter;
    public Coin(int iTn){
        iColor = iTn;
        setImage("coin"+iTn+".png");
    }
 
    public int getPoints(){
        return 5*(4-iColor);
    }
 
    /**
     * Act - do whatever the Coin wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Coin(){
        this(0);
    }
 
    public void act()
    {
        boolRemove = false;
        // Add your action code here.
        boolRemove = wasIJustCollected();
        setLocation(this.getX(),this.getY()+3);
        // at the bottom of the world delete the coin
        if(getY()>430){
 
            boolRemove = true;
        }
 
        if (boolRemove){
            // Remove the coin
            this.getWorld().removeObject(this);
        }
        // Remove me after all other lines of code have executed
    }
 
    private boolean wasIJustCollected(){      
        return (isTouching(Pirate.class));
    }
 
    private void wasICollected(){
        if(isTouching(Pirate.class)){
            oCounter.add(iPoint);
        }
    }
}
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);
    }
}
I think the problem is in the coin class, in this section of code.
1
2
3
4
5
6
7
8
9
private boolean wasIJustCollected(){      
     return (isTouching(Pirate.class));
 }
 
 private void wasICollected(){
     if(isTouching(Pirate.class)){
         oCounter.add(iPoint);
     }
 }
Because if I remove the private void wasICollected code, the game still works even if it was there or not. And so most likely the boolean is the problem that is causing the oCounter.add(iPoint); from not working. I'm not too sure, this is what I think but its most likely not right.
danpost danpost

2016/6/6

#
You will probably need to show your World subclass code as well as the Pirate class code to determine why the counter is not updating.
KrystalLo KrystalLo

2016/6/6

#
World Class
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
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class MyWorld here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class MyWorld extends World
{
    //background was taken from this website
    //background was edited to make the height higher
    Pirate Nog;
    Counter myCounter;
    GreenfootSound backgroundMusic = new GreenfootSound("8bitmusic1.wav");
    /**
     * Constructor for objects of class MyWorld.
     *
     */
    public MyWorld()
    {   
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(659, 483, 1);
        prepare();
        backgroundMusic.playLoop();
       // randomCoins();
    }
 
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Counter myCounter = new Counter();
        Pirate Nog = new Pirate(myCounter);
        addObject(Nog,306,411);
        Nog.setLocation(286,411);
        Nog.setLocation(268,414);
        addObject(myCounter,599,32);
        myCounter.setLocation(602,25);
    }
    int iCounter = 0;
    
    public void act(){
    iCounter++;
    if (iCounter==40){
         iCounter =0;
         //shorthand code http://www.greenfoot.org/topics/56657/0#bottom
            int rand = Greenfoot.getRandomNumber(4);
            int x = Greenfoot.getRandomNumber(getWidth());
            addObject(rand == 0? new Bomb() : new Coin(rand), x, 0);
             
           
           //Coin myCoin = new Coin();
           //addObject(myCoin,100,0);
           // addObject(new Coin(),Greenfoot.getRandomNumber(getWidth()),0);
            //http://www.greenfoot.org/topics/4602/0
          
        }
        }
    public void stopped(){
    backgroundMusic.pause();
    }
    public void started(){
    backgroundMusic.playLoop();
    }
    }
Pirate Class
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
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 iCounter =0;
    int iHurt =1;
    Counter oCounter;
    private String direction = "right";
    public Pirate(Counter myCounter){
        oCounter = myCounter;
        setImage("pirate"+direction+iHurt+".png");
    }
 
    /**
     * 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.
        //sounds & music gotten from http://freesound.org/
        checkKeys();
         
        setImage("pirate"+direction+iHurt+".png");
        int iCounter = oCounter.getValue();
        //if (iCounter%10 == 0){
        // iHurt = 1;
        // setImage("pirate"+direction+iHurt+".png");
        //}
        if (isTouching(Bomb.class)){
            removeTouching(Bomb.class);
            iHurt++;
            Greenfoot.playSound("explode.wav");
            setImage("pirate"+direction+iHurt+".png");
            if(iHurt>4){
                Greenfoot.stop();
            }
        }
        if(isTouching(Coin.class)){
            //gold coins will give 15 points, silver 10 points and bronze 5 points
            iCounter++;
            Greenfoot.playSound("coins.wav");
        }
    }  
 
    private void checkKeys(){
        if(Greenfoot.isKeyDown("right")){
            direction = "right";
            this.setLocation(getX()+moveBy,getY());
 
        }
        if (Greenfoot.isKeyDown("left")){
            direction = "left";
            this.setLocation(getX()-moveBy,getY());
 
        }
    }
}
danpost danpost

2016/6/6

#
Well, there is no place in the code that adds to the counter (the 'wasICollected' method in the Coin class is never called). You seem to have too many reference fields and stuff for the counter. You only have one Counter object, yet every coin, the pirate and the world all seem to have a field for a Counter object. If you have a field at all for it, it should be in your MyWorld class only -- and it can be accompanied by a 'getter' method. However, neither the field nor the method are even needed since there is only going to be one Counter object in your world. You should remove all code related to the Counter object and start afresh with just adding one in the world in your MyWorld 'prepare' method. Also, remove both the 'wasICollected' and 'wasIJustCollected' methods from the Coin class. The pirate is already checking for contact with any coins (which can be worked on to add the proper values to the counter). You would have something like the following to update the counter from the Pirate class when contacting a coin:
1
2
3
4
5
6
7
if (isTouching(Coin.class))
{
    Coin coin = (Coin)getOneIntersectingObject(Coin.class);
    ((Counter)getWorld().getObjects(Counter.class).get(0)).add(coin.getPoints());
    getWorld().removeObject(coin);
    Greenfoot.playSound("coins.wav');
}
KrystalLo KrystalLo

2016/6/6

#
Ah sorry for the late reply, its working. Thank you alot Dan! :D
You need to login to post a reply.