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

2012/3/19

Timer that continues through different worlds

freddekl freddekl

2012/3/19

#
So I am new to Greenfoot and I am making a maze that goes through 7 Levels, I have created a times as a subclass of actor, but it only times for one world, then resets in the next one. How do I create a timer that continues to time through all the levels? Thanks
davmac davmac

2012/3/20

#
Just move the existing timer into the new world, when you create the new world.
freddekl freddekl

2012/3/20

#
I don't think you understand, when I go to the next world, the timer resets, how do I make it so it doesnt reset and counts through all the levels
davmac davmac

2012/3/20

#
Well, I guess I don't understand, because if you use the same timer it wouldn't reset unless you tell it to. It sounds to me like your new world is making a new timer, rather than using the timer from the other world. If you posted your code it would help!
freddekl freddekl

2012/3/20

#
So i made the timer a subclass of actor. Here is the code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class timer  extends Actor
{
    long start_time, time;
    boolean has_stored = false;
    public void act()
    {
        /**
         * Get the start time and store it when boolean has_stored is false.
         */
        if(has_stored == false)
        {
            start_time = System.currentTimeMillis();
            has_stored = true;
            System.out.println("0" + start_time);
        }
        /**
         * long time is equal to start_time less current time (Milliseconds) divided by 1000.
         */
        time = (System.currentTimeMillis() - start_time) / 1000;
        /**
         * Output the time since first executed.
         */
        System.out.println("1" + time + " seconds"); //Enable to output
        /**
         * Update the image of this timer. Enable to use image.
         */
        GreenfootImage timer_image = new GreenfootImage(135, 20);
          setImage(timer_image);
          timer_image.clear();
           timer_image.drawString("0" + time, 0, 10);
           
    }
}
davmac davmac

2012/3/20

#
Ok, how about the code that sets the new world, and that transfers the timer to the new world?
freddekl freddekl

2012/3/21

#
Here is the code for going to the next level I'm using the Cansee method from one of the instruction levels I don't understand how to transfer the timer to the new world
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public void nextLevel()
    {
        if (canSee(Food3.class))
        {
          Greenfoot.setWorld(new Level4());
          Greenfoot.playSound("nextLevel.mp3");
        }
        if (canSee(FoodIntro.class))
        {
          Greenfoot.playSound("Tetris.mp3");
          Greenfoot.setWorld(new Level1());
        }
        if (canSee(Food1.class))
        {
          Greenfoot.setWorld(new Levelinbetween());
          Greenfoot.playSound("nextLevel.mp3");
        }
davmac davmac

2012/3/21

#
Ok - I see you are creating a new world, but you are not removing the timer from the current world and adding it to the new world. Probably, you are creating a new timer in the constructor of each of the world classes - you shouldn't do that. After making that change, the above code can be modified to something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
if (canSee(Food3.class))
{
      // First pull the timer from the current world:
      Timer timer = (Timer) getWorld().getObjects(Timer.class).get(0);
      int tx = timer.getX();
      int ty = timer.getY();
      getWorld().removeObject(timer);
 
      // Now create the new world, and add the timer into it:
      World newLevel = new Level4();
      Greenfoot.setWorld(newLevel); 
      newLevel.addObject(timer, tx, ty);
 
      Greenfoot.playSound("nextLevel.mp3"); 
 }
etc.
freddekl freddekl

2012/3/27

#
Hey again, must say thanks for your help, but I have one final question. On the final level, I want the timer to stop and display the time as soon as the Actor reaches the world so I would have text in the background that says -------- Your Time: # (the # being the final time) How do i display a stopped final time as soon as it reaches the level?
You need to login to post a reply.