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

2015/12/23

Changing worlds in a series layout

jalaw02 jalaw02

2015/12/23

#
Hi. I'm working on a game which requires the player to click on a moving dot within a certain time period. There are multiple levels, and for each level, another dot appears that you must click. To progress, I want it to be that you click all the dots, but once it's been clicked, it disappears. Basically, I need to know the code which gives the game the intelligence to only change levels once both the dots have been clicked. This is my code so far:
private int timer = 5;
    public void act()
    {
         move();
         click();
         
         if (timer > 0)
            {
                timer --;
                if (timer == 0)
            
                    {
                        timer = 5;
                        getWorld().addObject(new dotA2(), 127, 286);
                        getWorld().removeObject(this);
                    }
             }
    }    
    public void move()
    {
        setLocation(Greenfoot.getRandomNumber(600), getY());
        setLocation(getX(), getY() + Greenfoot.getRandomNumber(400));
        setLocation(getX(), getY() - Greenfoot.getRandomNumber(400));
        Greenfoot.delay(60);
    }
    public void click()
    {
        if (Greenfoot.mouseClicked(this))
        {   
            getWorld().removeObject(this);
            Greenfoot.setWorld((new dotWorld3()));
        }
    }
(Thanks to danpost for the help! (Click here to see)) The current class (dotA) is green and after a certain time, it changes to another class (dotA2), and then an A3. The code for the transition can be seen in the method click(). Any ideas would be much appreciated. Thanks.
danpost danpost

2015/12/24

#
By placing the transition in the class of the dot, any dot clicked on will cause the transition. But, that is not what you say you wanted. You said you want that when no dots remain in the world, for the transition to take place -- "no dots in world" would be the condition and should be coded in your world class act method. If you have no other actors but dots, then the following should be sufficient as the condition:
if (getObjects(null).isEmpty())
which basically asks if the world is totally empty of actors. If you have other actors other than dots, then:
boolean noDots =  getObjects(DotA.class).isEmpty();
noDots = noDots && getObject(DotA2.class).isEmpty();
// etc.
if (noDots) Greenfoot.setWorld(new dotWorld3());
jalaw02 jalaw02

2016/1/5

#
Hello! Thank you for that help, and sorry about the delay (Christmas and New Year!). I have added this to the world's method, but it won't actually make the transition to the next world! Here's the code:
public void newWorld()
    {
         if (getObjects(null).isEmpty())
         {
             Greenfoot.setWorld(new dotWorld3());
         }
    }
I've made it its own public void, just to keep things tidy. If it helps, here is the code for the removal of the dot once it's been clicked:
    public void click()
    {
        if (Greenfoot.mouseClicked(this))
        {   
            getWorld().removeObject(this);
        }
    }
Super_Hippo Super_Hippo

2016/1/5

#
Do you call 'newWorld' from the world's act method?
jalaw02 jalaw02

2016/1/6

#
Yes. There are the two methods "prepare" and "newWorld" within the world's act method:
public dotWorld2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        newWorld();
    }
danpost danpost

2016/1/6

#
jalaw02 wrote...
Yes. There are the two methods "prepare" and "newWorld" within the world's act method: < Code Omitted >
The code shown is not the 'act' method -- but, it is the constructor of the 'dotWorld2' class. The 'prepare' method calls is fine there -- but, the 'newWorld' method call needs to be moved to a 'public void act()' method. Add one if there is not yet one in the class.
jalaw02 jalaw02

2016/1/6

#
Thank you. Sorted that one out!
You need to login to post a reply.