10 Replies - 10873 Views - Last Post: 15 April 2009 - 05:08 AM Rate Topic: -----

#1 waz  Icon User is offline

  • New D.I.C Head

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

Stop()Method

Posted 15 April 2009 - 03:15 AM

Hi All,

I've crated a step() method which will print out "Yippee!"
then I created a method run() which calls the step() method repeatedly in never ending loop.

Now, I'm trying to create a gameOver() methd which will stop the loop using the stop() method. However, it will not stop that loop.
Even my code will be compiled without error!!

Can someone advise me what is the problme?

//Method step which just prints out "Yippee!"
	
	public void step()
	{
		 System.out.println("Yippee!");
	}
	
	//Method run which call the step method with a loop.
	
	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()
	{
	 running=false;
	 notifyListeners();
	 stop();
		
			
	}
	   



Is This A Good Question/Topic? 0
  • +

Replies To: Stop()Method

#2 ashishshevale  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 92
  • Joined: 14-April 09

Re: Stop()Method

Posted 15 April 2009 - 03:28 AM

View Postwaz, on 15 Apr, 2009 - 02:15 AM, said:

Hi All,

I've crated a step() method which will print out "Yippee!"
then I created a method run() which calls the step() method repeatedly in never ending loop.

Now, I'm trying to create a gameOver() methd which will stop the loop using the stop() method. However, it will not stop that loop.
Even my code will be compiled without error!!

Can someone advise me what is the problme?

//Method step which just prints out "Yippee!"
	
	public void step()
	{
		 System.out.println("Yippee!");
	}
	
	//Method run which call the step method with a loop.
	
	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()
	{
	 running=false;
	 notifyListeners();
	 stop();
		
			
	}
	   


the first bug is your method name is step() and you are calling stop()
also you first call step() in ren() and then trying to put thread in sleep() instead use suspend and resume

Was This Post Helpful? 0
  • +
  • -

#3 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Stop()Method

Posted 15 April 2009 - 03:35 AM

Don't call stop(). Setting running to false will exit the loop and end the thread. What is notifyListeners()? A GUI method or something of your own?

If that doesn't fix it, let us know what errors you are getting.
Was This Post Helpful? 0
  • +
  • -

#4 waz  Icon User is offline

  • New D.I.C Head

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

Re: Stop()Method

Posted 15 April 2009 - 03:40 AM

[quote]

View Postashishshevale, on 15 Apr, 2009 - 02:28 AM, said:

View Postwaz, on 15 Apr, 2009 - 02:15 AM, said:

Hi All,

I've crated a step() method which will print out "Yippee!"
then I created a method run() which calls the step() method repeatedly in never ending loop.

Now, I'm trying to create a gameOver() methd which will stop the loop using the stop() method. However, it will not stop that loop.
Even my code will be compiled without error!!

Can someone advise me what is the problme?

//Method step which just prints out "Yippee!"
	
	public void step()
	{
		 System.out.println("Yippee!");
	}
	
	//Method run which call the step method with a loop.
	
	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()
	{
	 running=false;
	 notifyListeners();
	 stop();
		
			
	}
	   


the first bug is your method name is step() and you are calling stop()
also you first call step() in ren() and then trying to put thread in sleep() instead use suspend and resume



Quote

Thank you, I'm actully trying to crate the gameOver() method by following the three instuctions.
1. Set the running attribute to false.
2.Invoke the noditfyLsteners() method
3.Invoke the stop()method.
My problem with the stop()method. My complier will give me a warning Java 121

Was This Post Helpful? 0
  • +
  • -

#5 waz  Icon User is offline

  • New D.I.C Head

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

Re: Stop()Method

Posted 15 April 2009 - 03:49 AM

View Postcfoley, on 15 Apr, 2009 - 02:35 AM, said:

Don't call stop(). Setting running to false will exit the loop and end the thread. What is notifyListeners()? A GUI method or something of your own?

If that doesn't fix it, let us know what errors you are getting.


Quote

Thank you,

I'm using the nofityListeners() method to let the other guys who are listening like user interface, know when something happen in the game so that it has the opportunity to update the pixels on the screen.
My aim formth gameOver method () to stop the run () methodeven though the run method looks like it will run forever.
the complete code for this class is as follow:

import java.util.*;

public class Space extends Thread
{
	//Attribute "width" of type double.
	private double width;
	
	//Attribute "height" of type double.
	private double height;
	
	//Attribute "playerShip" of type Playership.
	private PlayerShip playerShip;
	
	//Attribute "spaceObjects" of type SpaceObject[]
	private SpaceObject[] spaceObjects;

	//Attribute "running" of type boolean.
	private boolean running;
	
	
	//Constructor
	
	public Space(double width,double height)
	{
	 // initialies the "width" attribute from the parameter
	 this.width=width;
	 
	 // initialies the "height" attribute from the parameter
	 this.height=height;
	
	 //initialise the "playerShip" object with a new playerShip object using the PlayerShip constructor.
	 
	 //PlayerShip myShip = new PlayerShip( new Space( 40, 40 ), 20, 20 );
	 PlayerShip playerShip=new PlayerShip (this,40,80);
	 
	 
	 //Initialise the "spaceObjects" attribute with a new array to store 10 space objects
	 
	 this.spaceObjects=new SpaceObject[10];
	 
	 //Initialise the "running" attribute to true
	 
	 this.running=true;
	 }
	 
	 //Method getWidth which returns attribute "width"
	 
	public double getWidth()
	 {
		 return width;
	 }
	 
	 
	 //Method getHeight which returns attribute "height"
	 
	public double getHeight()
	 {
		 return height;
	 }
	 
	 //Method getPlayerShip which returns attribute "playerShip"
	 
	public PlayerShip getPlayerShip ()
	{
		return playerShip;
	}
	
	//Method getSpaceObjects which returns attribute "spaceObjects"
	
	public SpaceObject getSpaceObjects()
	{
		return spaceObjects[10];
	}
	
	
	//Method isRunning which returns attribute "running"
	
	public boolean isRunning()
	{
		return running;
	}
	
	//Method step which just prints out "Yippee!"
	
	public void step()
	{
		 System.out.println("Yippee!");
	}
	
	//Method run which call the step method with a loop.
	
	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()
	{
	 running=false;
	 notifyListeners();
	 stop();
		
			
	}
	   
	
	
	
	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
  • +
  • -

#6 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Stop()Method

Posted 15 April 2009 - 04:00 AM

//Attribute "width" of type double.
No shit sherlock! :P

You're getting a compiler warning because the method stop() is deprecated. You shouldn't use it because it is unsafe. Really all your gameOver method needs to be is this:

public void gameOver()
    {
     running=false;
    }


Once that is set to false the loop will exit on its own and the notifyListeners() method outside the loop will be called.

You don't need the stop method because the thread will finish this way.
Was This Post Helpful? 0
  • +
  • -

#7 waz  Icon User is offline

  • New D.I.C Head

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

Re: Stop()Method

Posted 15 April 2009 - 04:02 AM

View Postcfoley, on 15 Apr, 2009 - 02:35 AM, said:

Don't call stop(). Setting running to false will exit the loop and end the thread. What is notifyListeners()? A GUI method or something of your own?

If that doesn't fix it, let us know what errors you are getting.


I've amended the setting running to false. It complie fine without any error but It has not stop the loop.
 public void gameOver()
	{
	 this.running=false;
	}


Was This Post Helpful? 0
  • +
  • -

#8 waz  Icon User is offline

  • New D.I.C Head

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

Re: Stop()Method

Posted 15 April 2009 - 04:13 AM

View Postcfoley, on 15 Apr, 2009 - 03:00 AM, said:

//Attribute "width" of type double.
No shit sherlock! :P

You're getting a compiler warning because the method stop() is deprecated. You shouldn't use it because it is unsafe. Really all your gameOver method needs to be is this:

public void gameOver()
    {
     running=false;
    }


Once that is set to false the loop will exit on its own and the notifyListeners() method outside the loop will be called.

You don't need the stop method because the thread will finish this way.



Thank you again for all you help. I've just started doing Java coding about 8 weeks ago and I'm still getting use to the concept of Java.
Was This Post Helpful? 0
  • +
  • -

#9 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Stop()Method

Posted 15 April 2009 - 04:18 AM

I suspect the gameOver method is never getting called. Temporarily add a System.exit(); line to it as a diagnostic. That forces the program to quit so if it doesn't quit, the method is not being called.
Was This Post Helpful? 0
  • +
  • -

#10 waz  Icon User is offline

  • New D.I.C Head

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

Re: Stop()Method

Posted 15 April 2009 - 04:46 AM

View Postcfoley, on 15 Apr, 2009 - 03:18 AM, said:

I suspect the gameOver method is never getting called. Temporarily add a System.exit(); line to it as a diagnostic. That forces the program to quit so if it doesn't quit, the method is not being called.



System.exit(); will not execute. I will try to have look at it again.

Thank you again for the help.
Was This Post Helpful? 0
  • +
  • -

#11 cfoley  Icon User is offline

  • Cabbage
  • member icon

Reputation: 2069
  • View blog
  • Posts: 4,307
  • Joined: 11-December 07

Re: Stop()Method

Posted 15 April 2009 - 05:08 AM

No problem. Good luck finding the bug.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1