This site requires JavaScript, please enable it in your browser!
Greenfoot back
QueryLover
QueryLover wrote ...

2017/4/12

Can't pick up flowers in Level2, only in Level1.

QueryLover QueryLover

2017/4/12

#
how can i make this code so that it is applied in every level and not only in level1? public void pickFlowers() { if (canSee(Flower.class)) { Flower flower = (Flower) getOneObjectAtOffset(0, 0, Flower.class); // eat(Flower.class); if(flower.getPoisonous()) { score=score-5; }else { score++; } removeObject(Flower.class); Level1 level1 = (Level1)getWorld(); World world = getWorld(); world.removeObjects(world.getObjects(Scoreboard.class)); world.addObject (new Scoreboard(score), 75,25); } }
danpost danpost

2017/4/12

#
You made your picker strictly a Level1 type object. Replace the last 4 lines with the following:
((Scoreboard)getWorld().getObjects(Scoreboard.class).get(0)).setScore(score);
The breakdown of this line is:
World world = getWorld();
java.util.List<Scoreboard> objects = world.getObjects(Scoreboard.class);
Object obj = objects..get(0);
Scoreboard scoreboard = (Scoreboard)obj;
scoreboard.setScore(score);
You need to login to post a reply.