Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2012, 09:39 AM
lisa92 lisa92 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2011
Posts: 43 lisa92 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 31 m 45 sec
Reputation Power: 2
How to include Thread in while loop ?

I need to make the object change modes (movements) depending on the seconds elapsed. I created a while loop with while(object alive) ...
The thing is it loops way faster than seconds and goes into infinte loop. Someone suggested to include a Thread but I never worked with threads..some help or guidance is appreciated

Code:

	private int seconds=0;
	private boolean ghostalive=true;
	
	protected static final int chaseMode = 0;
	protected static final int scatterMode = 1;
	protected static final int frightenedMode = 2;
	
	static int mode = scatterMode; //initially ghost start in scatterMode
 
	public Ghost(int x, int y, Maze maze){
		super(x, y, maze);
		futureDirection = 0;
		timer = 0;
		updateMode();
		//chaseMode = false; 
		//frightenedMode = false;
		
	}     
	
	public static int getMode(){
		return mode;
	}

	//LEVEL 1
	//scatter for 7s 
	//chase for 20s 
	//scatter for 7s 
	//chase for 20s 
	//scatter for 5s 
	//chase for 20s
	//scatter for 5s 
	//chase indefinite

	public void updateMode(){
		
		
	while(ghostalive){
		
		if(seconds<7){
			mode = scatterMode;
		}
		if(7<seconds && seconds<27){
			mode = chaseMode;
		}
		if(27<seconds && seconds<34){
			mode = scatterMode;
		}
		if(34<seconds && seconds<54) {
			mode = chaseMode;
		}
		if(54<seconds && seconds>59) {
			mode = scatterMode;
		}
		if(59< seconds && seconds<79){
			mode = chaseMode;
		}
		if(seconds>84){
			mode = scatterMode;
		}
		seconds++;
		
		}	
	}

Reply With Quote
  #2  
Old November 15th, 2012, 10:29 AM
NormR's Avatar
NormR NormR is offline
Contributing User
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Aug 2010
Location: SW Missouri
Posts: 2,956 NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level)NormR User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 2 h 56 m 11 sec
Reputation Power: 345
Some comments on the code:
The chain of if tests inside the while look will all be executed. I assume that only one is supposed to be true at a time which means that none of the other if tests should be executed. If so, the if chain should be changed to if/else if.
Quote:
it loops way faster than seconds and goes into infinte loop

How does the while() loop exit? I don't see where the ghostalive value is changed.
It will hang the program and execute forever. How many times should the while loop go around? When should it stop?

Quote:
depending on the seconds elapsed.

You could use a Timer for that. Set the timer to call a method after the elapsed time. The method could then change the mode and start the timer for the next duration.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > How to include Thread in while loop ?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap