Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (498)
Games in Android Showcase (117)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Looping through ArrayList using enums?  (Read 672 times)
0 Members and 1 Guest are viewing this topic.
Offline VaPS
« Posted 2013-10-06 22:46:59 »

I'm having trouble creating a method which loops through an ArrayList of multiple types of objects.

Here's my failed attempt:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
import java.util.ArrayList;

public class DebugObjects {

   public enum Block {
      Solid, Bad, Bounce
   }

   public static void debug(ArrayList<Block> list) {

      for (Block b : list) {

         String n = b.getClass().getName();
         byte i = (byte) Solid.list.indexOf(b);

         if (b.X < 0) {
            System.out.println(n + " " + i + " is outside the left side.");
         }
         if (b.X + b.W > Game.WH) {
            System.out.println(n + " " + i + " is outside the right side.");
         }
         if (b.Y < 0) {
            System.out.println(n + " " + i + " is outside the up side.");
         }
         if (b.Y + b.H > Game.WH) {
            System.out.println(n + " " + i + " is outside the down side.");
         }

      }

   }

}


My goal with this is to be able to call the method like this:
DebugObjects.debug(Solid.list);
This would loop through all Solid objects inside of Solid.list and tell me if any of them exceed the game window.

Thanks in advance!

Offline Troncoso

JGO Coder


Medals: 20



« Reply #1 - Posted 2013-10-06 23:25:34 »

What issue are you having with your current code?  I can see that you are doing b.X and b.Y when the Block enum has no such fields.
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #2 - Posted 2013-10-07 03:52:30 »

Yes, indeed. Also, why make a debug() method? The whole point of debugging is finding the error in some piece of code that your game uses. If it's in the player class, debug it there. If in the game loop, debug it there. Or even better use a debugger.

Also, your enum code isn't valid. Should be a semicolon after Bounce.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Danny02
« Reply #3 - Posted 2013-10-07 06:40:40 »

Your example code is not complete, wouldn't compile. So I can only throw some random thought at you.
When you create one enum you also create only ONE class. Solid, Bad and Bounce in your example are NOT classes they are simple instances of the Block class.

When you want to print a enum instance you can use the .name() method or just rely on the toString method, like:
1  
2  
println("10 " + Block)
//prints "10 Block"
Offline VaPS
« Reply #4 - Posted 2013-10-07 13:03:46 »

Yes, indeed. Also, why make a debug() method? The whole point of debugging is finding the error in some piece of code that your game uses. If it's in the player class, debug it there. If in the game loop, debug it there. Or even better use a debugger.

Also, your enum code isn't valid. Should be a semicolon after Bounce.
My goal is not exactly debugging but the name has nothing to do with it. Plus, according to pretty much all my Google searches, the semicolon is optional when using enums.

Offline VaPS
« Reply #5 - Posted 2013-10-07 13:05:39 »

Your example code is not complete, wouldn't compile. So I can only throw some random thought at you.
When you create one enum you also create only ONE class. Solid, Bad and Bounce in your example are NOT classes they are simple instances of the Block class.

When you want to print a enum instance you can use the .name() method or just rely on the toString method, like:
1  
2  
println("10 " + Block)
//prints "10 Block"

I don't know if I misunderstood but Solid, Bad & Bounce are all classes in my game. The printing is not what I want help with, what I need help with is what I actually wrote in the post.

Offline xsvenson
« Reply #6 - Posted 2013-10-07 13:06:44 »

You have not yet defined what exactly is the problem You are having ?

“The First Rule of Program Optimization: Don't do it. The Second Rule of Program Optimization (for experts only!): Don't do it yet.” - Michael A. Jackson
Offline Danny02
« Reply #7 - Posted 2013-10-07 13:17:10 »

The problem we have with your example is, that the code you show us doesn't make sense.

You define a Block enum with a Solid INSTANCE, but also use Solid.list. This doesn't make sense, because the Block enum does not have a "list" field. Is it possible that you also created classes which have the same name as the enum instances?

Perhaps does the following help, your List<Block> does NOT hold "multiple types of objects." it does only hold Block objects. You can switch over enums, so you can give the following a try.

1  
2  
3  
4  
5  
6  
7  
8  
for(Block b: blockList)
{
   switch(b){
     case Solid: foo(); break;
     case Bad: bar(); break;
     case Bounce: zomfg(); break;
  }
}
Offline Troncoso

JGO Coder


Medals: 20



« Reply #8 - Posted 2013-10-07 13:20:08 »

Yes, indeed. Also, why make a debug() method? The whole point of debugging is finding the error in some piece of code that your game uses. If it's in the player class, debug it there. If in the game loop, debug it there. Or even better use a debugger.

Also, your enum code isn't valid. Should be a semicolon after Bounce.

Technically, it's not needed. Only if you add methods after the constants.
Online Jimmt
« League of Dukes »

JGO Kernel


Medals: 132
Projects: 4
Exp: 3 years



« Reply #9 - Posted 2013-10-07 16:31:42 »

Ah, my bad. Didn't know that.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

radar3301 (12 views)
2014-09-21 23:33:17

BurntPizza (30 views)
2014-09-21 02:42:18

BurntPizza (22 views)
2014-09-21 01:30:30

moogie (20 views)
2014-09-21 00:26:15

UprightPath (28 views)
2014-09-20 20:14:06

BurntPizza (33 views)
2014-09-19 03:14:18

Dwinin (48 views)
2014-09-12 09:08:26

Norakomi (74 views)
2014-09-10 13:57:51

TehJavaDev (103 views)
2014-09-10 06:39:09

Tekkerue (50 views)
2014-09-09 02:24:56
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!