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

2011/12/5

Actors doesn't show in Level2

1
2
raniye raniye

2011/12/7

#
None but it just doesn't show my Actor in level2...
davmac davmac

2011/12/7

#
I really can't see why it wouldn't work. Are you sure that it's just not appearing underneath another actor? If you still have a problem, can you upload your scenario here (to greenfoot.org) so that we can have a look.
raniye raniye

2011/12/7

#
you can download it here: http://www.hairos.be/greenfoot/GameSOFO.zip
davmac davmac

2011/12/7

#
The problem is in the "haai" class, in the checkNextLevel() method:
   public void checkNextLevel()
   {
       if(getX() == 57 && getY() == 30)
       {
           if(level == 1) {
               level = 2;
               Greenfoot.setWorld(new level2(this));
               getWorld().removeObject(this);
            }
            else {
                level = 1;
                Greenfoot.setWorld(new wereld(this));
                getWorld().removeObject(this);
            }
        }
    }
... you set the level2 world, after creating it by calling the second constructor - which adds the actor into the world - and then you immediately remove it from the world. You should move the line which says: getWorld().removeObject(this); ... above the line before it - so you remove the actor from the current world first.
kiarocks kiarocks

2011/12/7

#
That causes a Location problem.
davmac davmac

2011/12/7

#
That causes a Location problem.
No, it doesn't.
raniye raniye

2011/12/7

#
It works until I start walking around and collecting coins then it freezes again and gives this error in the Greenfoot: Terminal Window java.lang.ClassCastException: level2 cannot be cast to wereld at haai.punt(haai.java:196) at haai.collectCoin(haai.java:114) at haai.moveRight(haai.java:63) at haai.bewegen(haai.java:44) at haai.act(haai.java:33) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194)
raniye raniye

2011/12/7

#
oh and the score doesn't count up !
davmac davmac

2011/12/8

#
The exception tells you where the problem is - in this case, line 196 in your "haai" class. It's in this method:
    public void punt()
   {   
       wereld mySpaceWorld = (wereld) getWorld();  // <-- THIS LINE!
       Score addScore = mySpaceWorld.getScoreObject();
       addScore.addTenToScore();
      
   }
As you can see, you are casting the world to a "werld", but in level 2 it is actually a "level2" which is a different class. One way to solve this would be to have "level2" be a subclass of "werld" (there are other solutions too).
kiarocks kiarocks

2011/12/8

#
if you look at my version of this i use the "instanceof" to make sure im casting right.
raniye raniye

2011/12/8

#
Ok I tried to make my level2 a subclass but when I ran the game, it just jumped from subclass from 'wergeld' to subclass from 'World' again.. So it didn't make a difference... Is there another solution ?
kiarocks kiarocks

2011/12/8

#
Look at my scenario of the same thing.
davmac davmac

2011/12/8

#
Ok I tried to make my level2 a subclass but when I ran the game, it just jumped from subclass from 'wergeld' to subclass from 'World' again.. So it didn't make a difference...
Well, it works for me. Greenfoot won't magically change your code so if it moved in the diagram then you must have made it "extends World" again (instead of "extends wereld").
You need to login to post a reply.
1
2