In my game, I need to make it so that when an actor eats another actor, the score is recorded in a scoreboard. How do I create the scoreboard and allow it to record scores?


1 | theReferenceToTheObject.add( 1 ); |
1 | Counter.add( 1 ); |
1 2 3 4 5 | public void addScore( int sceor) { if (!getWorld().getObjects(Counter. class ).isEmpty()) { getWorld().getObjects(Counter. class ).get( 0 ).add(score); } } |
1 | ((Counter) getWorld().getObjects(Counter. class ).get( 0 )).add(score); |
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 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) public class Crab extends Animal { private GreenfootImage image1; private GreenfootImage image2; private int wormsEaten; private CrabWorld crabWorld = null ; private ScoreBoard scoreBoard = null ; public Crab() { image1 = new GreenfootImage( "crab.png" ); image2 = new GreenfootImage( "crab2.png" ); setImage(image1); wormsEaten = 0 ; } /**act- includes all methods that allow the crab to perform all actions needed in the game.*/ public void act() { checkKeypress(); lookForWorm(); switchImage(); } /**selectImage- allows the crab to select the images that it will need to change between for the switchImage() method.*/ public void selectImage() { if (getImage() == image1) { setImage(image2); } else { setImage(image1); } } /**switchImage- allows the crab to change between images when it moves so that it looks like it's moving.*/ public void switchImage() { if (Greenfoot.isKeyDown( "up" )) { selectImage(); } if (Greenfoot.isKeyDown( "down" )) { selectImage(); } if (Greenfoot.isKeyDown( "left" )) { selectImage(); } if (Greenfoot.isKeyDown( "right" )) { selectImage(); } } /**checkKeypress- allows the crab to move and turn in different directions according to the arrows being pressed on the keyboard.*/ public void checkKeypress() { if (Greenfoot.isKeyDown( "left" )) { turn(- 6 ); } if (Greenfoot.isKeyDown( "right" )) { turn( 6 ); } if (Greenfoot.isKeyDown( "up" )) { move( 7 ); } if (Greenfoot.isKeyDown( "down" )) { move(- 7 ); } } /**lookForWorm- allows the crab to eat the worms and add score and wormcounts accordingly.*/ public void lookForWorm() { CrabWorld datWorld= (CrabWorld) getWorld(); if (canSee(Worm. class )) { eat(Worm. class ); Greenfoot.playSound( "slurp.wav" ); addScore(); } } public void addScore() { if (!getWorld().getObjects(Counter. class ).isEmpty()) { ((Counter) getWorld().getObjects(Counter. class ).get( 0 ))add( 1 ); } } /**setLocation- This code is here so that the Rocks and Trees act as obstacles and the character can't walk through them.*/ public void setLocation( int x, int y) { int oldX = getX(); int oldY = getY(); super .setLocation(x,y); if (!getIntersectingObjects(Rock. class ).isEmpty()) { super .setLocation(oldX, oldY); } if (!getIntersectingObjects(Tree. class ).isEmpty()) { super .setLocation(oldX, oldY); } } } |