Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Restarting a thread

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Restarting a thread

    I'm working on a game right now. I want it to have different levels. Right now I have made level 1 and I have a little problem. If you fail to win level 1 I want to have a button to restart the level and if you do win level 1 there should be a button to continue to level 2 (there is some more to it, but that is the part I don't understand). I know how to make buttons and everything, but now once the game restarts level 1 or starts level 2 the threads don't start to run. This is my code.

    My Screen.java
     
    	//score
    	static int coinscore = 0;
    	static boolean awarded = false;
     
    //level
    	static int level = 0;
     
    	//object
    	static EntityBall ball = new EntityBall(200, 150);
     
    	//threads
    	static Thread ballthread = new Thread(ball);
    	static Thread paddle1 = new Thread(ball.paddle1);
    	static Thread paddle2 = new Thread(ball.paddle2);
     
    //random paint things, making images etc.
     
    //code that starts the levels once the button for it is clicked.
    public static void startLevel1() 
    	{
    		awarded = false;
     
    		ballthread.start();
    		paddle1.start();
    		paddle2.start();
     
    		ball.x = 200;
    		ball.y = 150;
     
    		ball.score1 = 0;
    		ball.score2 = 0;
     
    		if(ball.score1 >= 5 || ball.score2 >= 5)
    		{
    			level2explain = 1;
    		}
    	}
     
    	public static void restartLevel1() 
    	{
    		awarded = false;
     
    		ball.score1 = 0;
    		ball.score2 = 0;
     
    		ball.x = 200;
    		ball.y = 150;
     
    		if(ball.score1 >= 5 || ball.score2 >= 5)
    		{
    			level2explain = 1;
    		}
    	}
     
    	public static void startLevel2() 
    	{
    		awarded = false;
     
    		ball.score1 = 0;
    		ball.score2 = 0;
     
    		if(ball.score1 >= 5 || ball.score2 >= 5)
    		{
    			level3explain = 1;
    		}
    	}

    My ActionMouse

    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
     
    public class ActionMouse implements MouseListener 
    {
    	public void mouseClicked(MouseEvent event) 
    	{
    		int mousex = event.getX();
    		int mousey = event.getY();
     
    //starts the game
    		if(Screen.menuScreen == 4)
    		{
    			if (mousex > Screen.storyButton.x && mousex < Screen.storyButton.x + Screen.storyButton.width && 
    					mousey > Screen.storyButton.y && mousey < Screen.storyButton.y + Screen.storyButton.height)
    			{
    				Screen.menuScreen = 0;
    				Screen.level1explain = 1;
    				Screen.story = true;
    			}
    		}
     
    //starts first level
    		if(Screen.level1explain == 25 )
    		{
    			if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && 
    					mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height)
    			{
    				Screen.level = 1;
    				Screen.gameScreen = true;
    				Screen.level1explain = 0;
    				Screen.startLevel1();
    			}
    		}
    		//restarts level 1
    		if(Screen.level2explain == 3)
    		{
    			if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && 
    					mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height)
    			{
    				Screen.level = 1;
    				Screen.gameScreen = true;
    				Screen.level2explain = 0;
    				Screen.restartLevel1();
    			}
    		}
    		//starts level 2
    		if(Screen.level2explain == 11)
    		{
    			if (mousex > Screen.startButton.x && mousex < Screen.startButton.x + Screen.startButton.width && 
    					mousey > Screen.startButton.y && mousey < Screen.startButton.y + Screen.startButton.height)
    			{
    				Screen.level = 2;
    				Screen.gameScreen = true;
    				Screen.level2explain = 0;
    				Screen.startLevel2();
    			}
    		}
    	}
     
    	//other mouse things are empty
    }

    My ball class.
    import java.awt.*;
    import java.awt.image.ImageObserver;
    import java.util.Random;
     
    import javax.swing.ImageIcon;
     
    public class EntityBall implements Runnable, ImageObserver
    {
    	int x, y;
    	int xDir, yDir;
     
    	int score1, score2;
     
    	Image ballpic;
     
    	static Rectangle ball;
     
    	EntityPaddle paddle1 = new EntityPaddle(830, 250, 1);
    	EntityAIPaddle paddle2 = new EntityAIPaddle(50, 250, 1);
     
    	public EntityBall(int x, int y)
    	{
    //image
    		ImageIcon ballimg = new ImageIcon(this.getClass().getResource("/ball.png"));
    		ballpic = ballimg.getImage();
     
    		if(ballpic == null)
    		{
    			System.err.println("Image problems");
    		}
     
    //random start direction
    		Random rand = new Random();
     
    		this.x = x;
    		this.y = y;
     
    		int startX = rand.nextInt(2);
     
    		if (startX == 0)
    		{
    			this.setXDir(-2);
    		}
    		if (startX != 0)
    		{
    			this.setXDir(2);
    		}
     
    		int startY = rand.nextInt(2);
     
    		if (startY == 0)
    		{
    			this.setYDir(-2);
    		}
     
    		if (startY != 0)
    		{
    			this.setYDir(2);
    		}
     
    		ball = new Rectangle(this.x, this.y, 15, 15);
    	}
    	//direction
    	public void setXDir(int xDirection)
    	{
    		xDir = xDirection;
    	}
     
    	public void setYDir(int yDirection)
    	{
    		yDir = yDirection;
    	}
    	//graphics
    	public void paint(Graphics graphics)
    	{
    		graphics.setColor(Color.white);
    		graphics.drawImage(ballpic, ball.x, ball.y, this);
    	}
    public void move()
    	{
           //direction, collision and score handler
        }
     
    	public void run() 
    	{
    		try
    		{
    			while(Screen.running && Screen.level >= 1)
    			{
    				move();
    				Thread.sleep(8);
     
    				if(Screen.level >= 1)
    				{
    					scoreWatcher();
    				}
    			}
    		}
    		catch(Exception e)
    		{
    			System.err.println(e.getMessage());
    		}
    	}
     
    	private void scoreWatcher() 
    	{
    		if(Screen.level == 1)
    		{
    			if(score1 >= 5 && score2 < 5)
    			{
    				Screen.level2explain = 1;
    				Screen.level = 0;
    			}
     
    			if(score1 >= 5 && score2 == 0)
    			{
    				Screen.level2explain = 2;
    				Screen.level = 0;
    			}
     
    			if(score1 < 5 && score2 >= 5)
    			{
    				Screen.level2explain = 3;
    				Screen.level = 0;
    			}
    		}
     
    		if(Screen.level == 2)
    		{
    			if(score1 >= 5 && score2 < 5)
    			{
    				Screen.level3explain = 1;
    				Screen.level = 0;
    			}
     
    			if(score1 >= 5 && score2 == 0)
    			{
    				Screen.level3explain = 2;
    				Screen.level = 0;
    			}
     
    			if(score1 < 5 && score2 >= 5)
    			{
    				Screen.level3explain = 3;
    				Screen.level = 0;
    			}
    		}
    	}
    //required for image
    	public boolean imageUpdate(Image img, int infoflags, int x, int y, int width, int height) 
    	{
    		return false;
    	}
    }
    My paddle and AI paddle are mostly the same.

    So my problem is that when I restart level1 or start level 2 it doesn't restart the threads. How do I restart them?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Restarting a thread

    it doesn't restart the threads. How do I restart them?
    You can not restart a thread that has ended. You need to create a new thread.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    wuppy29 (June 20th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Restarting a thread

    Thanks. Do you also know how to reset the ball position? Right now when I do a second level the ball starts at the location it was when the game before ended.

  5. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Restarting a thread

    how to reset the ball position?
    Are you asking how to change the x,y location for the ball? Does the ball object have methods you can call to change its location?
    If you don't understand my answer, don't ignore it, ask a question.

  6. The Following User Says Thank You to Norm For This Useful Post:

    wuppy29 (June 20th, 2012)

  7. #5
    Junior Member
    Join Date
    Jun 2012
    Posts
    11
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: Restarting a thread

    You are great. You tell me what to do, but not exactly how to do it so I learn from it. Thanks again

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,166
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Restarting a thread

    Good luck.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 4
    Last Post: June 15th, 2012, 01:50 PM
  2. Tic Tac Toe = Naming Winning Player & Restarting Program
    By Override in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 28th, 2010, 12:20 AM
  3. Restarting at the beginning of an array
    By bonbon242 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 31st, 2010, 10:58 AM
  4. Save the information from an ArrayList after restarting the program
    By noFear in forum Java Theory & Questions
    Replies: 4
    Last Post: August 14th, 2010, 08:53 AM
  5. Java Question Need Help Restarting the Values of Variables from Random Numbers So Out
    By JavaStudent1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: July 24th, 2010, 02:25 PM