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

2017/6/27

Counter getValue

Venbha Venbha

2017/6/27

#
I am making a game like that of in snake.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo etc)

public class Snake extends Actor
{
    public void act() 
    {
        MouseInfo pointer = Greenfoot.getMouseInfo();
        if(pointer != null)
        {
            int mouseX = pointer.getX();
            int mouseY = pointer.getY();
            turnTowards(mouseX, mouseY);
        }
        move(5);
        if(isTouching(Food.class))
        {
            removeTouching(Food.class);
            PryHabitat.counter.add(1);

        }
    }    
}
and notice
PryHabitat.counter.add(1);
line. it adds a score, but when it gets value`33` it should stop and show text "Congrats" but the point is:What should we write for "getValue()"???
Venbha Venbha

2017/6/27

#
Venbha wrote...
I am making a game like that of in snake.class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo etc)

public class Snake extends Actor
{
    public void act() 
    {
        MouseInfo pointer = Greenfoot.getMouseInfo();
        if(pointer != null)
        {
            int mouseX = pointer.getX();
            int mouseY = pointer.getY();
            turnTowards(mouseX, mouseY);
        }
        move(5);
        if(isTouching(Food.class))
        {
            removeTouching(Food.class);
            PryHabitat.counter.add(1);

        }
    }    
}
and notice
PryHabitat.counter.add(1);
line. it adds a score, but when it gets value`33` it should stop and show text "Congrats" but the point is:What should we write for "getValue()"???
I know that to import
"import java.awt.Color"
First. ignore telling that.
Super_Hippo Super_Hippo

2017/6/27

#
First, the counter should not be static. You would use '<counterReference>.getValue() == 33' in an if statement. Your counterReference is PryHabitat.counter but that is not really a good idea... As an alternative, you could put the condition into the add method of the counter. Why would you want to import the java.awt.Color class?
Venbha Venbha

2017/6/28

#
um for that
Super_Hippo wrote...
First, the counter should not be static. You would use '<counterReference>.getValue() == 33' in an if statement. Your counterReference is PryHabitat.counter but that is not really a good idea... As an alternative, you could put the condition into the add method of the counter. Why would you want to import the java.awt.Color class?
this line..
Why would you want to import the java.awt.Color class?[/quote wrote...
for that to show text in color.
Super_Hippo Super_Hippo

2017/6/29

#
If you are using the newest Greenfoot version, then there is greenfoot.Color which replaces java.awt.Color. Anyways, I didn't only write this last line.
You need to login to post a reply.