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

2014/10/25

Score Updates, but Proton Charge Does Not

BryanGSMST BryanGSMST

2014/10/25

#
I have two images in my world that are strings: a score counter and a charge counter. The score updates, but the proton wave's charge does not. The score is of the Counter class, and the charge is of the ProtonCharge class. Both are created in the Space world. Click for My Code
danpost danpost

2014/10/26

#
Please either post the code in the discussion thread or upload your scenario with source published. I, for one, refuse to acquire source from any other locations. It will not be necessary to show the Counter or ProtonCharge classes -- just the code that deals with the objects created from those classes (or at least your ProtonCharge object, since that is the one you are experiencing problems with).
danpost danpost

2014/10/26

#
You should be able to get it to work if you set up the ProtonCharge class and object created from that class similar to that of the Counter and its object.
BryanGSMST BryanGSMST

2014/10/26

#
danpost wrote...
You should be able to get it to work if you set up the ProtonCharge class and object created from that class similar to that of the Counter and its object.
They are literally the same thing, but one wants to update and the other doesn't.
Super_Hippo Super_Hippo

2014/10/26

#
Post the related code here. Then we can help you. To do this, click on 'code' below the text field (left side) and then, paste your code there.
BryanGSMST BryanGSMST

2014/10/26

#
Super_Hippo wrote...
Post the related code here. Then we can help you. To do this, click on 'code' below the text field (left side) and then, paste your code there.
ProtonCounter (one that doesn't work):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;

/**
 * Counter that displays a text and number.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class ProtonCounter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
    
    public static int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;

    public ProtonCounter()
    {
        this("100");
    }

    public ProtonCounter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);

        updateImage();
    }
    
    public void act() {
        updateImage();
    }
    
    public static int getCharge()
    {
        return Rocket.protonDelayCount;
    }

    /**
     * Make the image
     */
    public void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
Counter (one that works):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;

/**
 * Counter that displays a text and number.
 * 
 * @author Michael Kolling
 * @version 1.0.1
 */
public class Counter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
    
    public static int value = 0;
    private int target = 0;
    private String text;
    private int stringLength;

    public Counter()
    {
        this("");
    }

    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);

        updateImage();
    }
    
    public void act() {
        updateImage();
    }

    public int getValue()
    {
        return value;
    }

    /**
     * Make the image
     */
    public void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
Space (where Counter and ProtonCounter get created):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * Space. Something for rockets to fly in.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Space extends World
{
    private Counter scoreCounter;
    private ProtonCounter charge;
    private LevelCounter levelCounter;
    public int level = 0;
    private int asteroids = 2;
    public Space() 
    {
        super(600, 400, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        
        
        Rocket rocket = new Rocket();
        Rocket.protonDelayCount = 500;
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);
        
        addAsteroids(asteroids);
        
        scoreCounter = new Counter("Score:" + ScoreBoard.score);
        addObject(scoreCounter, 60, 380);
        
        charge = new ProtonCounter("Charge: " + ProtonCounter.getCharge());
        addObject(charge, 200, 380);

        levelCounter = new LevelCounter("Level:" + level);
        addObject(levelCounter, 320, 380);
        
        Explosion.initializeImages();
        ProtonWave.initializeImages();
        
        Counter.value = 0;
        ProtonCounter.value = 0;
        LevelCounter.value = 0;
        
        createStars(300);
    }
    
    public void act(){
        if (numberOfObjects() == 4){
            level++;
            addAsteroids(asteroids + level);
            Greenfoot.playSound("chipquest.wav");
        }
    }
    
    /**
     * Add a given number of asteroids to our world. Asteroids are only added into
     * the left half of the world.
     */
    private void addAsteroids(int count) 
    {
        for(int i = 0; i < count; i++) 
        {
            int x = Greenfoot.getRandomNumber(getWidth()/2);
            int y = Greenfoot.getRandomNumber(getHeight()/2);
            addObject(new Asteroid(), x, y);
        }
    }
    private void createStars(int number)
    {
        GreenfootImage background = getBackground();
        for (int i = 0; i < number; i++)
        {
            int x = Greenfoot.getRandomNumber (getWidth());
            int y = Greenfoot.getRandomNumber (getHeight());
            int color = 120 - Greenfoot.getRandomNumber(100);
            background.setColor(new Color(color, color, color));
            background.fillOval(x, y, 2, 2);
        }
    }
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        // TODO: show the score board here. Currently missing.
        Counter.value = 0;
        addObject(new ScoreBoard(ScoreBoard.score), getWidth()/2, getHeight()/2);
        ScoreBoard.reset();
        level = 0;

    }
}
Rocket (where protonDelayCharge is):
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A rocket that can be controlled by the arrowkeys: up, left, right.
 * The gun is fired by hitting the 'space' key. 'z' releases a proton wave.
 * 
 * @author Poul Henriksen
 * @author Michael Kolling
 * 
 * @version 1.0
 */
public class Rocket extends SmoothMover
{
    private static final int gunReloadTime = 5;         // The minimum delay between firing the gun.
    private static final int protonReloadTime = 500; 
    
    private int reloadDelayCount;               // How long ago we fired the gun the last time.
    public static int protonDelayCount;
    
    private GreenfootImage rocket = new GreenfootImage("rocket.png");    
    private GreenfootImage rocketWithThrust = new GreenfootImage("rocketWithThrust.png");
    /**
     * Initilise this rocket.
     */
    public Rocket()
    {
        reloadDelayCount = 5;
        addForce(new Vector(13,0.3));
        reloadDelayCount = 5;
    }

    /**
     * Do what a rocket's gotta do. (Which is: mostly flying about, and turning,
     * accelerating and shooting when the right keys are pressed.)
     */
    public void act()
    {
        checkKeys();
        move();
        reloadDelayCount++;
        ignite(Greenfoot.isKeyDown("up"));
        checkCollision();
        reloadDelayCount++;
        protonDelayCount++;
    }
    public void startProtonWave()
        {
           if (protonDelayCount >= protonReloadTime){
           ProtonWave wave = new ProtonWave();
           getWorld().addObject(wave, getX(), getY());
           protonDelayCount = 0;
        }
        }
    public void ignite(boolean boosterOn)
    {

        if (boosterOn)
        {
            setImage (rocketWithThrust);
            addForce (new Vector(getRotation(),0.1));
        }
        else
        {
            setImage(rocket);
        }
    }
    /**
     * Check whether there are any key pressed and react to them.
     */
    private void checkKeys() 
    {
        // place code here
        if (Greenfoot.isKeyDown("space")){
            fire();
        }
        if (Greenfoot.isKeyDown("left")){
            setRotation(getRotation() - 5);
        }
        if (Greenfoot.isKeyDown("right")){
            setRotation(getRotation() + 5);
        }
        if (Greenfoot.isKeyDown("z")){
            startProtonWave();
        }
    }
    private void checkCollision() 
    {
        // place code here
        Actor a = getOneIntersectingObject(Asteroid.class);
        if (a != null)
        {
            Space space = (Space) getWorld();
            space.addObject(new Explosion(), getX(), getY());
            space.removeObject(this);
            space.gameOver();
            Counter.value = 0;
        }
    }
    /**
     * Fire a bullet if the gun is ready.
     */
    private void fire() 
    {
        if (reloadDelayCount >= gunReloadTime) 
        {
            Bullet bullet = new Bullet (getMovement().copy(), getRotation());
            getWorld().addObject (bullet, getX(), getY());
            bullet.move ();
            reloadDelayCount = 0;
        }
    }
    
}
Super_Hippo Super_Hippo

2014/10/26

#
Where are you changing the values the counters should display?
BryanGSMST BryanGSMST

2014/10/26

#
Super_Hippo wrote...
Where are you changing the values the counters should display?
Here: public void act()
public void act()  
    {  
        checkKeys();  
        move();  
        reloadDelayCount++;  
        ignite(Greenfoot.isKeyDown("up"));  
        checkCollision();  
        reloadDelayCount++;  
        protonDelayCount++;
    }  
    public void startProtonWave()  
        {  
           if (protonDelayCount >= protonReloadTime){  
           ProtonWave wave = new ProtonWave();  
           getWorld().addObject(wave, getX(), getY());  
           protonDelayCount = 0; 
        }  
        }  
Super_Hippo Super_Hippo

2014/10/26

#
You change the protonDelayCount there. Your ProtonCounter doesn't check this value.
BryanGSMST BryanGSMST

2014/10/26

#
Super_Hippo wrote...
You change the protonDelayCount there. Your ProtonCounter doesn't check this value.
So what do I do?
Super_Hippo Super_Hippo

2014/10/26

#
You could write 'ProtonCounter.value = protonDelayCount;' at the end of the act method.
danpost danpost

2014/10/26

#
This issue is the result of having more than one field trying to track the same value. You may think that it is easier to get the value of a field from one class to another by doing so; but, that is not the case -- especially when you are using a field that is 'static' (or multiple fields that are 'static'). Here, you have a static field 'protonDelayCount' in your Rocket class and a static field 'value' in your ProtonCounter class. Then you have added a method 'getCharge' (which, by the way, is probably what should have been used in the act method before the 'update' method call). First, 'get' methods are usually used to return a value that is from within the class, not from a different class. Second, because the 'value' field is public and static, the 'get' method is not needed at all. Instead of attempting to keep the two fields in different classes with the same value. Just use one field (I would suggest keeping the 'value' field in the ProtonCounter class, just to be consistant with the Counter class) and control it from the Rocket class using 'ProtonCounter.value' to refer to the field. Sidenote: the 'target' fields in both the ProtonCounter and the Counter class can be removed as they are not being used.
You need to login to post a reply.