That worked great. Now if I add a public void eat, do I add a getColor and return color and then add the eat function?


1 | for ( int y= 2 ; y< 33 ; y+= 4 ) for ( int x= 2 ; x< 33 ; x+= 4 ) |
1 2 3 4 5 6 7 8 9 10 11 | public void eat() { Actor tile; tile = getOneObjectAtOffset( 0 , 0 , Tile. class ); if (tile != null ) { World world; world = getWorld(); world.removeObject(tile); } } |
1 2 3 4 5 | public void getEaten() { color = 0 ; setImage( "egg0.png" ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | Tile tile = (Tile)getOneObjectAtOffset( 0 , 0 , Tile. class ); if (tile == null || tile.getColor() == 0 ) { return ; } if (tile.getColor() != color) { setLocation(getX()-dx, getY()-dy); } else { tile.getEaten(); mutate(); } |
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, Greenfoot and MouseInfo) /** * Write a description of class Arrow here. * * @author (your name) * @version (a version number or a date) */ public class Arrow extends Actor { private Counter counter; private int tileDelayCount; public Arrow(Counter pointCounter) { counter = pointCounter; tileDelayCount = 0 ; } /** * Act - do whatever the Arrow wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int color; public Arrow() { color = Greenfoot.getRandomNumber( 5 )+ 1 ; setImage( new GreenfootImage( "arrow" +color+ ".png" )); } public int getColor() { return color; } public void addedToWorld(World world) { while (((Tile)getOneObjectAtOffset( 0 , 0 , Tile. class )).getColor() != 0 ) { int x = Greenfoot.getRandomNumber(world.getWidth()); int y = Greenfoot.getRandomNumber(world.getHeight()); setLocation(x, y); } } private void mutate() { color = Greenfoot.getRandomNumber( 5 )+ 1 ; setImage( "arrow" +color+ ".png" ); } public void act() { move(); checkNewTile(); newTile(); } public void move() { String key = Greenfoot.getKey(); int dx = 0 , dy = 0 ; // the offsets from current location if ( "up" .equals(key)) dy--; if ( "down" .equals(key)) dy++; if ( "left" .equals(key)) dx--; if ( "right" .equals(key)) dx++; setLocation(getX()+dx, getY()+dy); Tile tile = (Tile)getOneObjectAtOffset( 0 , 0 , Tile. class ); if (tile == null || tile.getColor() == 0 ) { return ; } if (tile.getColor() != color) { setLocation(getX()-dx, getY()-dy); } else { tile.getEaten(); mutate(); newTile(); } private void newTile() { tileDelayCount = 100 ; // check for illegal movement here and reset location adding negative offsets } private void checkNewTile() { if (tileDelayCount > 0 ) { tileDelayCount--; if (tileDelayCount == 0 ) { createNewTile(); } } } private void createNewTile() { int num = Greenfoot.getRandomNumber( 15 ); if (num > 5 ) num = 0 ; addObject( new Tile(num), x, y); } } } |
1 2 3 4 5 6 | if (tileDelayCount > 0 ) { tileDelayCount--; if (tileDelayCount == 0 ) { mutate(); } } |