Results 1 to 5 of 5
- 06-15-2012, 01:54 AM #1
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Calling another class after one is done running
Ok, so I'm having a problem in calling another class after I have finished running one. What I am doing is making a game sort of thing, and in the very beginning, I have an introduction type thing that just makes a different part of the story on this jTextPanel. At one point in the introduction, a friend of yours asks you to duel, and you accept. When this happens it opens another class, battle, and passes it the stats for who you are battling, in this case your friend. After either you or him have been defeated, how can I make the battle class call another class which is the second part of the introduction? I do not want to just put a statement into the end of my battle class because this is something I will call every time I have a battle and I can't have it always returning to the intro. Is there some way I could pass to this battle class where it should redirect to once it is done, so it can still remain something that can always be called and not only called in one situation?
Here is the code for when the battle class is called:
Java Code:int response = JOptionPane.showConfirmDialog(null, "Would you like to accept his request for battle?"); Enemy friend = new Enemy(); friend.creation("your friend", "Slash", "Stab", "", 100, 10, 20, 0, 100, 1, 2, 100); if(response == JOptionPane.YES_OPTION) { setVisible(false); Battle battle = new Battle(); battle.main(friend); } if(response == JOptionPane.NO_OPTION) { JOptionPane.showMessageDialog(null, "You know I'm not going to take that. Lets go!"); setVisible(false); Battle battle = new Battle(); battle.main(friend); }
Here is the code for when you have finished battling:
Here is the code or when the class that introduces the first part of the story is called(the method is called secondMain because I had nothing better to call it and that is all it really is for, to stand as a continuation of the main method that can be called at any time):Java Code:if(Main.tempEnemy.health <= 0) { JOptionPane.showMessageDialog(null, "Congratulations! You have defeated " + Main.tempEnemy.name + "!\n"); Main.victory = true; JOptionPane.showMessageDialog(null, "You have just gained " + Main.tempEnemy.experienceGiven + " points of experience"); Main.hero.experience = Main.hero.experience + Main.tempEnemy.experienceGiven; if (Main.hero.experience >= 100) { Main.hero.experience = Main.hero.experience - 100; Main.hero.levelUp(); } dispose(); } if(Main.hero.health <= 0) { JOptionPane.showMessageDialog(null, "Too bad! You have been defeated by " + Main.tempEnemy.name + "!\n"); Main.victory = false; dispose(); }
Thank you for any help you are able to give. I have been stuck on this for some time now and I haven't been able to come up with anything.Java Code:public static void secondMain() { String[] args = null; IntroduceStory intro = new IntroduceStory(); intro.main(args); }
- 06-15-2012, 03:39 AM #2
Re: Calling another class after one is done running
I didn't follow your requirements. There must be a way you can define methods in the various classes so that you can call them in the order that you want them executed. I'd move the logic out of the main() method and only have it call a class's constructor.
If you don't understand my response, don't ignore it, ask a question.
- 06-15-2012, 05:01 AM #3
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Re: Calling another class after one is done running
I don't understand what you are saying. As for the requirements, Every time the user chooses an attack, it runs a method that chooses an attack for the opponent and then deals damage to you and your opponent. After all this it checks to see if either of you has zero health, in which case it closes down the battle class and it will do special things whether you won or lost. I can't call them all in the same method, because it will open everything else before you are finished with the last thing. If possible, how would I tell everything in the main method or some other method to wait until something else has closed.
- 06-15-2012, 09:52 AM #4
Member
- Join Date
- Apr 2012
- Posts
- 59
- Rep Power
- 0
Re: Calling another class after one is done running
The general practice in games is to have a "main loop", i.e.
then in outro you can do whatever needs to be done based on whether you won or lost, as represented by 'didWin'Java Code:private static void main(String[] args) { Enemy enemy = new Enemy("Dr Evil"); do{ introduction(enemy); boolean didWin = fight(enemy); outro(didWin); }while(gameCompleteConditionNotMet) } private boolean fight(Enemy enemy) { boolean notWon = true; boolean notLost = true; while(notWon && notLost) { //TODO: all the fight code } }Last edited by k1ng; 06-15-2012 at 09:54 AM. Reason: forgot the main() class
- 06-17-2012, 07:12 PM #5
Member
- Join Date
- Jun 2012
- Posts
- 13
- Rep Power
- 0
Re: Calling another class after one is done running
Sorry I forgot to reply to this. I ended up fixing the problem another way. I just had my introduction pass an integer to the battle class and based on the integer the battle class would send you somewhere else. Thanks for your help though. I suppose next time I will do it your way, as that seems so much easier.
Similar Threads
-
Calling overridden parent method of outter class from an inner class
By jlczuk in forum Advanced JavaReplies: 8Last Post: 04-18-2012, 04:58 PM -
calling a function of a specific type in a java class from another class.....
By safi532 in forum Advanced JavaReplies: 3Last Post: 01-26-2012, 11:20 PM -
Applet calling paint method twice = for loop running twice?
By enerj in forum Java AppletsReplies: 10Last Post: 09-17-2010, 04:04 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
calling a C routine from JAVA running in AIX
By jcosta in forum New To JavaReplies: 1Last Post: 01-13-2008, 07:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks