Jump to content

Tiled objects


Recommended Posts

hey again. I have a problem with tiled objects from a tiled map object layer.

The problem is that when I use createFromObjects:


 player = map.createFromObjects('npcs', 'player', {key: 'playerSheet'});

the player isn't moveable. It doesn't have a body and there doesn't seem to be a way to even move a sprite that doesn't have a body. If I try player.setPosition(), causes runtime error because player doesn't have that function. If I try player.Sprite.setPosition, I pretty much get the same thing. So I tried to create a physics enabled version of the created object like this:


 player = this.physics.add.sprite(map.createFromObjects('npcs', 'player', {key: 'playerSheet'}));

and it still creates the object, but I cannot move the object or if I try to have the camera follow the object i just get a black screen. 

So the only way I seem to be able to make a moveable - camera followed player, is to :


player = this.physics.add.sprite(0, 0, 'playerSheet').setOrigin(0, 0);

just manually create the player. Which is fine, except my object layer will in future have enemies and npcs in it, and they will need to be able to move, obviously.
So does anyone know how to do this in a way that works. I need my objects to be able to move, to have physics enabled for collisions etc.

Thanks for any help or just for reading.

Link to comment
Share on other sites

createFromOjects returns an array of the objects created. So in the first two examples your player is not the sprite itself but instead an array which contains the player sprite. createFromObjects is a fantastic method but its going to take a bit more to get it to do what you want. For instance you could check the contents of the array and assign them to variables. Or you could create custom gameobjects and register them to the GameObjectFactory which can then be created using createFromObjects. Your custom objects would have your additonal methods, movement etc.


I cut all of that out and just made my own object creation method. See the convertObjects() method on my LevelScene:


Link to comment
Share on other sites

Thanks again, B3L7, really helped. Well, I couldn't make too much out of your code, though thanks for sharing that and maybe ill study it closer in future, but knowing that the object was actually an array - or at least contained an array, led me to console.log my objects and figure out different ways of accessing the data. Which helped a lot, and allowed me to create the player from the tiled info and have the camera etc still work as I wanted.

Would it be really cheeky to ask yet another question? Sorry if it is, but I will anyways as you seem super helpful. 

I set up collision boxes for my wall layer in tiled as seen in the image below. These tiles are part of a tileset that is 32x32 per tile, but because these wall tiles are smaller - made to look thin - i made the Bounding boxes as small as I could, i.e smaller than 32x32.

However in Phaser,  the collision boxes are still just the size of the tile itself, so 32, 32. I thought the point of drawing your collision box in tiled was to allow for cases like this (smaller box than tile dimensions) yet there doesn't seem to be a way.

I tried every collision related method i could find in the docs. The only one that I couldn't get to work was the impact one, but im not keen on using that anyways as I have no idea about impact, and i've only just started learning phaser, let alone another engine and I'm not using slopes( top-down game). And when I say worked, I mean they didn't cause a crash, but all they did was make a 32x32 box anyways.

Anyway if you have any insight on this that'd be wicked, thank you.




Edit, nevermind. I still think the collision boxes are bigger than they should be, but having looked over your code I see you don't do anything special there to compensate for wrongly sized collision boxes. I found out that my problem was being exacerbated by having set my player's origin to 0, 0, and after commenting that out, and changing the size of the player's body, the map functions not as expected, but in a way that works( character can easily enter doorways and get closer to walls than before etc).

Thanks again for all the help today dude. 

Edited by NoxBrutalis
Link to comment
Share on other sites

Physics bodies really depend on what system you are using. In arcade you can change the size of the bodies for sprites and images fairly simply but as far as I know you can't change the size of the bodies on tiles (I could be wrong). So I believe that if you are using arcade physics then phaser will disregard collision shapes from tiled and just create a body the size of the tile.

Matterjs allows for much more complex bodies and thus allows you to define collision shapes on tiles as well.


Hope that makes sense.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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