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

2018/6/26

Updating my score board

amazingfish amazingfish

2018/6/26

#
When I create a score board for my game, I don't find it updating the score when it should. Can anyone help me with it please, thank you.
danpost danpost

2018/6/26

#
amazingfish wrote...
When I create a score board for my game, I don't find it updating the score when it should. Can anyone help me with it please, thank you.
Please provide World subclass codes for review.
amazingfish amazingfish

2018/6/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class CrabWorld here. * * @author (your name) * @version (a version number or a date) */ public class Space extends World { Counter counter = new Counter(); /** * Constructor for objects of class CrabWorld. * */ public Space() { super(1000, 1000, 1); populate(); } public Counter getCounter() { return counter; } public void populate() { Shark w1 = new Shark(); addObject(w1, 300, 300); Shark w2 = new Shark(); addObject(w2, 100, 700); Shark w3 = new Shark(); addObject(w3, 100, 700); Shark w4 = new Shark(); addObject(w4, 100, 700); Small_fish l1 = new Small_fish(); addObject(l1, 500, 300); Small_fish l2 = new Small_fish(); addObject(l2, 0, 200); Small_fish l3 = new Small_fish(); addObject(l3, 700, 500); Small_fish l4 = new Small_fish(); addObject(l4, 200, 600); Small_fish l5 = new Small_fish(); addObject(l5, 500, 0); Small_fish l6 = new Small_fish(); addObject(l6, 400, 700); Small_fish l7 = new Small_fish(); addObject(l7, 600, 300); Small_fish l8 = new Small_fish(); addObject(l8, 100, 200); Small_fish l9 = new Small_fish(); addObject(l9, 800, 500); Player_fish P1 = new Player_fish(); addObject(P1, 500, 500); Player2_fish P2 = new Player2_fish(); addObject(P2, 600, 600); Counter C1 = new Counter(); addObject(C1,200,200); } }
amazingfish amazingfish

2018/6/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Counter extends Actor { int score = 0; public void act() { setImage(new GreenfootImage ("Score"+ score,24, Color.RED, Color.BLACK)); } public void addSocre() { score++; } }
amazingfish amazingfish

2018/6/27

#
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Player_fish extends Actor { World myWorld; public void addedToWorld(World w) {myWorld= w;} /** * Act - do whatever the Crab wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { moveAndTurn(); eat(); } /** Crab controls*/ public void moveAndTurn() { if (Greenfoot.isKeyDown("left")) { turn(-3); } if (Greenfoot.isKeyDown("right")) { turn(3); } if (Greenfoot.isKeyDown("up")) { move(4); } if (Greenfoot.isKeyDown("down")) { move(-4); } } /** Eating worms*/ public void eat() { Actor Small_fish; Small_fish = getOneObjectAtOffset(0,0,Small_fish.class); if (Small_fish !=null) { World world; World myWorld = getWorld(); myWorld.removeObject(Small_fish); Space space = (Space)myWorld; Counter counter = space.getCounter(); counter.addSocre(); Greenfoot.playSound("eating.wav"); } } }
danpost danpost

2018/6/27

#
In your Space class, change the last two lines -- these:
Counter C1 = new Counter();
addObject(C1,200,200);
to the following:
counter = new Counter();
addObject(counter, 200, 200);
amazingfish amazingfish

2018/6/27

#
Thank you danpost! Love the solution!
You need to login to post a reply.