Hey guys, Im really new to Greenfoot with an assignement due soon....
I was wondering, I want one actor of mine to chase another actor
Lets say for example I want "Boo" to chase "Slimy", what do I need to do in order to make that happen.


1 2 3 4 5 | void act(){ //this is Boo's act method turnTowards(Slimy.getX(),Slimy.getY()); move( 5 ); } |
1 | Actor slimy = (Actor)getWorld().getObjects(Slimy. class ).get( 0 ); |
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 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Slimy extends Actor { int value = 4 ; int x,y; int counter = 0 ; int speed = 0 ; public void act() { x = getX(); move(); y = getY(); if (Greenfoot.isKeyDown( "left" )) { setRotation( 180 ); move(value); } if (Greenfoot.isKeyDown( "up" )) { setRotation( 270 ); move(value); } if (Greenfoot.isKeyDown( "down" )) { setRotation( 90 ); move(value); } if (Greenfoot.isKeyDown( "right" )) { setRotation( 0 ); move(value); } if (isTouching(RedMushroom. class )) { removeTouching(RedMushroom. class ); //Add 1 to the counter counter++; ((Score)getWorld().getObjects(Score. class ).get( 0 )).ScoreCounter(counter); Greenfoot.playSound( "Slurp.wav" ); } if (isTouching(GreenMushroom. class )) { removeTouching(GreenMushroom. class ); //Add 1 to the counter counter++; ((Score)getWorld().getObjects(Score. class ).get( 0 )).ScoreCounter(counter); Greenfoot.playSound( "Slurp.wav" ); } if (getWorld() instanceof Level1) if (counter> 11 ) { Greenfoot.setWorld( new Level2()); } if (getWorld() instanceof Level2) if (counter> 23 ) { Greenfoot.setWorld( new Level3()); } if (isTouching(Boo. class )) { Greenfoot.setWorld( new GameOverWorld()); } } private void move() { // get change in rotation int dz = 0 ; if (Greenfoot.isKeyDown( "right" )) dz++; if (Greenfoot.isKeyDown( "left" )) dz--; // apply change in rotation setRotation(getRotation() + dz * 5 ); // get change in speed int ds = - 1 ; if (Greenfoot.isKeyDown( "up" )) ds += 20 ; // adjust speed speed += ds; if (speed < 0 ) speed = 0 ; if (speed > 2000 ) speed = 2000 ; // and move if (speed >= 200 ) move(speed / 100 ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) public class Boo extends Slimy { public void act() { turnTowards(Slimy.getX(),Slimy.getY()); move( 5 ); } } |