I started reading "Java Game Development with LibGDX" and I'm building a game, which is explained step by step in chapter 3 of the book. The player controls a turtle, avoiding rocks and captures starfish. I'm using intellij (instead of blueJ, recommend by the book) and java8.
To explain the problem. The BaseActor Class has a method "ArrayList getList" which creates a list for each type of actor(which can be found in the StarfishCollecor, which are 4 Rocks actors and 4 Starfish actors. The output should be 2 actor lists. The Rock list is created no problem, but the starfish list generates a error: java.lang.ClassNotFoundException: Starfish
Can I do this different? Yes, but this code will be reused later in the book, so I like to solve the problem using this method.
I looked around the internet and I found a post (Class Not Found Exception on Class.forName()), asking the same question I have and the person seems to have found the solution.
S/he says the For each loop was getting out of bounds for the Starfish class and did a little null checking. But I don't understand how the For each loop gets out of bound or what s/he means with null checking. Please check the code in the post. And if you can explain the solution in a way a simple beginner like me can underhand, you would help be out a lot.
Lastly I put a prinln in the getList method to check how the method is building the list. The method is checking for actors in the MainStage and only grabbing the ones with the name "Rock" and later again for "Starfish". At the end the action is repeating, but I dont understand why that is. If feel this is were thinks get out of hand.
BaseActor index: BaseActor || list size: 0 || BaseActor list: []
BaseActor index: Starfish || list size: 0 || BaseActor list: []
BaseActor index: Starfish || list size: 0 || BaseActor list: []
BaseActor index: Starfish || list size: 0 || BaseActor list: []
BaseActor index: Starfish || list size: 0 || BaseActor list: []
BaseActor index: Rock || list size: 1 || BaseActor list: [Rock]
BaseActor index: Rock || list size: 2 || BaseActor list: [Rock, Rock]
BaseActor index: Rock || list size: 3 || BaseActor list: [Rock, Rock, Rock]
BaseActor index: Rock || list size: 4 || BaseActor list: [Rock, Rock, Rock, Rock]
BaseActor index: Turtle || list size: 4 || BaseActor list: [Rock, Rock, Rock, Rock]
BaseActor index: BaseActor || list size: 0 || BaseActor list: []
BaseActor index: Starfish || list size: 1 || BaseActor list: [Starfish]
BaseActor index: Starfish || list size: 2 || BaseActor list: [Starfish, Starfish]
BaseActor index: Starfish || list size: 3 || BaseActor list: [Starfish, Starfish, Starfish]
BaseActor index: Starfish || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
BaseActor index: Rock || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
BaseActor index: Turtle || list size: 4 || BaseActor list: [Starfish, Starfish, Starfish, Starfish]
Class.forName()with a value in aforloop? And do you understand whatnullis in Java?nullas theclassNameto thegetList()method. But they don't show the code where they did that so I can't tell any more about what they did. Just looking at the code they did post, I would be wary of the advice of whoever wrote the tutorial. It is a fundamental design error to load a Texture in the constructor of an Actor, especially if there are multiple copies of that Actor.