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

2014/7/15

Winning before the timer ends?

miku622 miku622

2014/7/15

#
So I successfully created a timer for my game, and made it so that if the timer goes to 0, it goes to a "You Lose" page. My problem is how to get the program to recognize when the player wins, and go to the "You Win" page. The objective of my game is to go through a maze to get to a door. How do I make it so that if the player gets to the door before the time goes up, it will switch to the "You Win" page? Thanks! Here is the code i have for the timer: import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo) import java.awt.Color; public class Timer extends Actor { /** * Act - do whatever the Timer wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ private int time = 20; private int count = 65; public void act() { // Add your action code here. if(time == 0) { Greenfoot.setWorld(new YouLose()); return; } if(counter()) { time--; count = 65; } display(); } private boolean counter() { if(count > 0) { count--; } return count == 0; } private void display() { setImage(new GreenfootImage("Time Left: " + time, 30, Color.BLACK, Color.WHITE)); } public void setTime() { time = 20; } public boolean isTimeUp() { return time == 0; } }
danpost danpost

2014/7/15

#
Nice timer. But what does it or its code have to do with the player reaching the door. It will obviously still be running at that time (or the You Lose page would have shown beforehand); but, the timer is of no consequence ONCE the player reaches the door. Have the door or the player check for the other intersecting it and show the You Win page if so. My only concern, beyond that, is will what actions you take at that time prevent the timer from still running out and show the You Lose screen afterwards. If you use 'Greenfoot.stop()', there should be no problem. Otherwise, you may want to add a Boolean field to the Timer class to control its running state, so you can stop the timer when the player reaches the door.
You need to login to post a reply.