Results 1 to 11 of 11
- 01-10-2013, 09:41 PM #1
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Java/Eclipse is reading my constructor commands in wrong order
Hello people,
I am new to this forum so, I hope im gonna have some fun here, learn and maybe I can help others ;)
I had a problem with my strategy game im working on...
And than I thought about something, I have worked with other (easier) programming languages and I loved to be on a forum for fun, to help others and to get help, and I typed in google "Java Forum" and registered at the first one coming up ;)
So ehm, lets explain my problem now..
So, like you have read, I am making a strategy game but I walked into a problem...
I am declaring a class in the Constructor of my JPanel and after the declaration of that class, called createLevel, I declare some instances from the class Slave...
And there the problem is, It first declares the Slaves and after that the createLevel class, and that gives some problems, because I am using some information from the createLevel class in the slave instances.
Here the broncode:
ThanksJava Code:new createLevel(); // give blocks the good ID's and stuff // player get 3 slaves on start WHY DOES THIS STUPID PROGRAM/LANGUAGE declare THIS BEFORE declaring THE CREATELEVEL??????????????????????????? slave[0] = new Slave(baseXY[0][0], baseXY[0][1], true); slave[1] = new Slave(baseXY[0][0], baseXY[0][1], true); slave[2] = new Slave(baseXY[0][0], baseXY[0][1], true); System.out.println(baseXY[0][0]); for(int i = 3; i < slave.length; i++){ slave[i] = new Slave(-1, -1, false); }
- 01-10-2013, 10:15 PM #2
Re: Java/Eclipse is reading my constructor commands in wrong order
Class names should start with an uppercase letter -> Code Conventions for the Java Programming Language: Contents
Somebody else here may have a better crystal ball, but I haven't a clue where your problem might lie (or even exactly what problem you have).And there the problem is, It first declares the Slaves and after that the createLevel class, and that gives some problems, because I am using some information from the createLevel class in the slave instances.
To get better help sooner, post a SSCCE (Short, Self Contained, Correct (Compilable), Example) that demonstrates the problem.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
- 01-10-2013, 11:53 PM #3
Re: Java/Eclipse is reading my constructor commands in wrong order
How do you know the order the code is executed?It first declares the Slaves and after that the createLevel class,If you don't understand my response, don't ignore it, ask a question.
- 01-11-2013, 09:22 AM #4
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Java/Eclipse is reading my constructor commands in wrong order
I guess the problem is that you do not know the concept of object orientation that is the difference between class, object and method and declaration and reference.
I can also only guess what your problem might be - and if I am right this advice will helkp you: Do less things in the constructor and more in dedicated methods - the constructor can be tricky as it constructs the object and e.g. constructing others during that process that may reference back this one creates errors. Use a straight way of creating, initializing and then using your objects.
If you want more specific help I think you have to post the full class or at least method because nobody can work with just a code fragment.I like likes!.gif)
- 01-11-2013, 06:48 PM #5
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Re: Java/Eclipse is reading my constructor commands in wrong order
Thanks, I have never known that!Class names should start with an uppercase letter
I was afraid that, the baseXY variable didn't change at all so I printed everywhere (with System.out.println("Class blabla " + baseXY)) to actually see where it was going wrong, but what I noticed is that the Slave class prints got printed out BEFORE the CreateLevel class prints (I have changed it to an uppercase letter now ;)How do you know the order the code is executed?
Yeah, you are true, but I have just placed it in the constructor so you can see it in like, 10 seconds, and not have to watch through my define class where im initializing hundreds of images and bad stuff ;)Do less things in the constructor and more in dedicated methods - the constructor can be tricky as it constructs the object and e.g. constructing others during that process that may reference back this one creates errors. Use a straight way of creating, initializing and then using your objects.
There is nothing to show more :S, in the define method the only thing that is happening is initializing of some images...Java Code:public class GamePanel extends JPanel implements Runnable { public GamePanel(){ define(); //set all baseXY to -1, means unused for(int x = 0; x<baseXY.length; x++){ baseXY[x][0] = -1; } new CreateLevel(); // give blocks the good ID's and stuff // player get 3 slaves on start WHY DOES THIS STUPID PROGRAM/LANGUAGE initialize THIS BEFORE initializing THE CREATELEVEL??????????????????????????? slave[0] = new Slave(baseXY[0][0], baseXY[0][1], true); slave[1] = new Slave(baseXY[0][0], baseXY[0][1], true); slave[2] = new Slave(baseXY[0][0], baseXY[0][1], true); for(int i = 3; i < slave.length; i++){ slave[i] = new Slave(-1, -1, false); } } public void run(){ while(true){ try{ repaint(); Thread.sleep(5); }catch(Exception e){ System.out.println("run error!"); } } } }
But if its helpful to post the whole project I will do that, but I dont know why that should be helpful, its just initializing in the wrong order...
- 01-11-2013, 06:55 PM #6
Re: Java/Eclipse is reading my constructor commands in wrong order
How can the posted code be compiled and executed for testing to show the problem?
If you don't understand my response, don't ignore it, ask a question.
- 01-11-2013, 07:13 PM #7
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Re: Java/Eclipse is reading my constructor commands in wrong order
Aha, okey then,
Here the full project is:
Strategy.rar
Thanks for the fast reply
- 01-11-2013, 07:39 PM #8
Re: Java/Eclipse is reading my constructor commands in wrong order
I'm not looking for the full project that has to be downloaded from a foreign site.
I am looking for a small, complete program that compiles, executes and shows the problem.If you don't understand my response, don't ignore it, ask a question.
- 01-11-2013, 07:50 PM #9
AN21XX
- Join Date
- Mar 2012
- Location
- Munich
- Posts
- 297
- Rep Power
- 2
Re: Java/Eclipse is reading my constructor commands in wrong order
It is 99.9% NOT doing what you say here...its just initializing in the wrong order...
In the files you provided I do not see any printouts to reproduce the error... sorry. But you know that if you get an Exception, all of your code there will be skipped and you return, right?
That is most probably what happens...
EDIT: Wait, I just saw something... you don't dare, do you? You wonder why your baseXY does not change? Because you do never change it anywhere! Show me where it should be changed in CreateLevel()...?Last edited by Sierra; 01-11-2013 at 08:01 PM.
I like likes!.gif)
- 01-11-2013, 08:13 PM #10
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Re: Java/Eclipse is reading my constructor commands in wrong order
- 01-11-2013, 08:16 PM #11
Member
- Join Date
- Jan 2013
- Location
- FailLand
- Posts
- 6
- Rep Power
- 0
Re: Java/Eclipse is reading my constructor commands in wrong order
Wait, I have downloaded my own files and now it's working fine... Thanks for all the help, if I have next time a problem like this Im gonna upload it to the internet and download it for myself lolz.
To be honest... :S hard to say but I failed... putting a print in the paint method instead of in the constructor... Gonna make new account where I have a better prestation without BIG fails ^^Last edited by Herjan; 01-11-2013 at 08:23 PM.
Similar Threads
-
reading commands from console
By merik in forum New To JavaReplies: 9Last Post: 11-17-2010, 04:09 PM -
What can cause of java program to suddenly stop reading commands?
By ryuzog in forum New To JavaReplies: 25Last Post: 11-04-2010, 04:26 PM -
Reading random order
By khh717 in forum New To JavaReplies: 6Last Post: 05-13-2010, 10:24 PM -
Wrong Order in Program Run
By HeatR216 in forum New To JavaReplies: 0Last Post: 03-25-2010, 08:41 PM -
CMD is not reading commands
By colonial in forum Forum LobbyReplies: 1Last Post: 03-15-2010, 02:36 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks