Hello, I've put in a counter code into my game from the previous code I've learnt from. But the counter is not increasing/updating the points after the pirate touches the coin. Here is the code for the coin and counter class.
I think the problem is in the coin class, in this section of code.
Because if I remove the private void wasICollected code, the game still works even if it was there or not. And so most likely the boolean is the problem that is causing the oCounter.add(iPoint); from not working. I'm not too sure, this is what I think but its most likely not right.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class Coin here. * * @author (your name) * @version (a version number or a date) */ public class Coin extends Actor { private int iColor; private boolean boolRemove; int iPoint = 1 ; Counter oCounter; public Coin( int iTn){ iColor = iTn; setImage( "coin" +iTn+ ".png" ); } public int getPoints(){ return 5 *( 4 -iColor); } /** * Act - do whatever the Coin wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public Coin(){ this ( 0 ); } public void act() { boolRemove = false ; // Add your action code here. boolRemove = wasIJustCollected(); setLocation( this .getX(), this .getY()+ 3 ); // at the bottom of the world delete the coin if (getY()> 430 ){ boolRemove = true ; } if (boolRemove){ // Remove the coin this .getWorld().removeObject( this ); } // Remove me after all other lines of code have executed } private boolean wasIJustCollected(){ return (isTouching(Pirate. class )); } private void wasICollected(){ if (isTouching(Pirate. class )){ oCounter.add(iPoint); } } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | 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; private int target; private int iCount; /** * Create a new counter, initialised to 0. */ public Counter() { background = getImage(); // get image from class value = 0 ; target = 0 ; 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. */ public void add( int score) { target += score; iCount++; } /** * 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(); } public int getCount(){ return iCount; } /** * Update the image on screen to show the current value. */ private void updateImage() { GreenfootImage image = new GreenfootImage(background); GreenfootImage text = new GreenfootImage( "" + value, 22 , Color.BLACK, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/ 2 , (image.getHeight()-text.getHeight())/ 2 ); setImage(image); } } |
1 2 3 4 5 6 7 8 9 | private boolean wasIJustCollected(){ return (isTouching(Pirate. class )); } private void wasICollected(){ if (isTouching(Pirate. class )){ oCounter.add(iPoint); } } |