Thanks davmac, the code worked when I tested it on my actor. I tried to use it for something else but it came up with an error. Essentially, what I want to happen is that there is a world change (to the battle world) when the player hits another actor. Answering a question correctly would then lead to a AnswerCorrectWorld which is basically a world with the words 'correct, please press enter to continue' in it. Pressing enter would take take them back to the world and position they were in before they hit the actor.
The actor that is getting transported has this code:
As you can see the mouse actor is transported to a different world and the game loads the Battle world. I did this because the mouse actor has methods inside it that I don't want activating in the battle world. So I sent the mouse to another world where I can store it temporarily in a sense. However, when the game finally gets to the AnswerCorrectWorld, the mouse isn't there?
Just in case it was hidden beneath the text that's in the world, I carried on with the code and implemented it into the 'PressEnterToContinue' class where upon pressing the enter key, the user is transported back to the first world.
public void CheckCollision() { Actor snake = getOneObjectAtOffset(0, 0, Snake.class); if (snake != null) { int battle = 1; World nextWorld = new AnswerCorrectWorld(); Actor mouse = (Actor)getWorld().getObjects(Mouse.class).get(0); nextWorld.addObject(mouse, 50, 50); Greenfoot.setWorld(new BattleFight());
public class PressEnterToContinueCorrectAnswer extends PressEnterToContinue { /** * Act - do whatever the PressEnterToContinueCorrectAnswer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setImage(new GreenfootImage("Press Enter to Continue", 35, Color.WHITE, Color.BLACK)); World World = getWorld(); if ("enter".equals(Greenfoot.getKey())) { World nextWorld = new GameWorld(); Actor mouse = (Actor)getWorld().getObjects(Mouse.class).get(0); nextWorld.addObject(mouse, 391, 209); Greenfoot.setWorld(nextWorld); } } }