Quote
Why do I keep insisting on using
•Set the running attribute to false.
• Invoke the notifyListeners() method.
• Invoke the stop() method
Because, these are the provided instructions to create the gameOver() method.
After advising me that I do not need to invoke the stop() method because I'm changing the running attribute to false within the gameOver() method, which it will end the loop anyway. But the gameOver method could not be called!!!
Below is the Space class code.
•Set the running attribute to false.
• Invoke the notifyListeners() method.
• Invoke the stop() method
Because, these are the provided instructions to create the gameOver() method.
After advising me that I do not need to invoke the stop() method because I'm changing the running attribute to false within the gameOver() method, which it will end the loop anyway. But the gameOver method could not be called!!!
Below is the Space class code.
This is my Space class,
import java.util.*;
public class Space extends Thread
{
private double width;
private double height;
private PlayerShip playerShip;
private SpaceObject[] spaceObjects;
private boolean running;
//Constructor
public Space(double width,double height)
{
this.width=width;
this.height=height;
PlayerShip playerShip=new PlayerShip (this,40,80);
this.spaceObjects=new SpaceObject[10];
this.running=true;
}
public double getWidth()
{
return width;
}
public double getHeight()
{
return height;
}
public PlayerShip getPlayerShip ()
{
return playerShip;
}
public SpaceObject getSpaceObjects()
{
return spaceObjects[10];
}
public boolean isRunning()
{
return running;
}
public void step()
{
System.out.println("Yippee!");
}
public void run()
{
while (running==true)
{
step();
notifyListeners();
sleep(40);
}
notifyListeners();
}
public static void sleep(long ms)
{
try
{
Thread.sleep(ms);
}
catch (Exception e)
{
}
}
//The gameOver method
public void gameOver()
{
this.running=false;
notifyListeners();
}
private List listeners = new ArrayList();
public void addListener(Listener listener)
{
listeners.add(listener);
}
private void notifyListeners()
{
for (Iterator it = listeners.iterator(); it.hasNext(); )
((Listener)it.next()).spaceStepped();
}
public interface Listener
{
void spaceStepped();
}
}

New Topic/Question
Reply




MultiQuote



|