Invoke the stop() method to stop an infinite loop

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 4957 Views - Last Post: 20 April 2009 - 01:54 AM Rate Topic: -----

#16 waz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-April 09

Re: Invoke the stop() method to stop an infinite loop

Posted 19 April 2009 - 03:21 PM

Hi,

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.



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();
	}
}



Was This Post Helpful? 0
  • +
  • -

#17 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Invoke the stop() method to stop an infinite loop

Posted 19 April 2009 - 04:21 PM

The problem is you don't call the gameOver() method anywhere. You need to call it yourself somewhere in your code.
Was This Post Helpful? 0
  • +
  • -

#18 waz   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 14-April 09

Re: Invoke the stop() method to stop an infinite loop

Posted 19 April 2009 - 08:38 PM

View Postcfoley, on 19 Apr, 2009 - 03:21 PM, said:

The problem is you don't call the gameOver() method anywhere. You need to call it yourself somewhere in your code.


That means: Define the method as follow
public void gameOver()
	{
	 this.running=false;
	 notifyListeners();
	 }


Then we call it as below
 
gameOver();



Where do we need to call it from?
I tried to do it as follow, but it did not stop the loop
 public void gameOver()
	{
	 this.running=false;
	 notifyListeners();
	 gameOver();
   
	}


This post has been edited by waz: 19 April 2009 - 08:45 PM

Was This Post Helpful? 0
  • +
  • -

#19 javalover   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 19-April 09

Re: Invoke the stop() method to stop an infinite loop

Posted 19 April 2009 - 11:34 PM

http://java.sun.com/...eprecation.html

look at this.. it really helps!! you will have to not use the stop() method as given in the class Thread. Look at the link. It explains a lot. Hope it helps!

cheers!
Was This Post Helpful? 0
  • +
  • -

#20 cfoley   User is offline

  • Cabbage
  • member icon

Reputation: 2425
  • View blog
  • Posts: 5,068
  • Joined: 11-December 07

Re: Invoke the stop() method to stop an infinite loop

Posted 20 April 2009 - 01:54 AM

Nice link javalover.

OK, this is the method definition:

public void gameOver()
    {
     this.running=false;
     notifyListeners();
     gameOver();
   
    }


You call it with a line this.gameOver();. Where that goes depends on when the game should end.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2