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

2013/10/21

Call to one specific object

ddvink ddvink

2013/10/21

#
This code makes a character change to another world. In the other world is a portal which is calling the char back to the first world. If I use the portal overthere again, the char is not changing from world, but keeps coming up in the same world.
 public void checkForPortal(){
        Actor portal = getOneObjectAtOffset(10, 10, Portal.class);

        if(portal != null && Greenfoot.isKeyDown("up")){
            if(getWorld() instanceof TrainingGround){
                Greenfoot.setWorld(new Questmap());                       
            } 
            if(getWorld() instanceof Questmap){
                Greenfoot.setWorld(new TrainingGround());                       
            } 
        }
    }
What's wrong? And is there a way to interact with a specific object instead of any object of that class, because four portals needs four different world-transitions. Yours, Dennis
Gevater_Tod4711 Gevater_Tod4711

2013/10/21

#
Is Questmap a subclass of TrainingGround? Because if so getWorld() instanceof TrainingGround is true when getWorld is an instance of the class TrainingGround and also if getWorld is an instance of the class Questmap. That could be the problem. If you want to check in which world you have to change you could declare a variable (maybe int world) in your portal. You can check this variable through the reference (portal in this method) and then see in which world you have to change.
ddvink ddvink

2013/10/21

#
Hi, thanx for your reply, but no, Questmap is not a subclass of TrainingGround. But I'm gonna try the try the trick with the world-integer. yours, Dennis
danpost danpost

2013/10/21

#
Do all your worlds extend World or do you have an intermediate world superclass between them and World? The reason I asked is that if you had an intermediate world, you could add an 'id' field to that intermediate world superclass, set them uniquely in all the world subclasses and use it to distinguish between the different worlds.
ddvink ddvink

2013/10/21

#
It's one superclass (World) and three subclasses that extends world.
danpost danpost

2013/10/21

#
Does you char spawn at the portal in the new world when it is created? If so, possibly because you are using 'isKeyDown' instead of 'getKey' it is registering multiple times putting the char back in a new world of the type it just left.
danpost danpost

2013/10/21

#
To test last theory (if spawning at portal), add instance field 'private boolean active;' to the portal class and in its act method, for its first lines 'if (!active && !Greenfoot.isKeyDown("up")) active = true;' followed by 'if (!active) return;'.
ddvink ddvink

2013/10/21

#
That isKey and getKey will do the job! Thnx ;-)
You need to login to post a reply.