Results 1 to 6 of 6
  1. #1
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default Dynamic Subclasses?

    I'm working on a game that uses tiles. It's setup so players can make their own maps with the default tiles given. I want to make it so users can can create their own tiles with their own properties in a text file and be loaded to the game with all of its properties as a tile subclass. Is it possible to create subclasses like this?

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  3. #3
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    Ok, here is an example of some tiles programmed normally-

    Java Code:
    public class Tile
    {
    	Color c;
    	Image texture;
    	int alpha;
    	int bounce;
    	boolean solid;
    	boolean buildable;
    	boolean smoothDraw;
    	
    	etc...	
    		
    	public Tile()
    	{
    		c = new Color(Color.BLACK);
    		texture = null
    		bounce = 0;
    		
    		etc...
    	}
    }
    
    public class Grass extends Tile
    {
    	public Grass()
    	{
    		c = new Color(Color.GREEN);
    		
    		etc...
    	}
    }
    
    public class Dirt extends Tile
    {
    	etc...
    But what is a user wants to make a tile called "Trampoline" for his/her map that has a bounce property of 20. So that when a user comes down on it they will bounce. Can I dynamically generate this subclass based on that property?

    I know it's possible to make a Tile object and just set all these values one by one in a loop. But a map may have tens of thousands of tiles. Is that the only way it can be done or can I dynamically make the "Trampoline" subclass someway and add it to the Tile grid?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One solution is to rather than making a bunch of classes, make one Tile class that is flexible enough to change properties. And why not change the values in a loop? Even if you have many tiles this can work.

  5. #5
    MrFish is offline Member
    Join Date
    Jan 2010
    Posts
    43
    Rep Power
    0

    Default

    That's what I originally thought but I also thought that was a little primitive. So I guess I will go with that.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Yes, in a one class make possible to change required parameters according to your design. Then you may need to deals with additional values in a situation, if any. Otherwise only thing you've to do is assign appropriate values.

Similar Threads

  1. Lost with subclasses
    By vinyacam in forum New To Java
    Replies: 3
    Last Post: 05-09-2010, 07:12 PM
  2. Generics and subclasses
    By Sven in forum Advanced Java
    Replies: 1
    Last Post: 12-22-2008, 06:06 PM
  3. Main class and subclasses
    By rosh72851 in forum New To Java
    Replies: 5
    Last Post: 11-19-2008, 06:16 PM
  4. help needed with methods in subclasses
    By uncopywritable in forum New To Java
    Replies: 4
    Last Post: 08-01-2007, 01:47 PM
  5. Hibernate subclasses
    By Ed in forum JDBC
    Replies: 2
    Last Post: 07-02-2007, 04:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •