Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBacterius

Posted 18 December 2012 - 06:00 PM

If I understand what you're asking, I simply mean that constructors are often overloaded. However, in the process of designing the level, I wanted the user to define the objects they create.
I have a Player object for instance that can turn 360 degrees. Based on the level being designed, the user may want the player to be turned 270 degrees at the start, but if they don't specify I want it to default to 90 degrees. This is an example of an option parameter, because in my code I've accounted for it, by having a constructor that doesn't include an angle parameter.

Yes, I understand that (though in many languages you can achieve the same thing by specifying a default value for parameters, I'm not sure Java can though). I think I understand what you want - you need objects to behave differently depending on the arguments passed to the constructor. One way to do it is to use inheritance, where all objects in your world derive from a base Entity class, and you can make different classes such as Player, Wall, Enemy, etc.. inherit from this, all with their own individual behavior, arguments, constructors, etc... Then you can just store an array of Entity instances (which may be players, walls, etc.. you don't care) and call the appropriate methods, e.g. does this Entity intersect with another (regardless of what the entities actually represent). This may not work well if your entities are too different from one another and don't share common features, and inheritance is often not the preferred solution.

So here I don't think the problem is about data structures, but about design patterns.

#1Bacterius

Posted 18 December 2012 - 05:58 PM

If I understand what you're asking, I simply mean that constructors are often overloaded. However, in the process of designing the level, I wanted the user to define the objects they create.
I have a Player object for instance that can turn 360 degrees. Based on the level being designed, the user may want the player to be turned 270 degrees at the start, but if they don't specify I want it to default to 90 degrees. This is an example of an option parameter, because in my code I've accounted for it, by having a constructor that doesn't include an angle parameter.

Yes, I understand that (though in many languages you can achieve the same thing by specifying a default value for parameters, I'm not sure Java can though). I think I understand what you want - you need objects to behave differently depending on the arguments passed to the constructor. One way to do it is to use inheritance, where all objects in your world derive from a base Entity class, and you can make different classes such as Player, Wall, Enemy, etc.. inherit from this, all with their own individual behavior. Then you can just store an array of Entity instances (which may be players, walls, etc.. you don't care) and call the appropriate methods, e.g. does this Entity intersect with another (regardless of what the entities actually represent). This may not work well if your entities are too different from one another and don't share common features, and inheritance is often not the preferred solution.

So here I don't think the problem is about data structures, but about design patterns.

PARTNERS