danpost wrote...
Change 'getWorld()' to '((MyWorld)getWorld())'.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); } }
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; }
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(); } } }
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); } }
if (((MyWorld)getWorld()).getCounter().getScore() == 3) { getWorld().removeObject(this); return; }
if (getWorld() != null)
if (((MyWorld)getWorld()).getCounter().getScore() == 3) { getWorld().removeObject(this); return; }
if (getWorld() != null)
if (((MyWorld)getWorld()).getCounter().getScore() == 3) { getWorld().removeObject(this); return; }