danbruegge Posted April 24, 2014 Share Posted April 24, 2014 Hi, 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 More sharing options...
Heppell08 Posted April 24, 2014 Share Posted April 24, 2014 Look at tile functions. They do all the necessary callbacks you'll need. Link to comment Share on other sites More sharing options...
Heppell08 Posted April 24, 2014 Share Posted April 24, 2014 Here's my answered post from a while ago.http://www.html5gamedevs.com/topic/3932-tile-callback-function-undefined/ danbruegge 1 Link to comment Share on other sites More sharing options...
danbruegge Posted April 24, 2014 Author Share Posted April 24, 2014 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 More sharing options...
Heppell08 Posted April 24, 2014 Share Posted April 24, 2014 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 More sharing options...
Heppell08 Posted April 24, 2014 Share Posted April 24, 2014 But...If you have an object layer and have items in the game, you can call a kill on the items and other stuff in the overlap function. Have a look a createFromObjects in phaser examples for that too Link to comment Share on other sites More sharing options...
danbruegge Posted April 24, 2014 Author Share Posted April 24, 2014 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 More sharing options...
Heppell08 Posted April 25, 2014 Share Posted April 25, 2014 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 danbruegge 1 Link to comment Share on other sites More sharing options...
danbruegge Posted May 2, 2014 Author Share Posted May 2, 2014 Thans for this. Link to comment Share on other sites More sharing options...
Recommended Posts