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

2019/4/16

HOW TO CONTINUE MY TIMER IN TO ANOTHER WORLD

joycepestin joycepestin

2019/4/16

#
can I ask if, how will I continue my timer into another world? for example, I have this character that walks through the room and collects things, and when he goes to the door, another world will pop-out. The problem is when he goes to another world, the time will stop, how can I maintain the time even if it is in another world? thanks
Nosson1459 Nosson1459

2019/4/16

#
it would help if you would post the relevant code. but offhand I think the best way to do that is make a new constructor for your world class that has a parameter for the timer. you then set the timer in the new world with using the variable inserted into the constructor from the old world. then your timer will continue from where it left off.
joycepestin joycepestin

2019/4/16

#
public babyWorld()
    {    
      
        time = 2100;
        timer = 0;
        prepare();
    }
    
    public void act()
    {
        if(time <= 0)
        {
            Greenfoot.setWorld(new gameOver());
        }
        timer--;
        time--;
        showmsg();
    }
    
    public void showmsg()
    {
        if(timer <= 0)
        {
            showTime();
        }
    }
    
    public void showTime()
    {
        
        showText("Time: " + time/100, 650, 50);
    }
here's my code
joycepestin joycepestin

2019/4/16

#
public class doorBaby1 extends Actor
{
    int collect;
    int time;
    /**
     * Act - do whatever the doorBaby1 wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        player p = (player) getOneObjectAtOffset(0,0, player.class);
        if (p!=null && collect != 8 && time != 0)
        {
            Greenfoot.setWorld(new babyWorld2());
        }
      
    }    
}
and here's my code for the door, whenever the actor touch it, it goes to another world my problem is how can I make the time still running even if it is in another world
joycepestin joycepestin

2019/4/16

#
Nosson1459 wrote...
it would help if you would post the relevant code. but offhand I think the best way to do that is make a new constructor for your world class that has a parameter for the timer. you then set the timer in the new world with using the variable inserted into the constructor from the old world. then your timer will continue from where it left off.
thank you so much
Nosson1459 Nosson1459

2019/4/22

#
if you can please include the entire class code so can follow your code and see whats going on. but based on your code I can suggest this. make another constructor in the world class that looks like this
public babyWorld2(int time) //this is a parameter that will allow you to pass the time variable into the world
    {    
       
        this.time = time; //this sets the worlds time variable to the one passed in the constuctors parameter
        timer = 0;
        prepare();
    }
and then make a getter method in the babyWorld class that looks like this:
public int getTime() //the public here is very important
{
    //this method is so you can access the worlds time variable without being able to modify it
    return time();
}
now in the doorBaby1 class remove the if statement and replace it with this:
if (p!=null && collect != 8 && time != 0)
        {
            babyWorld bbWorld=(babyWorld)getWorld();
            Greenfoot.setWorld(new babyWorld2(bbWorld.getTime()));//this calls the constructor in the babyWorld2 Class that sets the time variable and you use the getter method you made in the babyWorld class to set the timer to be what it was up to in the previous world
        }
Nosson1459 Nosson1459

2019/4/22

#
also you reference other world classes and other things here please post all the classes and the entirety of them. I did my best based on the info you gave but the code I gave you very likely wont work because there are parts of your code that I cant see
You need to login to post a reply.