Hello, I am currently creating a platform game but am having a few troubles with the coin counter. When my actor (alien) touches the first coin, the score counter works fine and adds 1 to the score. However, after that, every other coin I touch does not do anything to the score. The coins disappear like I want, but the score does not continue to go up. It just stays on one. I know there must be something wrong in my code somewhere but I cannot for the life of me, work out where it is. I am pretty sure the error is somewhere within the counter class though. Here is some of my code:
Counter class:
Alien Class:
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 CoinCounter extends Actor { private static final Color transparent = new Color(0,0,0,0); private GreenfootImage background; private int value; private int target; /** * Create a new counter, initialised to 0. */ public CoinCounter() { 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; } /** * Return the current counter value. */ public int getValue() { return target; } /** * Set a new counter value. */ public void setValue(int newValue) { target = newValue; value = newValue; updateImage(); } /** * 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.WHITE, transparent); image.drawImage(text, (image.getWidth()-text.getWidth())/2, (image.getHeight()-text.getHeight())/2); setImage(image); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Alien extends Actor { private int vSpeed = 0; //how fast player falls down to platforms private int acceleration = 2; //accelerates the speed of falling, rather than just floating to the ground private boolean jumping; //keeps track of whether player is jumping or not private int jumpStrength = 23; //controls how far up player jumps private int speed = 4; //how fast character moves around private GreenfootImage alien = new GreenfootImage("Alien.png"); private GreenfootImage alien2 = new GreenfootImage("Alien2.png"); private GreenfootImage alienl = new GreenfootImage("Alienl.png"); private GreenfootImage alien2l = new GreenfootImage("Alien2l.png"); private int frame = 1; //keeps track of image shown private int animationCounter = 0; // private int points; private CoinCounter coincounter; public Alien(CoinCounter pointCounter) { coincounter = pointCounter; } public void act() { checkFall(); checkKey(); platformAbove(); checkRightWalls(); checkLeftWalls(); collect(); animationCounter ++; //count up by one Key key = (Key)getOneIntersectingObject(Key.class); if (key != null) key.encountered(); } public void checkKey() { if(Greenfoot.isKeyDown("up") && jumping == false) //cannot be already jumping to make character jump { jump(); } if(Greenfoot.isKeyDown("right")) { moveRight(); } if(Greenfoot.isKeyDown("left")) { moveLeft(); } } public void moveRight() { setLocation(getX() + speed, getY()); if(animationCounter % 10 == 0) animateRight(); } public void moveLeft() { setLocation(getX() - speed, getY()); if(animationCounter % 10 == 0) animateLeft(); } public void animateRight() { if(frame == 1) { setImage(alien); } else if(frame == 2) { setImage(alien2); frame = 1; return; } frame ++; //add one frame to itself } public void animateLeft() { if(frame == 1) { setImage(alienl); } else if(frame == 2) { setImage(alien2l); frame = 1; return; } frame ++; //add one frame to itself } public void fall() { setLocation(getX(), getY() + vSpeed); if(vSpeed <=9) { vSpeed = vSpeed + acceleration; //falls down 2 pixels per cycle } jumping = true; } public boolean onGround() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight/2) + 5; //look for ground 5 pixels below character Actor ground = getOneObjectAtOffset(0, getImage().getHeight()/2, platform.class); if(ground == null) { jumping = true; return false; } else { moveToGround(ground); return true; } } public boolean platformAbove() { int spriteHeight = getImage().getHeight(); int yDistance = (int) (spriteHeight/-2); //look above Actor ceiling = getOneObjectAtOffset(0, yDistance, platform.class); if(ceiling != null) { vSpeed =1; bopHead(ceiling); return true; } else { return false; } } public boolean checkRightWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int) (spriteWidth/2); Actor rightWall = getOneObjectAtOffset(xDistance, 0, platform.class); if(rightWall == null) { return false; } else { stopByRightWall(rightWall); return true; } } public void stopByRightWall(Actor rightWall) { int wallWidth = rightWall.getImage().getWidth(); int newX = rightWall.getX() - (wallWidth + getImage().getWidth())/2; setLocation(newX - 5, getY()); //the - 5 allows the player to bounce off of the wall } public boolean checkLeftWalls() { int spriteWidth = getImage().getWidth(); int xDistance = (int) (spriteWidth/2); Actor leftWall = getOneObjectAtOffset(-xDistance, 0, platform.class); if(leftWall == null) { return false; } else { stopByLeftWall(leftWall); return true; } } public void stopByLeftWall(Actor leftWall) { int wallWidth = leftWall.getImage().getWidth(); int newX = leftWall.getX() + (wallWidth + getImage().getWidth())/2; setLocation(newX + 5, getY()); //the + 5 allows the player to bounce off of the wall } public void bopHead(Actor ceiling) { int ceilingHeight = ceiling.getImage().getHeight(); int newY = ceiling.getY() + (ceilingHeight + getImage().getHeight())/2; setLocation(getX(), newY); } public void moveToGround(Actor ground) { int groundHeight = ground.getImage().getHeight(); int newY = ground.getY() - (groundHeight + getImage().getHeight())/2; setLocation(getX(), newY); jumping = false; } public void checkFall() { if(onGround()) { vSpeed =0; //If the player is on the ground(platform), vertical speed will stop } else { fall(); } } public void jump() { vSpeed = vSpeed - jumpStrength; //makes character move up jumping = true; fall(); } public boolean canSee(Class clss) { return getOneObjectAtOffset(0, 0, clss) != null; } public void eat(Class clss) { Actor actor = getOneObjectAtOffset(0, 0, clss); if(actor != null) { getWorld().removeObject(actor); } } public void collect() { if (canSee(Coin.class)) { eat(Coin.class); coincounter.add(1); } /*if (canSee(Diamond.class)) { eat(Diamond.class); //coincounter.add(1); } */ } }