Reputation: 3
I have recently begun coding a 3d computer game as part of my IB MYP personal project, Although I know a sufficient amount of Java I am having troubles getting my classes to work together. What I'm trying to do is create a class called block that defines a block and all it's properties, then call the block class for a basic description of a block every time I create an individual block with unique properties. I've tried extends which works, but I'd have to create a new extending class for every unique block, and I've tried creating an object but it won't work. All my searches turned up dry. Here's my code:
package src;
public class Block {
//Defines a Block
double id; //Full = type, decimal = subtype
String type; //Name/tooltip
int sound; //Type of sound played on collision
int light; //Ammount of light given off
boolean breaks; //Wether the block is breakable
boolean solid; //Wether the block has collision detection
}
How can I go about calling this object multiple times in a different class, each time with all the values slightly different?
Upvotes: 0
Views: 89
Reputation: 33534
1. If the values keep changing, its better u make this class Abstract.
2. Then you can have a custom constructor to initialize the state of every object differently.
eg:
public Block(double id, String type, int sound, int light, boolean breaks, boolean solid)
3. Have getter methods so that you can fetch the values of the variables.
Upvotes: 0
Reputation: 3190
You could have a constructor for a Block as follows:
public Block(double id, String type, int sound, int light, boolean breaks, boolean solid) {
this.id = id;
this.type = type;
this.sound = sound;
this.light = light;
this.breaks = breaks;
this.solid = solid;
}
With this, you can create as many different kinds of Blocks as you want.
Upvotes: 2