Jump to content

How to dynamicaly increase the size of my building .


Recommended Posts

HI, can some one suggest me how i can dynamically increase the size(increase the floors) of  building which created in  tiled map editor (exported  in JSON format) from game and save or update my JSON.

To increase the size of the building from game ,in which layer i have to create my building in titled map.

* I wanted  to increase floor by floor.

*user should give the input in form ,like how many floors he want to add.


Can someone help me out how can i achieve this

Thank You.

Link to comment
Share on other sites


It sounds like you want to implement tile map editing inside melonJS. This would be an amazing contribution; it's something that hasn't been worked on yet.

Hypothetically, it should just be a matter of maintaining a reference to the original JSON source, and updating that with the existing "setter" methods on the layer, tile, and map objects. For example, changing a tile on a layer is just a matter of calling the setTile method on the layer object. To update the JSON source as well, this method needs to be updated to poke the given tileId into the correct array location (based on provided coordinates) within the appropriate layer (defined by the layer object instance itself).

The UI is totally up to you; whether it's a form (as you asked), or click-and-drag style drawing (as in Tiled). In the end, you're going to use API calls like the one I linked above.

Best of luck!

Link to comment
Share on other sites

thankyou!!!!  in such case the original json file(the map) should be updated after each event. Is that what you are saying?? So even if I try to update the map json using node, to see the effect in the game i will have to load the page again rite??  

Link to comment
Share on other sites

No, you can do dynamic updates to the in-memory representation of the map. Just be aware that this in-memory representation is a series of class instances in melonJS, which are built out of the original JSON source. But the original JSON source is not used after the initial load.

The disconnect with saving the modified map out to JSON is the missing bits that the "setter methods" currently do not touch the JSON source. Does that make sense? It is a feature that can be added, that's all.

Link to comment
Share on other sites

3 differents ways :

  • if you want to have access to the loaded json, you can use the getJSON method from the loader :


  • if you want to have access to the parsed & interpreted map, you can use the level director:


  • and finally if you want to have access to the currently "loaded" map, you can use the getChildByXXX method from the world container object :


           this will for example return you the instantiated layer : http://melonjs.github.io/melonJS/docs/me.TMXLayer.html


now the usage depends a bit of what you exactly want to do ? do you want to change the level definition before a level is loaded ? or do you want to modify the level "in-game" ?

Link to comment
Share on other sites

@Parasyte..  regarding the in  in-memory representation of the map ..  so  'me.game.world.children' will give me all the object entities mentioned in the map.

so i was trying to alter in dynamically after an event.. since me.game.world.children is return an array. I was trying to push new object to the same array.

But its giving me error " TypeError: obj.update is not a function" .. is der any other way to alter it dynamically!!

Link to comment
Share on other sites

me.game.world.children is a private property, and you should not use it directly !


the proper way to add or remove child from a container (me.game.world is a container) is through the addChild and removeChild method :



also as you will see, addChild expect an object inheriting from the base me.Renderable class. 


as for modifying the level definition, you should rather directly get the me.Layer object, as I was explaining before

the following code for example, will returns you all the container TMX Layer object (actually returns an array of me.TMXLayer) :


that you can manipulate through the getter/setter described in the documentation here : http://melonjs.github.io/melonJS/docs/me.TMXLayer.html

Link to comment
Share on other sites

In addition, use me.game.world.getChildByName() and friends to lookup the instantiated layers and game objects.

When adding a child to the game world, make sure it is an instance of a class, and a not just a reference to the class itself. E.g. you need to use the new operator to create a new instance of the entity. There are many examples of this pattern, like the platformer tutorial, you'll find the following snippet:

// add our HUD to the game world
this.HUD = new game.HUD.Container();

An alternative is using the entity pool, which can help eliminate GC pauses by reusing instances. This snippet is from the Space Invaders tutorial:

me.game.world.addChild(me.pool.pull("player"), 1);
me.game.world.addChild(me.pool.pull("enemy", 50, 50), 2);


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...