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

2014/2/26

How would I get an actor to spawn at a certain location depending on which door it collided with?

stateoaks stateoaks

2014/2/26

#
I've run into a bit of trouble with my doors. The code for going to the next level is
1
2
3
4
if (getOneIntersectingObject( DoorToRoom.class) !=null
        
            Greenfoot.setWorld(new SmallRoom()); 
        }
However, I have another door to that room and I want the player to spawn at a different location if he uses the other door. I have tried using this code, but it doesn't seem to be working
1
2
3
4
5
if (getOneIntersectingObject( DoorToRoom.class) !=null
       
           Greenfoot.setWorld(new World2()); 
           setLocation(500,500);
       }
How would I go about doing this? Thanks
davmac davmac

2014/2/26

#
When you create the new world, how does your actor get into that world? Perhaps you are creating a new actor instead of re-using that one? It seems like the problem is either that: - you are setting the location of the actor in *this* world, before you are moving it to the new world - you are not transferring the actor to the new world, and instead are creating a new actor. So calling 'setLocation' on this actor has no effect; you need to call it on the new actor instead.
stateoaks stateoaks

2014/2/26

#
When the player goes to the next level, the actor is loaded by being placed in the world's 'Prepare' method. E.g.
1
2
Char xchar = new Char();
       addObject(xchar, 78, 399);
Would I have to put the code for colliding with the door in the new World class?
davmac davmac

2014/2/26

#
Would I have to put the code for colliding with the door in the new World class?
No, the collision happens in the original world. You must check for it there. I don't see how you could check for the collision in the new world, seeing as you would never create the new world without first detecting the collision? One way to resolve this is to not create and add the actor in the new world's 'prepare' method, and instead add the actor to the new world after you've created it, like:
1
2
3
World2 newWorld = new World2();
newWorld.addObject(new Char(), 500, 500);
Greenfoot.setWorld(newWorld);
stateoaks stateoaks

2014/2/26

#
That makes sense. Where would I put said code? I've tried putting it when the player collides with the door and it loads the new level, but the player is nowhere to be seen.
davmac davmac

2014/2/26

#
That makes sense. Where would I put said code? I've tried putting it when the player collides with the door and it loads the new level, but the player is nowhere to be seen.
That's the right place to put it. Maybe you could upload the scenario so I can have a look?
danpost danpost

2014/2/26

#
If you have a door at the location where you are placing the Char actor in the new world (in other words, if you have paired doors that lead back and forth between two locations, whether within the same world or not), you may want to download my Use of 'this' demo scenario and look at how I handled the pairing.
You need to login to post a reply.