Results 1 to 5 of 5
  1. #1
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default 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:
    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();
    			}
    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:
    public static void secondMain()
            {
                String[] args = null;
                IntroduceStory intro = new IntroduceStory();
                intro.main(args);      
            }
    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.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,421
    Rep Power
    25

    Default 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.

  3. #3
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default 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.

  4. #4
    k1ng is offline Member
    Join Date
    Apr 2012
    Posts
    59
    Rep Power
    0

    Default Re: Calling another class after one is done running

    The general practice in games is to have a "main loop", i.e.

    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
       }
     }
    then in outro you can do whatever needs to be done based on whether you won or lost, as represented by 'didWin'
    Last edited by k1ng; 06-15-2012 at 09:54 AM. Reason: forgot the main() class

  5. #5
    Burnett98 is offline Member
    Join Date
    Jun 2012
    Posts
    13
    Rep Power
    0

    Default 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

  1. Replies: 8
    Last Post: 04-18-2012, 04:58 PM
  2. Replies: 3
    Last Post: 01-26-2012, 11:20 PM
  3. Replies: 10
    Last Post: 09-17-2010, 04:04 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. calling a C routine from JAVA running in AIX
    By jcosta in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 07:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •