I am in the process of making a little breakout game for a change of pace, I have made a basic level editor that allows me to place blocks on a grid then parse them to json.
When I try to load these back in, the X and Y field for positioning seems to be 0 despite that in the file it clearly has the X and Y coordinate, I am not quite sure what I am doing wrong.
The game is using Box2D so it has a better flow, I am not serialising this stuff. I simply call a create() method after the block has been loaded, this creates the Box2D stuff and sets the sprite. Here is my block class:
public class Block extends Box2DObject {
/** The location on file that the sprite is located */
public String path = "graphics/blocks/blue.png";
/** This blocks sprite */
transient Sprite sprite;
/** The width of the block in meters */
private final float WIDTH = 0.75f;
/** The height of the block in meters */
private final float HEIGHT = 0.375f;
/** The x coord of the block*/
public float x;
/** The y coord of the block*/
public float y;
public Block() {
}
/**
* Constructs a new block at a given coordinate
*
* @param x
* the x coordinate
* @param y
* the y coordinate
*/
public Block(float x, float y) {
this.x = x;
this.y = y;
sprite = new Sprite(new Texture(Gdx.files.internal(path)));
create();
}
/**
* This positions, sets block rotation and finally draws it relative to the
* body
*
* @param batch
*/
public void draw(SpriteBatch batch) {
sprite.setPosition(getBody().getPosition().x - (WIDTH), getBody()
.getPosition().y - (HEIGHT));
sprite.setSize(WIDTH * 2, HEIGHT * 2);
sprite.draw(batch);
}
/**
* Creates the Box2D portion of the object
*/
public void create() {
sprite = new Sprite(new Texture(Gdx.files.internal(path)));
createBody(Engine.physics.getWorld(), BodyType.DynamicBody,
new Vector2(this.x, this.y), false);
createPolyFixture(WIDTH, HEIGHT, 0.25f, 0.5f, 1, true);
}
These all get added to an Array in an entity class, this array is passed to the class that handles drawing. Nothing to complicated there. Here is the part where I read the data from json and put it into the array:
/**
* Responsible for loading level
*
* @author Stephen Gibson
*/
public class Level {
/** For parsing levels from json */
Json json = new Json();
@SuppressWarnings("unchecked")
public Level(String level) {
String string = Gdx.files.external("Breakout/levels/"+level+".lvl").readString();
Engine.entitySystem.blocks = json.fromJson(Array.class, string);
for(Block block : Engine.entitySystem.blocks){
block.create();
System.out.println("creating block");
}
}
}
Any idea why the X and Y fields are not being loaded? Here is the file:
[{"class":"net.gibbo.breakout.entity.Block","x":9,"y":17}]