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

2018/5/13

Hi I recently created a health and a score counter in my game but one of them keeps increasing by 4 and the other by 1. I only want them to increase by 1, please help

1
2
Heet_Patel Heet_Patel

2018/5/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Shark here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Shark extends Actor
{
    /**
     * Act - do whatever the Shark wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        move(-4);
        if (Greenfoot.isKeyDown("Left"))
        {
            turn(-3);
        }
        if (Greenfoot.isKeyDown("Right"))
        {
            turn(3);
        }
        Actor Fish;
        Fish = getOneObjectAtOffset(0,0,Fish.class);
        if(Fish!=null)
        {
            Water thehouse = (Water)getWorld();
            thehouse.removeObject(Fish);
            Counter counter = thehouse.getCounter();
            counter.bumpCount(1);
        }    

    }    
}
This is how you get points in the counter. The shark eats the fish and gets one more point.
Heet_Patel Heet_Patel

2018/5/15

#
Add this for the counter in the world class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Water here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Water extends World
{
    GreenfootSound backgroundMusic = new GreenfootSound("Waterlevelsound.mp3");    
   [u]private Counter theCounter;[/u]
    /**
     * Constructor for objects of class Water.
     * 
     */
    public Water()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(1000, 700, 1);
        [u]theCounter = new Counter();
        addObject(theCounter, 500, 20);[/u]        
        prepare();
    }
   [u]public Counter getCounter()
    {
        return theCounter;
    }[/u]
}
add the underlined codes into the world class
Heet_Patel Heet_Patel

2018/5/15

#
don't include the {u} and {/u} parts. Those are supposed to be underlined. The {} have been replaced into the square brackets in the code
Heet_Patel Heet_Patel

2018/5/15

#
So basically add line 12, line 21, line 22, line 25, line 26, line 27, line 28
You need to login to post a reply.
1
2