Jump to content

Check if a sprite overlaps with a tile.


Recommended Posts



this is my first game with phaser.


I try to make a 2d game with a tiled map and RPG like movement.


For this i need a sprite that can overlaps a tile and fires a callback. At the moment it is impossible for me to detect the overlapping without a collision. But with a collision the player can't run over the tile.


I only want to check if the player stand on a specific tile.

/* global Phaser */(function (P) {    'use strict';    var map;    var layerCollision;    var layerVisual;    var layerObjects;    var player;    var cursors;    var game = new P.Game(500, 450, P.CANVAS, '');    var state = {        preload: function () {            var _game = this;            _game.load.tilemap('map', '/static/js/map.json', null, P.Tilemap.TILED_JSON);            _game.load.image('floor', '/static/img/background.png');            _game.load.image('tiles', '/static/img/tiles.png');            _game.load.spritesheet('staff', '/static/img/staff.png', 50, 50);        },        create: function () {            var _game = this;            var objectTiles;            _game.physics.startSystem(P.Physics.ARCADE);                        _game.add.sprite(0, 0, 'floor');            // map stuff --- start            map = _game.add.tilemap('map');            map.addTilesetImage('Tiles', 'tiles');            map.setCollision(3);            layerCollision = map.createLayer('collision');            layerCollision.resizeWorld();            // map stuff --- end            // player stuff --- start            player = game.add.sprite(0, game.world.height- 50, 'staff');            _game.physics.enable(player);                        player.body.collideWorldBounds = true;            player.animations.add('left', [3], true);            player.animations.add('right', [1], true);            player.animations.add('up', [2], true);            player.animations.add('down', [0], true);            //player.body.customSeparateX = true;            //player.body.customSeparateY = true;            cursors = _game.input.keyboard.createCursorKeys();            // player stuff --- end                        // additional map stuff --- start            layerVisual = map.createLayer('visual');            layerVisual.resizeWorld();            layerObjects = map.createLayer('objects');            objectTiles = layerObjects.getTiles(0, 0, _game.world.width, _game.world.height);            //for (var i = 0, length = objectTiles.length; i < length; i++) {            //    objectTiles[i].setCollisionCallback(function () {            //        console.log('hit the tile very bad!');            //    }, player);            //}            //map.setCollision(2, true, layerObjects);            layerObjects.resizeWorld();            // additional map stuff --- end        },        update: function () {            var _game = this;            var speed = 250;            _game.physics.arcade.collide(player, layerCollision);            //_game.physics.arcade.overlap(player, layerObjects, function (sprite, tile) {            //    console.log('hit');            //    if (_game.input.keyboard.isDown(P.Keyboard.SPACEBAR)) {            //        console.log('hit');            //        //map.replace(2, 4, tile.x, tile.y, tile.width, tile.height, layerObjects);            //    }            //});            player.body.velocity.x = 0;            player.body.velocity.y = 0;            if (_game.input.keyboard.isDown(P.Keyboard.X)) {                speed = 500;            }            if (cursors.left.isDown) {                player.body.velocity.x = -speed;                player.animations.play('left');            } else if (cursors.right.isDown) {                player.body.velocity.x = speed;                player.animations.play('right');            } else if (cursors.up.isDown) {                player.body.velocity.y = -speed;                player.animations.play('up');            } else if (cursors.down.isDown) {                player.body.velocity.y = speed;                player.animations.play('down');            }        }    };    game.state.add('main', state);    game.state.start('main');    })(Phaser);
Link to comment
Share on other sites

Ok, thanks allot. I figured out how. I also have to call setCollision and game.physics.arcade.collide. I thought it will work without it.

            layerItems = map.createLayer('items');            map.setCollision(2, true, layerItems);            map.setTileIndexCallback(2, function () {                console.log('hit the tile so bad!');            }, _game, layerItems);...            _game.physics.arcade.collide(player, layerItems);
Link to comment
Share on other sites

Yeah you set collisions in the map seperate from tile callbacks. The callbacks work on touching the tile so you can do a range of things with them.

Collisions in a topdown rpg we're a bit weird to me but I found using 2 layers and collising with a single layer a good approach. Means the bottom layer can be all aesthetic floor tiles and grass and the collide layer can be houses and fences etc but not take any of the look away from the tilemap. Looks more seemeless with doubled layers.

Link to comment
Share on other sites

Thanks. On my old RPG Maker 2000 days i know the 2-3 layer concept. It worked very well. :)


I saw the createFromObjects function but don't know how do deal correctly with the result. :( I thought on a way to make an object layer for items and stuff the player can deal with. But i don't know how to use it wisely.

Link to comment
Share on other sites

to use the object layer you just need to make the opbject layer in tiled. Then when you're loading it in you need to create a group for the objects and then use a callback to kill and add score etc etc. My object layers are coded like this:


setGravs: function()        {            Gravtiles = this.add.group();            Rgravitiles = this.add.group();            map.createFromObjects('Gravs', 806, 'gravitoke', 0, true, false, Gravtiles);            map.createFromObjects('Gravs', 807, 'Regravitoke', 0, true, false, Rgravitiles);        },

I have mine in a function because in my create i have a layered mechanic and order to what needs created at what time etc but you can just take the middle of that and put it in your create.

basically the code above goes like this:


create the groups for the 2 objects that i am using. in my case gravitoke and Regravitoke. They are also named that in my JSON so thats the reason for them. Now the 'Gravs' is the actual layer in the JSON itself. The '806' is the ID of the tile that is the opbject. the 0 is the frame, i have no frames so i have it as 0. The true is exists and the false is for autocull. Then the ens Rgravitiles is the group.


After that just code an overlap kill/score++ with the group andyou should be fine to go. If my explanation doesnt help as much as you need there is always this:- http://examples.phaser.io/_site/view_lite.html?d=tilemaps&f=create+from+objects.js&t=create%20from%20objects

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