Parsing location values from flat file for modular code

Parsing location values from flat file for modular code
0.0 0


We are working on virtual lab application where there are some 100 small experiments to be developed as game.I have a shelf and table and i need to drop assets from shelf to the table and perform some operation.For now iam hardcoding the the location values for assets position and also for the drop position on the table for specific asset.But for 100 experiments to be done it is not a good idea to hard code the location values for the assets.Could you suggest me some parser where i can load the location values and store them in data structures and load them in actual program or is there any alternate solution to this which makes my code modular and light weight?


Hi abhiram,

I suggest using json format to save location values.

Hopes to help.


Yeah…My idea was to use JSON.I was unclear of the implementation in the code.Can you guide me in this by providing some example?


Abhiran, have you tried this?


I tried but I could not make it work.
I clearly did not understand how exactly to implement it.

Do you have soluiton / code for the same?
As of now my location values are hardcoded,I do not want to go the way.Iam searching for elegant solution but could not find.It would be highly appreciated if you can guide me in this.


Hi, Abhiram

I’m not sure I fully understand your need, but here is an idea

Maybe you can try to use TexturePacker, we support the ‘plist’ file it generate with our cc.SpriteFrameCache.

  1. Split your assets into separated image files, and ensure there is no name conflict.
  2. Drag all asset images into TexturePacker
  3. Desactive “Allow rotation” option
  4. Publish it into plist file
  5. Add generated plist file and image file (which include all your asset) to your cocos2d project
  6. Then in your project, you can create a all-in-one asset sprite with the generated image file, and you can also find each asset with the sprite frame cache.

If you need to know how SpriteFrameCache works, take a look at our test cases.
Hope it helps



My problem is i have to drag and drop the sprite in particular location on the screen where the location values are hard coded.
if(location.x > 200 && location.x < 300 && location.y > 300 && location.y < 500)
In the above the location values for the range where the sprite has to be dropped are being hard coded.IS there any solution instead of hard coding them in main file i want them to load from a text file and parse them and load them into array or any and compare against the index of data structure.


The problem is not about sprites and locations in particular, but rather about loading files that weren’t specified at coding time, but rather by the user when the game is running. Right?

Try the “XMLHttpRequest test” on the samples/tests folder of your cocos2d-html5 download. The source is on “XHRTest.js”. I think you can do it.


Iam sorry that you did not understand my problem clearly.

My intention was I have location values to be compared in the game and comparisions are many if else statements.
If(location. x > 200 && location.x < 400)
etc etc…

In the above iam comparing the location values by hardcoding them,I do not want to hard code them.The problem is in future my location values may changes so instead i changing each and every line in the code i would like to change them in flat text file where i save my location values and parse the during game loading and store them in an array or so and compare the location values against data structure.

Hope you understood my problem of parsing.


Yes, please try with the example/test I mentioned, I believe you can do it that way.