I have a retry button:
but using getWorld(); as the next world refference doesn't restart the world!
So, How do I make this button restart the world, by moving to another world (setWorld) which is the same as this button was in this world?
e.g. this button was in this test world:
in that world, 2 optional test wombats that showText when being clicked:
to get the the idea. if the world restarts, the text should be gone, until being clicked again.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class retryButton here. * * @author (your name) * @version (a version number or a date) */ public class retryButton extends LevelResults { /** * Act - do whatever the retryButton wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private World targetGo = getWorld(); public void act() { // Add your action code here. if(Greenfoot.mouseClicked(this)){ Greenfoot.setWorld(getWorld()); //restart current world //getWorld().removeObject(this); } } private World getWorldName(){ return getWorld(); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class testResult here. * * @author (your name) * @version (a version number or a date) */ public class testResult extends testAreas { /** * Constructor for objects of class testResult. * */ public testResult() { prepare(); } /** * Prepare the world for the start of the program. * That is: create the initial objects and add them to the world. */ private void prepare() { endLevelBar endlevelbar = new endLevelBar(); addObject(endlevelbar,300,376); menuButton menubutton = new menuButton(); addObject(menubutton,440,330); retryButton retrybutton = new retryButton(); addObject(retrybutton,480,330); nextButton nextbutton = new nextButton(); addObject(nextbutton,580,330); testWombat testwombat = new testWombat(); addObject(testwombat,124,192); testWombat testwombat2 = new testWombat(); addObject(testwombat2,129,268); } }
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) /** * Write a description of class testWombat here. * * @author (your name) * @version (a version number or a date) */ public class testWombat extends testObjects { /** * Act - do whatever the testWombat wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { // Add your action code here. if(Greenfoot.mouseClicked(this)){ getWorld().showText("Hello World!", getX(), getY()); } } }