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

2013/9/2

Counter help

mattjames mattjames

2013/9/2

#
Hello When I try and compile the code below it tells me that the counter is not an int so I cannot do it. Any ideas how to fix this?
if (passengersleft==0&&counter>0){
            win=true;
        }
SPower SPower

2013/9/2

#
Can you show the code where you create the counter variable?
davmac davmac

2013/9/2

#
If the counter is an instance of the Counter class included with Greenfoot, you need to use the getValue() method to get the value of the counter.
    if (passengersleft==0 && counter.getValue() > 0) {  
        win=true;  
    }  
mattjames mattjames

2013/9/2

#
It now comes up with a can't find method error.
davmac davmac

2013/9/2

#
Well, what is the type of the 'counter' variable? What methods do exist in its type?
mattjames mattjames

2013/9/2

#
here is the code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;


public class Counter  extends Actor
{
    private int totalCount = 20;

    public Counter()
    {
        setImage(new GreenfootImage("20", 30, Color.WHITE, Color.BLACK));
    }

    
    public void bumpCount(int amount)
    {
        totalCount += amount;
        setImage(new GreenfootImage("" + totalCount, 20, Color.WHITE, Color.BLACK));
    }
}
SPower SPower

2013/9/2

#
Add this method to the Counter class:
public int getCount()
{
    return totalCount;
}
mattjames mattjames

2013/9/2

#
Still comes up with the came error
davmac davmac

2013/9/2

#
SPower is basically correct, but if you're using 'getValue()' as per my code before, you need to name the method getValue too, not getCount -
    public int getValue()  
    {  
        return totalCount;  
    }  
Or, call it getCount as SPower suggests and change the name at the place where you call it:
if (passengersleft==0 && counter.getCount() > 0) {    
    win=true;    
}  
SPower SPower

2013/9/2

#
@davmac Sorry, that's was not inteded: I just replied without properly reading your post. Sorry.
mattjames mattjames

2013/9/2

#
Guys works well now thanks both of you!
You need to login to post a reply.