elleniaw Posted November 6, 2018 Share Posted November 6, 2018 I'm trying to make a physics sprite collide with a Dynamic TileMap but I cannot get it collide. There is a github with my code here: https://github.com/nahkranoth/FactorySimulator/tree/tilemap-creator I took this as an example: https://labs.phaser.io/edit.html?src=src\game objects\tilemap\collision\tile callbacks.js I setup the tilemap like this (in tileMap.js) note that tileset is a tileset I created by generating a texture out of a graphic but I don't think that should matter: this.layer = this.map.createDynamicLayer(0, tileset, 0, 0); this.layer.setCollision(1); Then in game.js I make a physics sprite and add it to the collider: this.sprite1 = this.physics.add.sprite(120, 420, "tiles"); this.tilemap.layer.setTileIndexCallback(1, this.hitCoin, this); this.physics.add.collider(this.sprite1, this.tilemap.layer); >> The callback hitCoin is called when the sprite is colliding with a tile, but the sprite wont stop on a tile. While I took this from earlier code and it did stop. I know this explanation is missing a lot of details that matter - but if you look at those two files in the github project you should be able to find out those details. edit: Does the fact that I set the index of the tiles like tile.index = 1? IS there another method I'm not aware of? Link to comment Share on other sites More sharing options...
mhcdotcom Posted November 6, 2018 Share Posted November 6, 2018 I have a very similar setup and 2 things look different from yours to mine. This line: this.layer.setCollision(1); looks something like this in my code: this.layer.setCollisionByProperty({ collides: true }) Where 'collides' is a custom property I set in Tiled app. Might not apply to your build And here, where you wrote: this.tilemap.layer.setTileIndexCallback(1, this.hitCoin, this); I assigned the `this.tilemap.layer' to a variable and add that to the collider, something like: const itemTiles = this.map.addTilesetImage(imageKey); const itemLayer = this.map.createDynamicLayer(dynamicLayerName, itemTiles, 0, 0); itemLayer.setTileIndexCallback(tileIndex , this.collectItem, this); this.physics.add.overlap(this.sprite1, itemLayer); I'm a bit of a newbie myself, so I'm not entirely sure. At least you have something you can try out. Hope it helps at all. MHC Link to comment Share on other sites More sharing options...
elleniaw Posted November 7, 2018 Author Share Posted November 7, 2018 Thanks! I will take a look at your suggestions. Especially the first suggestion - althoug I don't see that's how the examples use the tile colliding - it might point me to the right direction. The weird part is that the tileIndexCallback does work - that function gets called without a problem. The problem is that the sprite doesn't stop moving when touching a tile. Like collision detection works, but it's effect on the body physics doesn't. Link to comment Share on other sites More sharing options...
elleniaw Posted November 7, 2018 Author Share Posted November 7, 2018 I tried adding the line and also seeing what it does this.layer.setCollisionByProperty({ collides: true }) And saw that it's setting the collision to true on a tile that has (for instance) the property collides as true attached to it. I tried setting that property to true when creating the tiles: _generatePerlexMap(){ for(let y=0;y<this.mapHeight; y++){ for(let x=0;x<this.mapWidth; x++){ let tile = this.map.getTileAt(x, y); let i = this._getRandom(x, y); tile.index = i; tile.collides = true; } } } But .collides is readonly. I thought maybe I misunderstood and tried to set it as tile.properties.collides but no luck there either. (It does not make sense doing it like this to me but I wanted to try it anyway) I see the TileIndexCallback being called on a hit but I don't see any expected physics behavior. In the examples I see a lot of tile collision examples being broken. I'm not complaining but I don't think this is a very stable part of Phaser at the moment and might have to look into the inner workings of tile layer collision and arcade physics to see if the problem might arise from there. I still have some hope that somebody can point me to an obvious mistake. Something I misunderstand. Please look into the github project if you want the complete project. Link to comment Share on other sites More sharing options...
elleniaw Posted November 10, 2018 Author Share Posted November 10, 2018 Found the problem: Because I generate the tilemap myself I have to be sure that all tile indexes are set when running the map.setCollision. This also means that for every tile I would generate I would have to either run the set collision again - or manually set colliding to true. Link to comment Share on other sites More sharing options...
Recommended Posts