I'm making a semi-horror game called Ollie's Ordinary Orchard. It's about collecting apples and completing orders. But as time goes on, things get creepier.
I'm having trouble keeping a counter on the screen, because I'm using multiple worlds. The reason I'm using multiple worlds is because I'm using Danpost's portal code. The counter is keeping track of how many apples you have collected, and it keeps disappearing whenever you go to another world/screen. It also gives me an error as well. I don't know what to do and I would like help please.
Here is some code that I think would be important:
/** * * This is the "player class". You control Ollie and move around. * */ public class Ollie extends Actor { private int timer; private GreenfootImage walk1; private GreenfootImage walk2; private static GreenfootSound collect = new GreenfootSound("collect.mp3"); public Ollie() { walk1 = new GreenfootImage("ollieWalk.png"); walk2 = new GreenfootImage("ollieWalk2.png"); setImage(walk1); } /** * The movement code here is all of @Danpost's code, it is not mine. * I have just copied it. But the rest is my code. */ public void act() { int dx = 0, dy = 0; if(Greenfoot.isKeyDown("W")) dy = -4; if(Greenfoot.isKeyDown("A")) dx = -4; if(Greenfoot.isKeyDown("S")) dy = 4; if(Greenfoot.isKeyDown("D")) dx = 4; setLocation(getX()+dx, getY()+dy); if(isTouching(Tree.class)) { setLocation(getX()-dx, getY()-dy); } if(isTouching(HouseHitbox.class)) { setLocation(getX()-dx, getY()-dy); } if(isTouching(BridgeHitBox.class)) { setLocation(getX()-dx, getY()-dy); } if(isTouching(PondHitBox.class)) { setLocation(getX()-dx, getY()-dy); } animation(); pickApples(); } public void pickApples() { Screen1 s1 = (Screen1) getWorld(); if(isTouching(apple.class)) { collect.play(); removeTouching(apple.class); s1.applesPicked.add(1); } } public void animation() { timer = timer + 1; if(timer == 10) { setImage(walk1); } if(timer == 20) { setImage(walk2); timer = 0; } } }
/** * Write a description of class MyWorld here. * * @author (your name) * @version (a version number or a date) */ public class Screen1 extends World { private GreenfootImage test; private static GreenfootSound daytime = new GreenfootSound("music.mp3"); Counter applesPicked = new Counter("Apples Picked: "); /** * The code to put the trees in a row is this: * for(int i = 0; i < 8; i++) { addObject(new Tree(), i*100+50, 43); } * The number at the end, 43, is the number that needs to be changed to * go down. The increasing numbers are: * 129, 215, 301, 387, 473, 559, 645 */ public Screen1() { super(800, 700, 1); //daytime.play(); Hitbox hitbox = new Hitbox(); hitbox = new Hitbox(); addObject(hitbox, 800, 384); hitbox = new Hitbox(hitbox); Screen2 s2 = new Screen2(); s2.addObject(hitbox, 0, 384); hitbox = new Hitbox(); addObject(hitbox, 0, 384); hitbox = new Hitbox(hitbox); Screen8 s8 = new Screen8(); s8.addObject(hitbox, 800, 384); for(int i = 0; i < 8; i++) { addObject(new Tree(), i*100+50, 43); } for(int i = 0; i < 6; i++) { if(i != 3) { addObject(new Tree(), 750, i*86+129); } } for(int i = 0; i < 8; i++) { addObject(new Tree(), i*100+50, 645); } addObject(new House(), 360, 342); addObject(new HouseHitbox(), 359, 310); addObject(new Ollie(), 563, 391); test = new GreenfootImage("screen1.png"); setBackground(test); addObject(applesPicked, 94, 660); applesPicked.setValue(0); } }
/** * 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); } }