0

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?

3
  • 1
    Create multiple objects? I don't get it... Commented Jul 17, 2012 at 18:02
  • I don't see anything static here at all. This code doesn't actually do anything, either; it just defines a class. How are you trying to use it? Commented Jul 17, 2012 at 18:04
  • So you want to have multiple different objects that are like Block? Like Block1, Block2, Block3, etc or you want to just create...multiple Block objects with different values for id,type,etc? Commented Jul 17, 2012 at 18:09

2 Answers 2

2

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.

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

1 Comment

This worked! I knew there was a simple fix. Thank you for bringing it out into the light :)
0

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.

1 Comment

Thank you, I'll need this too as some of my classes will involve multiple different variations of the object. Thank you :)

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.