Jump to content

My melonJS learning note : Using tile to defind entity in Tiled


Recommended Posts

When I start using melonsJS, I find out that integrate with Tiled make melonJS almost an editor base engine.

So I want to visually place player and enemy using there tile image as entity defind.
Problem is melonJS will load everything you defind, including these helper tileset.
Now I now how to replace these helper object in Tiled's json data , before building the map.
Here is the code in coffee:
convertHelperTileset = (mapJson, tilesetName)->
  tempArray = []
  for tileset in mapJson.tilesets
    if tileset.name isnt tilesetName
      tempArray.push tileset
    else #now covert all objects using this tileset
      for layer in mapJson.layers
        if layer.type is "objectgroup"
          for obj in layer.objects
            if obj.gid? #object that has gid is a tile object
              if obj.gid>=tileset.firstgid and obj.gid<=tileset.firstgid+tileset.tilecount
                delete obj.gid #will recognize as a rectangle object
                obj.y -= obj.height #this is the pivot point diffrent between tile and rectangle
  mapJson.tilesets = tempArray

After map are loaded,using


to get the map json object, and then use this function to convert all the helper tileset.

That is it.


Link to comment
Share on other sites

ooooh no I get what you meant by optional tileset, you meant use or not the provided tile id when creating an entity ?


Another option for that was to extend me.Entity, and just create a renderable from within the contrusctor, in that case melonJS does not add the "built-in" tile sprite :


Link to comment
Share on other sites

Let's me try to explain better:

I like to have actually "2 sets of Tiled map/room/stage tmx" setup in my workflow:
1.design/develop time tmx file, I will put a lot of helper object including tile object for visually design my level.
2.runtime tmx file, use in actual game, it's clean, for levelDirector to load, don't need to load helper tilesets nor objects.
And they don't need to be in 2 files, just use a little cpu time to "clean" the file before level start.
The workflow became very simple, edit the level in tiled, ctrl+s, reload the game.
So in this case, after I undenstand how melonJS works, I can solve this problem in user size, so melonJS don't need to do any change.

BUT, melonJS only treat objects in 2 cases:

1.Is this object has name? no -> create collison object, yes-> see below.
2.try to create entity from entity pool, if it is not exist, throw error and stop the game.
But objects in Tiled can be use on a lots of things :
*defind an area;
*defind a path for character to follow;

For example, put a poly line in tiled:

If leave the name empty, it became an collison object(even on a layer without collison in name):


If you give it any name, it throw an error:

Now, I think these should be improve, or user like me need to do more work to hacking it...

Link to comment
Share on other sites

The "name" that you give an object in Tiled is how melonJS associates a class to instantiate (through the entity pool registration). This is well documented in multiple places (seriously). This is by convention, only. But it's better than requiring code to instantiate classes using some kind of callback mechanism, or other association strategy. The pattern we chose is an example of data-driven development; build and maintain your games with data, not code!

If you want melonJS to ignore an object in Tiled, delete the object before loading the map in melonJS. Having a bunch of design-only layers and objects loaded at runtime is a bad idea (not clean). There are no features built into Tiled or the TMX format to "ignore these items at runtime".

However, you do have the option of preprocessing your TMX maps to remove the items you don't want at runtime. This is analogous to the very common practice of preprocessing JavaScript in various ways; ES6->ES5 transformations, minification, etc. Or preprocessing images to create texture atlases and CSS sprites.

I'm glad you're providing feedback, and sorry to hear that this is frustrating. But there are really good ways to keep your current workflow without hacking melonJS to ignore random stuff that you are putting into your maps on purpose.

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...