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

2014/2/13

Having Trouble with an Ammo Counter?

JaffyWaffy JaffyWaffy

2014/2/13

#
Okay so I'm fairly new to coding and everything still and what I'm trying to do is to have an ammo counter that starts at 3 and goes down by one when my Penguin shoots. I've been sorta of following tutorials online but instead of adding to the score I've been trying to minus but it doesn't work? all the code complies I'm just stuck on why it's not actually working.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) 
import java.awt.Color;

My Space Class;
[code]public class Space extends World
{

    Counter counter = new Counter();
    
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        addToWorld();
    }
    

    private void addToWorld() {
    
      Counter counter = new Counter();
       addObject(new Counter(), 35, 50);

   
        addObject(new Penguin(counter),80,290);
        addObject(new Seal(), 550,280);
        addObject(new Seal(), 480,320);
        addObject(new Seal(), 380,290);
    }
}
The Counter Class I'm using;
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 = 3;
    private int target;
    
    /**;
     * Create a new counter, initialised to 0.
     */
    public Counter()
    {
        background = getImage();  // get image from class
       
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
         value = 3;
        target = 3;
        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;
    }
    
    //Adding in a Minus scale for the ammo counter/life counter
       /**
     * Minus a new score from the current counter value.
     */
    public void minus(int score)
    {
        target -= score;
    }

    /**
     * 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();
    }

    /**
     * Update the image on screen to show the current value.
     */
    //Adding the text to the counter and the size of the font too.
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage("" + value, 40, Color.WHITE, transparent);
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
Penguin Class;
/**
 * Write a description of class Penguin here.
 * 
 * @author Jordan
 * @version
 */
public class Penguin extends Actor

{
    public Counter counter;

    public Penguin(Counter ammoCounter)
    {
        this.counter = ammoCounter;
    }

    public void act() //Setting the default position of the penguin and default image until changes
    {
        setImage("PengIdle.gif");
        if (Greenfoot.mouseClicked(null)) shoot(); 
         
    }

    
    public void shoot() //Ititated the shooting method which fires the fish from the location of
    //the Penguin.
    {
        counter.minus(1);    
        MouseInfo mouse = Greenfoot.getMouseInfo(); 
        if (mouse == null) return; 
        int x = mouse.getX(); 
        int y = mouse.getY(); 
        Fish fish = new Fish();
        spawnFish();

    }
    
    public void spawnFish()
    {
        //Spawns the Fish into the world to shoot at the seal at the current coords of Penguin
        getWorld().addObject(new Fish(), getX(), getY());       
                      
    }

}

As far as I know I've got most of it right just the part where it works?
danpost danpost

2014/2/13

#
The problem is that you are creating three counters in the Space class: (1) when declaring the instance field at line 8 (2) when declaring the local field in the 'addToWorld' method at line 24 (3) when adding a new one into the world at line 25 The one created for the local field (2) is passed to the Penguin object and is not the one that was added to the world in (3); and neither of these are the one saved in the instance field which is the one whose reference is retained throughout the life of the Space world object. Remove line 24 and use 'counter' instead of 'new Counter()' in line 25.
You need to login to post a reply.