0

I am working on making a game where each level has different mechanics, but I want to keep all the input handling, rendering, etc in one place. My approach is to have a Level class with all the needed methods, then each level is its own class (Intro, BossRoom, etc) that extends Level and override methods to change the parts that need to change for each without having to copy over all of the contents of Level for each class. I have a LevelManager class that will hold all of the reference to the levels, and I was wanting to use ArrayList to do this.

I've tried using ArrayList<Level>, ArrayList<?>, ArrayList<? extends Level>, and ArrayList<? instanceof Level> both when I declare and instantiate the variable. Nothing I've tried seems to work. Is there something is Java that does this or should I just have each Level as a variable?

Sorry for block of text, I'm new to SO and do not know a better way to word it.

4
  • 1
    Post some relevant code. Commented Dec 3, 2016 at 20:20
  • 2
    Write up a minimal reproducible example Commented Dec 3, 2016 at 20:21
  • 2
    What's wrong with List<Level>? What issues does that cause? Commented Dec 3, 2016 at 20:21
  • Welcome to Stack Overflow! It looks like you are asking for homework help. While we have no issues with that per se, please observe these dos and don'ts, and edit your question accordingly. Commented Dec 3, 2016 at 20:22

1 Answer 1

1

It should rather be straight. Here is a little example.

Base class :

public abstract class Level{
  ....
}

Edit : You could declare the Level class as abstract if you deem that it should not (no sense) or it cannot be (at least one method is abstract ) instantiated in your application. According to your context, it is likely.

Child class :

public class IntroLevel extends Level{
...
}

Other child class :

public class LevelOne extends Level{
...
}

Your Level Manager :

public class LevelManager{

  private List<Level> levels;

  // constructor
  public LevelManager(){
   levels = new ArrayList<Level>();
   levels.add(new IntroLevel());
   levels.add(new LevelOne());
   ...
  }

}
Sign up to request clarification or add additional context in comments.

9 Comments

I'm fairly new to programming, so forgive the novice question, but what purpose does the abstract serve for the parent Level class? From my understanding it just makes it where you cannot instantiate it. Is that all it does or am I missing something?
Your understanding is good. It may avoid bad use of the class and it is also a way to clarify the intention of the class. When a class is abstract, you know that it is not enough to be usable.
@WilliamV in this instance, none. A class must be declared as abstract if any of its methods are declared abstract.
@davidxxx there is no requirement from the question that the class be abstract and as you can see, it just confuses the issue. Someone reading might think that a class has to be abstract to be used as generic parameter.
@pvg, I introduced the abstract modifier because the context of the question makes the statement that the Level class should probably not be instantiable : My approach is to have a Level class with all the needed methods, then each level is its own class (Intro, BossRoom, etc) Why allow to instantiate the Level class? By the way, it is a assumption based on I understand.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.