I am making this game, and it needs a couple timers, I am having trouble with the syntax. The game is just a little blue square (canvas()) hopping around the screen in an applet. You click on it and it changes color. I need a timer to make the game stop after 1 minute. And I have no idea where to start.Can anyone help with this????
I also have another problem, when I make it so that there are two creatures hopping around the screen, if I click on one, they both stop, as opposed to just one stopping, and the other moving.Anyone help with this?????
public class Creature extends Canvas { private Timer timer; protected Applet applet; private int hitCounter=0; private long pauseTime;
private final int x=(int)(Math.random()*480+1); private final int y=(int)(Math.random()*480+1);
private final int CANVAS_WIDTH = (int)(Math.random()*100+50); private final int CANVAS_HEIGHT = (int)(Math.random()*100+50); private Image img;
public Creature(Applet a)//Allows user to set the rate of Creature as well. { applet=a; int DELAY=(int)(Math.random()*1000+500);//Num=whatever you want it to be. timer=new Timer(DELAY,new CreatureActionListener()); setLocation(x,y); setSize (CANVAS_WIDTH, CANVAS_HEIGHT); addMouseListener(new CreatureMouseListener()); startCreature();
}
public void paint(Graphics screen) { screen.setColor(Color.blue); screen.fillRect(x,y,CANVAS_WIDTH, CANVAS_HEIGHT); }
//----------------------------------------------------------------------- //Sets up the MouseListener for the creature. //----------------------------------------------------------------------- private class CreatureMouseListener implements MouseListener { public void mousePressed (MouseEvent event) { if(timer.isRunning())//Checks to see that the timer is running. {
hitCount();//hitCounter++;(Counts the number of hits). applet.showStatus("You got me! "+getHits()+" times."); pauseTime=System.currentTimeMillis();
} move(); } } //Empty definitions for unused methods. public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseClicked (MouseEvent event) {} public void mouseReleased (MouseEvent event) {} }
//----------------------------------------------------------------------- //Sets up ActionListener for creature. //----------------------------------------------------------------------- private class CreatureActionListener implements ActionListener { public void actionPerformed (ActionEvent event) { //stopCreature();//timer.stop(); move(); } }
//----------------------------------------------- public void startCreature() { applet.showStatus("Catch me if you can!"); if(!(timer.isRunning()))//(timer.isRunning==false). timer.start(); }
public void stopCreature() { if(timer.isRunning()) timer.stop(); }
public void hitCount() { hitCounter++; }
public void move() { setBackground (Color.blue); stopCreature(); int newX=(int)(Math.random()*480+1); int newY=(int)(Math.random()*480+1); setLocation(newX,newY); repaint(); startCreature(); }
public class CreatureCatcher2 extends Applet { Creature PPP; Creature GGG; Vector creatureVector=new Vector(); private int creatureCounter=0; private int missCounter=0;
private Timer runtimer; private final int timer_interval=100; private long time_length=System.currentTimeMillis();
public void paint(Graphics screen) { screen.drawString("You have "+missCounter+" misses.",20,20);
}
private class AppletMouseListener implements MouseListener { public void mousePressed (MouseEvent event) { missCounter++;//(Counts the number of misses). repaint(); }
//Empty definitions for unused methods. public void mouseEntered (MouseEvent event) {} public void mouseExited (MouseEvent event) {} public void mouseClicked (MouseEvent event) {} public void mouseReleased(MouseEvent event) {} }
private class AppletActionListener implements ActionListener { public void actionPerformed (ActionEvent event) { creatureCounter++; } }