Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [LibGDX][Solved] Field not loading from Json  (Read 187 times)
0 Members and 1 Guest are viewing this topic.
Offline Gibbo3771
« Posted 2014-06-10 09:00:14 »

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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
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:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
/**
 * 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:

1  
[{"class":"net.gibbo.breakout.entity.Block","x":9,"y":17}]

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Offline Gibbo3771
« Reply #1 - Posted 2014-06-10 09:07:17 »

Total fail, it is reading in the X and Y coords.

It is however, not actually writing the path of the sprite. So I could not see them, really should turn debug on.

So yeah, new problem. Why is it not writing the string object?

"This code works flawlessly first time and exactly how I wanted it"
Said no programmer ever
Pages: [1]
  ignore  |  Print  
 
 

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (44 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (87 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!