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

2012/4/16

Hello all I will stop the game in like 60 seconds but How can I do that

1
2
martijn13039 martijn13039

2012/4/16

#
I now it is possible to say if actor eat let say 10 snakes greenfoot.stop but is it possible to stop the game in 60 seconds I need really help because I have no idea
Busch2207 Busch2207

2012/4/16

#
You've got a method, that is called System.currentTimeMillis() which returns the milli seconds since a date (I don't know from which, but I think something around the 1980th) in a long Number... If you save the number at the begin of your simulation, then you can check how much time has elapsed.
martijn13039 martijn13039

2012/4/16

#
if I put this in the game the game don´t stop in a certain time
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class tijd extends Actor
{
  
    public void act() 
    {
       System.currentTimeMillis(); 
    }    
}
SPower SPower

2012/4/16

#
Of course: you didn't write the code to let the scenario stop. The computer can't guess what you want to do: you have to write it down!
martijn13039 martijn13039

2012/4/16

#
sorry for my maby stupid questions but wil greenfoot stop now and than go to the hiscore?
 import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)  public class tijd extends Actor  {     public void act()       {     System.currentTimeMillis();       }      }  

gameover();
martijn13039 martijn13039

2012/4/16

#
gameover();
davmac davmac

2012/4/16

#
You want something to happen given a certain condition. That is, you want the game to finish if 60 seconds have elapsed. So you need to use an if statement. You need to figure out a way to express the condition '60 seconds have elapsed' in Java. System.currentTimeMillis() will tell you the time elapsed since some point, but not necessarily when the scenario started; think about you can measure the elapsed time.
martijn13039 martijn13039

2012/4/16

#
Sir davmac I hope you can help me with this problem if I will compile there is a problem there is an red squir on currentrimemillis
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class tijd extends Actor
{
  
    public void act() 
    {
       if (System.currentTimeMillis(1000)); 
       gameOver();
    }    
}
Michionlion Michionlion

2012/4/16

#
I think that you might want to find some documentation on what 'System.currentTimeMillis()' actually does. One thing, it does not return a boolean value (true or false) that is needed in an if statement. That is why it does not work. System.currentTimeMillis() returns a number of type 'long' that is the time from a date in history (that is not important, it is the difference between times--stay with me). If you save that number (using a 'long' type variable) at the beginning of the scenario, in your world class, you can then find out if so many milliseconds have elapsed by seeing if the number that you stored is a value smaller then the current one. that value will be the time in milliseconds between the two times. Example:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Example
 * 
 * @author Michionlion
 * @version 4/16/2012
 */
public class myWorld extends World {

private long time;

    /**
     * Constructor for objects of class myWorld.
     * 
     */
    public myWorld() {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1);
        time = System.currentTimeMillis();  // current time elapsed from January 1, 1970 in milliseconds
    }
    
    public void act() {
        
        if (time < time + 200) {
            // call gameover method, and stop greenfoot
            Greenfoot.stop();
        }
    }
}
I think that you may want to review some of the key concepts of programming, like how method calls work, what returns are, and how general logic works. BTW, the code I wrote above won't work if you simply put it in your game, you need to do some work, like customizing the time.
martijn13039 martijn13039

2012/4/16

#
Michionlion thank you for your help but there stil is something wrong because if I compile this there stay super an red squar plz help me futher
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


public class tijd extends Actor
{
    
   private long time;
   
       
    
  public tijd() 
  {
       super(700, 600, 1);          
       time = System.currentTimeMillis();
   }
   
   public void act()  
     {
         if (time < time + 1000)  
       Greenfoot.stop(); 
    }
}
danpost danpost

2012/4/16

#
Having the time and time check in an Actor class will cause the timer to start when the first Actor of that type is created. If that is what you want, then fine. Be aware, however, that unless you immediately add that actor to the world, the timing will be off. The other thing is that the if statement needs to be fixed (at least the condition within it). Obviously, 'time' will always be less than 'time + 1000', so think about what condition you actually want (you already said you wanted the game to stop after 60 seconds has elapsed). How do you find the elapsed time? (you already know how to get the 'current' time).
Michionlion Michionlion

2012/4/16

#
danpost wrote...
Having the time and time check in an Actor class will cause the timer to start when the first Actor of that type is created. If that is what you want, then fine. Be aware, however, that unless you immediately add that actor to the world, the timing will be off. The other thing is that the if statement needs to be fixed (at least the condition within it). Obviously, 'time' will always be less than 'time + 1000', so think about what condition you actually want (you already said you wanted the game to stop after 60 seconds has elapsed). How do you find the elapsed time? (you already know how to get the 'current' time).
oops... that was my mistake, the time variable. also, its red, because i said that that code can only be used in the world class, making the problems of instantiating it disappear. the call to 'super' creates a world, so obviously you can't have that in a class that extends 'Actor'.
martijn13039 martijn13039

2012/4/17

#
Danpost It is not really what I want but I don´t no how I can do it an because it is very difficult for me How can I fixed the if statement what need I to change with the code I hope you want to help me thx martijn
martijn13039 martijn13039

2012/4/17

#
and I really try it by my self and look in the greenfoot book and on internet but nothing work so I hope you are able to help me
Duta Duta

2012/4/17

#
I think you've struggled enough. Here's the code you want:
import greenfoot.*;
public class TimeChecker extends Actor {
	long initialTime;
	public TimeChecker() {
		initialTime = System.currentTimeMillis();
	}
	
	public void act() {
		if(System.currentTimeMillis() > initialTime + 60000) {
			//Put your code here that you want to happen when the game ends
			Greenfoot.stop();
		}
	}
}
Before just copy/pasting this, look over it and try to understand why it works.
There are more replies on the next page.
1
2