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

2018/6/1

How do I make the retry button that does move to the same new instance of world where the button was?

JOELwindows7 JOELwindows7

2018/6/1

#
I have a retry button:
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();
    }
}
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:
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);
    }
}
in that world, 2 optional test wombats that showText when being clicked:
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());
        }
    }    
}
to get the the idea. if the world restarts, the text should be gone, until being clicked again.
danpost danpost

2018/6/1

#
getWorld returns the currently active world (as is). If you want a world at its initial state, create a new one of that type. Maybe something like this:
import greenfoot.*;

public class retryButton extends LevelResults
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (getWorld() instanceof testResult) Greenfoot.setWorld(new testResult());
            else ; // continue in same manner for other possible type worlds
        }
    }
}
JOELwindows7 JOELwindows7

2018/6/1

#
danpost wrote...
getWorld returns the currently active world (as is). If you want a world at its initial state, create a new one of that type. Maybe something like this:
import greenfoot.*;

public class retryButton extends LevelResults
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this))
        {
            if (getWorld() instanceof testResult) Greenfoot.setWorld(new testResult());
            else ; // continue in same manner for other possible type worlds
        }
    }
}
thx for help. but trouble! There must be reusable. like a variable to set for going to other world. this way is inefficient. if I got many level, that mean it wil results: Whole n of level numbers of if-elses registrations
if (getWorld() instanceof testResult) 
    Greenfoot.setWorld(new testResult());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult3)
    Greenfoot.setWorld(new testResult3());
else if(getWorld instance of testResult4)
    Greenfoot.setWorld(new testResult4());
else if(getWorld instance of testResult5)
    Greenfoot.setWorld(new testResult5());
else if(getWorld instance of testResult6)
    Greenfoot.setWorld(new testResult6());
else if(getWorld instance of testResult7)
    Greenfoot.setWorld(new testResult7());
else if(getWorld instance of testResult8)
    Greenfoot.setWorld(new testResult8());
else if(getWorld instance of testResult9)
    Greenfoot.setWorld(new testResult9());
else if(getWorld instance of testResult10)
    Greenfoot.setWorld(new testResult10());
else if(getWorld instance of testResult11)
    Greenfoot.setWorld(new testResult11());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
else if(getWorld instance of testResult2)
    Greenfoot.setWorld(new testResult2());
// Ah I'm tired! but basically, testResultN, N++ every next else if.
Why would I have to tediously re-register every single level into that if-else registrations?
danpost danpost

2018/6/1

#
JOELwindows7 wrote...
Why would I have to tediously re-register every single level into that if-else registrations?
Well, whatever you do, it will have to be coded separately for each type world -- whether you use an if/if-else construct, a switch statement or inheritance (like how my Super Level Support Class Demo scenario controls the levels).
danpost danpost

2018/6/2

#
danpost wrote...
(like how my Super Level Support Class Demo scenario controls the levels)
Thank you @JOELwindows7, you were my long awaited 600th like on a scenario.
JOELwindows7 JOELwindows7

2018/6/4

#
danpost wrote...
JOELwindows7 wrote...
Why would I have to tediously re-register every single level into that if-else registrations?
Well, whatever you do, it will have to be coded separately for each type world -- whether you use an if/if-else construct, a switch statement or inheritance (like how my Super Level Support Class Demo scenario controls the levels).
Thx for info. What a shame that Greenfoot cannot simply create more than one instance (new thisWorld()), that the class is same as one is being active. I saw that Unity is much simpler tbh. Sorry, but this is the reality. You gotta have to check this video out. Not my video, but this is impressive! This Video link Basically, I tried the similar in Greenfoot. Like I said, it didn't work. Supposedly it goes jump out and in again.
danpost danpost

2018/6/4

#
Well, there is one other way which is not the recommended way -- but should work. This way requires a "try-catch" structure:
if (Greenfoot.mouseClicked(this))
{
    World world = null;
    try { world = getWorld().getClass().newInstance(); } catch (Exception e) { }
    if (world != null) Greenfoot.setWorld(world);
}
JOELwindows7 JOELwindows7

2018/6/6

#
danpost wrote...
JOELwindows7 wrote...
Why would I have to tediously re-register every single level into that if-else registrations?
Well, whatever you do, it will have to be coded separately for each type world -- whether you use an if/if-else construct, a switch statement or inheritance (like how my Super Level Support Class Demo scenario controls the levels).
I have used some of the refferences. it works out with little tuning. thx! an optional coding result is coming soon stay tuned.
You need to login to post a reply.