Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 1 of 3 123 LastLast
Results 1 to 25 of 61

Thread: Tower Defense on Java

  1. #1
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Question Tower Defense on Java

    I've always wanted to be able to create a tower defense game somehow, and now that I'm beginning to understand Java much more, I think soon I'd like to take a swing at it. However, my knowledge of Java is still quite limited...

    I'm pretty sure I could create Enemy and Tower objects to manage data there, but I guess one of my main confusions is how I could have multiple towers firing at multiple enemies, since I don't know of any way to make Java do more than one thing at once.

    Is there any class, API, etc. that someone could direct me to so that I can read up on it and perhaps get a little bit of a kick-start for creating this game?

    Any suggestions would be greatly appreciated! Thanks!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    way to make Java do more than one thing at once.
    Threads allow your code to do more that one thing at almost the same time.
    The code that executes on one thread can be doing something while the code that executes on a different thread can be doing something else. The JVM or OS decides which thread gets to execute and when.

  3. #3
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    @Norm

    So... Would each tower, enemy, and special skill have to have its own thread that would constantly update?

    Do you know any helpful examples for this (I know the documentation exists, but sometimes I learn a lot more quickly if I can see the code in action)?

    Thanks!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    Would each tower, enemy, and special skill have to have its own thread that would constantly update?
    Up to a limit. You do not want too many threads.
    You could use a Timer to start a thread that could quickly update the positons and status of many objects before calling repaint to have the paint method redraw the new postions etc.

    I see lots of students working on animation in the Forums. Do a Search here or Google.

  5. The Following User Says Thank You to Norm For This Useful Post:

    snowguy13 (December 4th, 2011)

  6. #5
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    Hmm... Okay. Well thank you for your help! I will ponder what you have suggested.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  7. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Tower Defense on Java

    Threading is certainly one way to go, but I'm not sure I'd recommend it to a beginner.

    Think about it this way: all of the stuff going on in a game is not happening at the same time, it's just happening so fast that it appears to be happening at the same time. For example, if you have two bullets going in different directions, you simply update the position of each and then call repaint.

    The basic flow of your program might be as follows:

    Use a single thread (I would use a Swing Timer) that loops through each of your game Objects, updates them, and then calls repaint. Repeat this multiple times a second (15, 30, or 60 times a second are some common framerates). Your program will appear to be doing multiple things at once, when in reality it's simply doing one thing at a time very fast.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. The Following User Says Thank You to KevinWorkman For This Useful Post:

    snowguy13 (December 5th, 2011)

  9. #7
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    @KevinWorkman

    Thanks for the help! I'll see if I can attempt that too. However, one thing concerns me: how can the Swing Time know what to update if there are a variable number of towers, bullets, enemies, etc.? I can't just tell it to update "everything" (unfortunately) ...

    Would an array work? I could make the update/repaint Swing Timer refer to an array, and have that array added to or subtracted from whenever a bullet, tower, or enemy is spawned or destroyed/sold... I'm venturing into totally unknown territory, but I guess this is the best way to learn, since there really isn't a definitive guide to tower defense game making.

    Anyways, thanks to both of you for your help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    how can the Swing Time know what to update if there are a variable number of towers, bullets, enemies, etc.? I can't just tell it to update "everything" (unfortunately)
    The Timer calls your code. Your code will have lists (in arrays or ArrayLists) of what is to be updated.

  11. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Tower Defense on Java

    What Norm said.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #10
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    Okay, another question...

    I want to create individual classes for each of the towers so that I can have them behave uniquely, and I can't decide what to do of these two options...
    1) Individually create each class
    2) Create a superclass, Tower, and base all specific tower types off of it

    *Note that example towers used below are hypothetical and are not the focus of my question
    I'm also taking into consideration the fact that each individual class needs to have static variables too; for example, the RangeTower may have a range boost because of an upgrade you choose, and that needs to be a static variable in the class that ALL instances of the RangeTower class take into account; however, the BombTower may also have a range boost that is independent of the RangeTower's range boost. My concern is that, if I create a superclass Tower, how can I make sure that each sub-tower class (BombTower, RangeTower, etc.) derived from it has its OWN set of static variables?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  13. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    each sub-tower class (BombTower, RangeTower, etc.) derived from it has its OWN set of static variables?
    It the variables have unique names, that should work. Have you tried writing a small set of classes to test what happens?

  14. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    Have you tried writing a small set of classes to test what happens?
    No, I haven't tried yet. In fact, all I've done so far is created a rough main menu and new game screen. I'm trying to do this in an order that won't prove disconcerting or confusing for me, but I can't stop myself from considering what challenges lie ahead.

    However, as I mull it over, it would make sense that if the subclass towers extend the superclass Tower, they should get all of its static variables (why wouldn't they?). But, as you said, I'll have to test it...once I get there.

    I seem to remember having another question...but I can't recall right now. I'll ask if I find it again.

    Thanks for your help!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  15. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    Sometimes taking a short break from design work to try a few simple techniques will free up/relax the brain.

  16. #14
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    Really? Okay, I'll have to try that. Of course, I'll have to totally switch my method of thinking to test the class idea!

    But again, thanks for your help!

    I still can't remember that other question... :/
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  17. #15
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    I remembered my other question! But, it has a length explanation before I get to it.

    In the main menu code, I had multiple JLabels formatted to look like menu buttons. So, I worked on a MouseListener to make them highlight when you mouse over them. I wasn't thrilled about writing four MouseListeners (one for each JLabel), so I tried to find a way to only write one. Luckily I did, using the MouseEvent's getComponent() method. I used this code:

    private class LabelListener implements MouseListener
    {
       public void mouseEntered(MouseEvent e)
       {
          JLabel x = (JLabel)e.getComponent();
     
          x.setForeground(Color.GRAY);
          //...more code to format label here...
       }
     
       //...rest of MouseListener methods here...
    }

    By doing that, I could format x however I wanted, and whatever changes I made to x would be mimicked into whatever JLabel triggered the event.

    So, I'm curious: how can I write a method that could do the same with my Tower objects? I would possibly use this for damaging enemies (perhaps determining what enemy was hit by a bullet, and damaging that enemy so I only had to write a single listener), or other related events (I would have to manually write these events, too). Again, this is unknown territory for me, but I don't want code laid out for me. Some general guidance would be greatly appreciated.

    Thanks!
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  18. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    method that could do the same with my Tower objects
    How are the Tower objects drawn on the GUI? Can you use the same logic with the mouse listener to tell the objects that the mouse is over them.

  19. #17
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    No, that's not what I meant.

    I meant to ask how could I manually write code that would do the same thing as the getComponent() method -- return a component and have whatever edits that are performed on whatever variable getComponent() is assigned to be mimicked to whatever getComponent() originally returned...

    Hmm... That ^ doesn't make anything any clearer... Sorry I can't provide a more explicit explanation.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  20. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    What class is the getComponent() method in? What does it do?

  21. #19
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    The getComponent() method is in the MouseEvent class, though I am relatively sure that it will also be in other Event classes. It returns the Component that triggered the event.

    If you look at my example code above, I have it used correctly there. What it allowed me to do was write one MouseListener that could be used for all of my labels, because it checks which JLabel triggered the event and only applies formatting changes to that JLabel. But my confusion is in the fact that I never explicitly tell the program which JLabel; instead I basically make a copy of the JLabel to JLabel x. However, whatever changes I make to x are also made to whatever JLabel triggered the event, which leads me to believe that a MouseEvent's getComponent() method somehow synchronizes two different variables...

    Sorry again for my lack of an ability to explain this correctly.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  22. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    The API doc: returns the Component object that originated the event, or null if the object is not a Component.

    What event are you going to create that you would want to tailor its getComponent() method to do what you want vs what the normal one does? Would there be listeners that would receive that event?

    I never explicitly tell the program which JLabel
    The component reference returned by getComponent is a reference to the JLabel that originated the event.

    Your confusion is that there can be many variables that refer to the same object.

  23. The Following User Says Thank You to Norm For This Useful Post:

    snowguy13 (December 7th, 2011)

  24. #21
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    What event are you going to create that you would want to tailor its getComponent() method to do what you want vs what the normal one does? Would there be listeners that would receive that event?
    Well, I'm thinking that I'm going to have to create my own events. For example, there may be an event that enemies used called a HitByBulletEvent, and there may be an event that towers use called a LevelUpEvent (I'm just throwing out possibilities). I'll try to avoid having to do this as much as possible, but I really am unsure of what I'll do in the future. Right now, I see clearly what I'm currently doing: making the general GUI's. However, looking ahead at what I have yet to do yields nothing but a mass of confusion and disorganization. So, I truly don't know whether I'll have to make custom events or not.

    However, should I end up having to do so, I may have to make my own getComponent() or perhaps a getTower() or getEnemy() method to get which tower fired a bullet so that the EXP goes to the right place or which enemy a bullet hit so that the damage is given to the correct enemy.

    Also, could you expand upon the multiple references to one object idea, or give me a link to a site that does so? I am predicting that that will be a useful technique to know...

    Your suggestions have been very thought provoking for me, as well as greatly helpful, so I once again thank you very much for taking so much time to help me.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  25. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    expand upon the multiple references to one object idea
    String ref1 = "The string";
    String ref2 = ref1; // now there are two references to "The String"
    String ref3 = ref2; // now 3

  26. #23
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    So, if you would say...

    ref2 = "Cake";
    System.out.print(ref1 + " " + ref3);

    ...the result would be...

    Cake Cake

    ...?

    But that doesn't seem right... Yet it works with getComponent()... I'm going to give myself a migrane. :p
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  27. #24
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Tower Defense on Java

    Your code does not show what the values of ref1 and ref3 are. You only show that ref2's value is changed to refer to the String "Cake"
    If you are using the variables from my example, their values would not be changed and what you show for the printed output is wrong.

  28. #25
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Tower Defense on Java

    That's what I thought. But then how does it work with getComponent()?

    If...

    //Allow an implied MouseEvent, e; let's also say that JPanel trigger caused the event

    JLabel x = e.getComponent();

    ...and I do this...

    x.setForeground(Color.ORANGE);

    ...that means that trigger's foreground color will also be change to orange. But why doesn't that work with the Strings? How could one make such an action work with Strings?
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

Page 1 of 3 123 LastLast

Similar Threads

  1. Extended Hanooi Tower
    By mahsa in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 31st, 2010, 05:55 PM