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 14th, 2012, 10:20 PM
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
While loop problem

Object is supposed to change mode (movement algorithm) depending on the time elapsed (switch btw chase or scatter). I created an while loop but the object moves only in one mode.

Code:
	//////
	private static int seconds=0;
	private static boolean ghostalive;
	
	protected static final int chaseMode = 0;
	protected static final int scatterMode = 1;
	protected static final int frightenedMode = 2;
	
	static int mode; //initially ghost start in scatterMode
 
	public Ghost(int x, int y, Maze maze){
		super(x, y, maze);
		futureDirection = 0;
		timer = 0;
		mode = getMode();
	}     
	
	public static int getMode(){
		mode=setMode();
		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 static int setMode(){
		
	while(ghostalive){
		
		mode = scatterMode;
		if(seconds>7)
			mode = chaseMode;//chaseMode=true;
		if(seconds>27)
			mode = scatterMode;
		if(seconds>34)
			mode = chaseMode;
		if(seconds>54) 
			mode = scatterMode;
		if(seconds>59) 
			mode = chaseMode;
		if(seconds>79)
			mode = scatterMode;
		if(seconds>84)
			mode = chaseMode;
		
		seconds++;
		
	}	return mode;
	}

Reply With Quote
  #2  
Old November 14th, 2012, 11:04 PM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,875 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 4 h 14 m 32 sec
Reputation Power: 813
Hi,

when you say that some objects move I guess there's code in a separate thread? Otherwise this wouldn't make sense at all.

Your mistake is that you assume the "seconds" counter actually determines the number of seconds. But Java will process a loop iteration in an instance, so the whole thing is over before you even notice it. You'll need something like Thread.sleep() to actually wait a second.

And you should follow naming conventions. A "set" or "get" methods are supposed to do nothing but "set" and "get" a variable (like the name suggests). Any other logic should be named differently.

Reply With Quote
  #3  
Old November 15th, 2012, 04:11 AM
Aurum84 Aurum84 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 74 Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level)Aurum84 User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 23 h 5 m 49 sec
Reputation Power: 17
Jacques1 is right here. Even though you named a variable 'seconds', does not imply that it actually *is* a second.
I ran your code, and the Ghost perfectly outputs the behaviour you expected in the command-line.
The issue you have here is that the code is executed in a split-second, also because it all is calculated in the constructor, resulting in that you only perceive the last mode of the Ghost. To resolve this: make the Ghost a Thread

There are some other issues, besides Jacques1 mentioned:
  • A get/set method should do only just that. put the Ghost logic in another method, for example: updateMode.
  • Your current setMethod references and updates the variable 'mode' and returns it. It does not need to return it, since it is a variable accessible by the entire instance of Ghost. Therefore the code-part
    Code:
    mode=setMode();
    can be changed to
    Code:
    setMode() //or: updateMode()
  • Change the large if-statement to an else-if construction, because only 1 clause can be true.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > While loop problem

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