danpost wrote...
Change 'getWorld()' to '((MyWorld)getWorld())'.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private int countCollected; public void act() { Actor Mario = (Actor)getWorld().getObjects(Mario. class ).get( 0 ); turnTowards (Mario.getX(), Mario.getY()); move( 3 ); if (++countCollected == 3 ) { getWorld().removeObject( this ); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class MyWorld extends World { Counter counter = new Counter(); /** * Constructor for objects of class MyWorld. * */ public MyWorld() { super ( 600 , 400 , 1 ); prepare(); setPaintOrder(Counter. class , Dark. class , PipeOpening. class , Mario. class , Zonnepaneel. class , Toolbox. class , Koperdraad. class , Lampuit. class , Lampaan. class , Bout. class ); } public Counter getCounter() { return counter; } |
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 | public class Mario extends Actor { /** * Act - do whatever the Mario wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { movement(); takeBout(); takeToolbox(); takeKoperdraad(); } public Mario() { setImage(right); } private GreenfootImage left= new GreenfootImage( "left.png" ); private GreenfootImage right= new GreenfootImage ( "right.png" ); private GreenfootImage up= new GreenfootImage ( "up.png" ); private GreenfootImage down= new GreenfootImage ( "down.png" ); public void movement() { if (Greenfoot.isKeyDown( "right" )){ move( 3 ); setImage(right); } else if (Greenfoot.isKeyDown( "left" )) { move(- 3 ); setImage(left); } else if (Greenfoot.isKeyDown( "up" )){ setLocation(getX(), getY()- 3 ); setImage(up); } else if (Greenfoot.isKeyDown( "down" )) { setLocation(getX(), getY()+ 3 ); setImage(down); } } public void takeBout(){ Actor bout; bout = getOneObjectAtOffset( 0 , 0 , Bout. class ); if (bout != null ) { World myWorld = getWorld(); ((MyWorld)getWorld()).getCounter().addScore(); myWorld.removeObject(bout); } } public void takeToolbox(){ Actor toolbox; toolbox = getOneObjectAtOffset( 0 , 0 , Toolbox. class ); if (toolbox != null ) { World myWorld = getWorld(); ((MyWorld)getWorld()).getCounter().addScore(); myWorld.removeObject(toolbox); } } public void takeKoperdraad(){ Actor koperdraad; koperdraad = getOneObjectAtOffset( 0 , 0 , Koperdraad. class ); if (koperdraad != null ) { World myWorld = getWorld(); myWorld.removeObject(koperdraad); ((MyWorld)getWorld()).getCounter().addScore(); } } } |
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 | private void prepare() { Mario mario = new Mario(); addObject(mario, 235 , 212 ); mario.setLocation( 218 , 180 ); Dark dark = new Dark(); Bout bout = new Bout(); addObject(bout, 456 , 121 ); bout.setLocation( 258 , 58 ); Toolbox toolbox = new Toolbox(); addObject(toolbox, 553 , 372 ); bout.setLocation( 189 , 23 ); Koperdraad koperdraad = new Koperdraad(); addObject(koperdraad, 27 , 376 ); Lampuit lampuit = new Lampuit(); addObject(lampuit, 306 , 189 ); lampuit.setLocation( 303 , 182 ); Zonnepaneel zonnepaneel = new Zonnepaneel(); addObject(zonnepaneel, 48 , 194 ); toolbox.setLocation( 560 , 23 ); PipeOpening pipeOpening = new PipeOpening(); addObject(pipeOpening, 594 , 343 ); toolbox.setLocation( 564 , 30 ); lampuit.setLocation( 352 , 176 ); mario.setLocation( 274 , 196 ); Lampaan lampaan = new Lampaan(); addObject(lampaan, 155 , 179 ); lampuit.setLocation( 152 , 188 ); mario.setLocation( 207 , 184 ); Koperdraad koperdraad2 = new Koperdraad(); addObject(koperdraad2, 394 , 203 ); removeObject(koperdraad2); mario.setLocation( 180 , 168 ); addObject(dark, 180 , 168 ); mario.setLocation( 199 , 175 ); dark.act(); lampaan.setLocation( 151 , 184 ); removeObject(dark); Counter counter = new Counter(); addObject(counter, 32 , 30 ); counter.setLocation( 49 , 22 ); counter.setLocation( 48 , 24 ); } } |
1 2 3 4 5 | if (((MyWorld)getWorld()).getCounter().getScore() == 3 ) { getWorld().removeObject( this ); return ; } |
1 | if (getWorld() != null ) |
1 2 3 4 5 | if (((MyWorld)getWorld()).getCounter().getScore() == 3 ) { getWorld().removeObject( this ); return ; } |
1 | if (getWorld() != null ) |
1 2 3 4 5 | if (((MyWorld)getWorld()).getCounter().getScore() == 3 ) { getWorld().removeObject( this ); return ; } |