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

2019/5/31

Hey, just wondering how to make counter work across lost of worlds

AmyIsBadAtCoding AmyIsBadAtCoding

2019/5/31

#
Already asked but to make it clear: the other person had a TOTALLY different game so the code is different. Keep in mind I also want to make it so the higher the counter is the faster the enemy goes-I would like access to the 'target' variable in other worlds so I can do this please. Am using 2.4.1 which is my school version btw. The counter code is the same as default but I'll give you it anyway:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * A Counter class that allows you to display a numerical value on screen.
 * 
 * The Counter is an actor, so you will need to create it, and then add it to
 * the world in Greenfoot.  If you keep a reference to the Counter then you
 * can adjust its value.  Here's an example of a world class that
 * displays a counter with the number of act cycles that have occurred:
 * 
 * <pre>
 * class CountingWorld
 * {
 *     private Counter actCounter;
 *     
 *     public CountingWorld()
 *     {
 *         super(600, 400, 1);
 *         actCounter = new Counter("Act Cycles: ");
 *         addObject(actCounter, 100, 100);
 *     }
 *     
 *     public void act()
 *     {
 *         actCounter.setValue(actCounter.getValue() + 1);
 *     }
 * }
 * </pre>
 * 
 * @author Neil Brown and Michael K├Âlling 
 * @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;
    private int target;
    private String prefix;
    
    public Counter()
    {
        this(new String());
    }

    /**
     * Create a new counter, initialised to 0.
     */
    public Counter(String prefix)
    {
        background = getImage();  // get image from class
        value = 0;
        target = 0;
        this.prefix = prefix;
        updateImage();
    }
    
    /**
     * Animate the display to count up (or down) to the current target value.
     */
    public void act() 
    {
        if (value < target) {
            value++;
            updateImage();
        }
        else if (value > target) {
            value--;
            updateImage();
        }
    }

  /**
     * Add a new score to the current counter value.  This will animate
     * the counter over consecutive frames until it reaches the new value.
     */
    public void add(int score)
    {
        target += score;
    }

    /**
     * Return the current counter value.
     */
    public int getValue()
    {
        return target;
    }
    
    
    /**
     * Set a new counter value.  This will not animate the counter.
     */
    public void setValue(int newValue)
    {
        target = newValue;
        value = newValue;
        updateImage();
    }
    
    /**
     * Sets a text prefix that should be displayed before
     * the counter value (e.g. "Score: ").
     */
    public void setPrefix(String prefix)
    {
        this.prefix = prefix;
        updateImage();
    }

    /**
     * Update the image on screen to show the current value.
     */
    private void updateImage()
    {
        GreenfootImage image = new GreenfootImage(background);
        GreenfootImage text = new GreenfootImage(prefix + value, 22, Color.BLACK, transparent);
        
        if (text.getWidth() > image.getWidth() - 20)
        {
            image.scale(text.getWidth() + 20, image.getHeight());
        }
        
        image.drawImage(text, (image.getWidth()-text.getWidth())/2, 
                        (image.getHeight()-text.getHeight())/2);
        setImage(image);
    }
}
danpost danpost

2019/5/31

#
What do you have in your other classes dealing with Counter objects?
AmyIsBadAtCoding AmyIsBadAtCoding

2019/6/10

#
I believe this Plane class is the only one that deals with the counter:
import greenfoot.*;

/**
 * Write a description of class Plane here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Plane extends Actor
{
/**
 * shrinking plane size
 */
    public Plane()
    {
     GreenfootImage image = getImage();
        image.scale(image.getWidth() - 60, image.getHeight() - 60);
        setImage(image);
        }
    
   
    private Counter counter;
/**
 * adding up-down controlls and if the actor touches the right side they will go to a random world
 */
    public void act() 
    { 
    
        {
            setRotation(0);
            move(2);
    }
        if (Greenfoot.isKeyDown("Up"))
    {
            setRotation(-20);
            move(2);
    }
    if (Greenfoot.isKeyDown("Down"))
    {
        setRotation(50);
            move(2);
        }
    if (getX() == getWorld().getWidth()-1)
    {
        if (Greenfoot.getRandomNumber(3)==0)
        {
            Greenfoot.setWorld(new SkyEnemy());
    }
       if (Greenfoot.getRandomNumber(3)==1)
       {
           Greenfoot.setWorld(new SkyEnemyTwo());
        }
        if (Greenfoot.getRandomNumber(3)==2)
        {
            Greenfoot.setWorld(new SkyEnemyThree());
        }
    }
    /** 
     * plane can collect ball, play sound and add 1 to counter
     */
    Actor Ball = getOneObjectAtOffset(0,0,Ball.class);
    if (Ball != null)
    {
        getWorld().removeObject(Ball);
        Greenfoot.playSound ("Coin-collect-sound-effect.mp3");
        counter.add(1);
    }
}
/**
 * counter code
 */
public Plane (Counter pointcounter)
{
    this();
    counter = pointcounter;
}
}
danpost danpost

2019/6/10

#
AmyIsBadAtCoding wrote...
I believe this Plane class is the only one that deals with the counter: << Code Omitted >>
You probably also create a Counter object in you World subclass and do stuff with it there (show code).
AmyIsBadAtCoding AmyIsBadAtCoding

2019/6/11

#
Heres one of the world codes, the only differences between the other worlds and this one is the enemy planes and balls(things you collect) spawn and where they spawn.
import greenfoot.*;

/**
 * Write a description of class Sky here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Sky extends World
{
    
    /**
     * Constructor for objects of class Sky.
     * 
     */
    public Sky()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 

        prepare();
        
    }
    
    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        Counter counter3 = new Counter();
        addObject(counter3, 66, 33);
        Plane plane3 = new Plane(counter3);
        addObject(plane3, 12, 227);
    }
danpost danpost

2019/6/11

#
I think if you just make the Counter field in your Plane class public static (line 22):
public static Counter counter;
then, in all your other worlds (other than Sky), you can just create a Plane object without having to worry about the counter. That is, use new Plane() and add the plane and counter into the world:
addObject(new Plane(), << x, y >>);
addObject(Plane.counter, << x, y >>);
AmyIsBadAtCoding AmyIsBadAtCoding

2019/6/12

#
Since my counter variable is called counter3, would I put
addObject(new Plane(), << x, y >>);
addObject(Plane.counter3, << x, y >>);
danpost danpost

2019/6/12

#
AmyIsBadAtCoding wrote...
Since my counter variable is called counter3, would I put << Code Omitted >>
It is not called counter3 (at least, not in the Plane class).
AmyIsBadAtCoding AmyIsBadAtCoding

2019/6/12

#
Now it's throwing a hissy fit, the world constructor threw an exception. It doesn't like lines 21 and 44 saying: java.lang.NullPointerException
import greenfoot.*;

/**
 * Write a description of class SkyEnemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SkyEnemy extends World
{

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

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        BadPlane badplane = new BadPlane();
        addObject(badplane, 520, 96);
        badplane.setRotation(90);
        badplane.setRotation(180);
        badplane.setLocation(524, 77);
        badplane.setLocation(521, 66);
        removeObject(badplane);
        BadPlane badplane2 = new BadPlane();
        addObject(badplane2, 595, 92);
        badplane2.act();
        badplane2.setRotation(180);
        Ball ball = new Ball();
        addObject(ball, 321, 346);
        addObject(new Plane(),  17, 204 );
        addObject(Plane.counter,  66, 33 );
    }
}
You need to login to post a reply.