Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Tilemap Collision - Error & Hanging?


xronn
 Share

Recommended Posts

Hi,

 

I'm trying to collide my player with either a tile number or a layer either would be great! I use the collision debug and it shows the player should collide but doesn't, my player doesn't collide with anything. :(

 

I also have a fairly large sprite sheet, and I'm not sure CollisionBetween is the best method as it takes roughly 30 seconds for the game to load, but I'm not sure if you can collide multiple layers, as I haven't used phaser since 1.1.2.

 

(yes my sprite sheet has 18k tiles)
 

function preload() {    game.load.tilemap('world', 'world/world.json', null, Phaser.Tilemap.TILED_JSON);    game.load.image('tiles', 'world/assets/Tile-Set.png');    //Player    game.load.spritesheet('player', 'sprites/character/player.png', 22, 30);}
var player;var facing = 'left';var jumpTimer = 0;var cursors;var jumpButton;var map;var layer;function create() {    //Game setup    game.physics.startSystem(Phaser.Physics.ARCADE);    game.time.desiredFps = 30;    //Load the world    map = game.add.tilemap('world');    map.addTilesetImage('TileSet', 'tiles');    layer = map.createLayer('base_layer');    layer = map.createLayer('ground_layer');    layer = map.createLayer('object_layer');    layer = map.createLayer('building_layer');    layer.resizeWorld();    //Collision    map.setCollisionBetween(1, 18000);    console.log('struggle?');    layer.debug = false;    //Player    player = game.add.sprite(410, 3700, 'player');    game.physics.enable(player, Phaser.Physics.ARCADE);    player.body.bounce.y = 0.2;    player.body.collideWorldBounds = true;    player.body.setSize(20, 32, 5, 16);    player.animations.add('left', [6, 7, 8], 10, true);    player.animations.add('down', [0, 1, 2], 10, true);    player.animations.add('right', [9, 10, 11], 10, true);    player.animations.add('up', [3, 4, 5], 10, true);    player.animations.add('stand', [0], 10, true);    //Controls    cursors = game.input.keyboard.createCursorKeys();    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    game.camera.follow(player);}function update() {    game.physics.arcade.collide(player, layer);    player.body.velocity.x = 0;    player.body.velocity.y = 0;    if (cursors.left.isDown) {        player.body.velocity.x = -100;        player.animations.play('left');    }    else if (cursors.right.isDown) {        player.body.velocity.x = 100;        player.animations.play('right');    }    else if (cursors.down.isDown) {        player.body.velocity.y = 100;        player.animations.play('down');    }     else if (cursors.up.isDown) {        player.body.velocity.y = -100;        player.animations.play('up');    }    else {        player.body.velocity.x = 0;        player.body.velocity.y = 0;        player.animations.play('stand');    }}function render () {    game.debug.bodyInfo(player, 26, 24);}
Link to comment
Share on other sites

collisionBetween means collide between tiles of certain *types* (between a range of index values)

 

I'm sure you probably don't have 18,000 different types of tiles...hmm ok maybe you do..... why would you need that?!

 

http://phaser.io/examples/v2/tilemaps/tile-callbacks

 

also I see in here the tilemap json has a "collides" property for the layer. have you got that set to true?

http://examples.phaser.io/assets/tilemaps/maps/tile_collision_test.json

 

and see this about multiple layers, but it's old

http://www.html5gamedevs.com/topic/1450-collision-tiled-multiple-layers/

Link to comment
Share on other sites

I've set the range in the example code above to be 1 - 18000 so it collides with everything, that code currently runs and it collides with nothing. I think it could be to do with the way I load layers? Multiple layers using the same layers variable and then in the update I run collide player layer?

I'm not sure 


My tiles are 16x16 and my sheet is 2240 x 2400, theres a lot of required white space but yeah

Link to comment
Share on other sites

I've created a smaller 10 tile sprite sheet, and a new map, I've also set the CollisionBetween 3,6 and I can still walk everywhere so it must be my code, are there any multi layer examples?

 (Sorry auto signed into wrong phaser account)


[update 1] Yes, collision is working if all the tiles are on a single layer but thats very limiting, I must be doing something wrong for multi layers

[update 2] Okay so I have my original 18k tile map colliding correctly when a single layer is declared, but as soon as I declare another layer collision stops working, hmm.
 
    layer = map.createLayer('ground_layer');    layer = map.createLayer('base_layer');    layer.resizeWorld();

 

[update 3] Seems to work if each layer is stored in it's own variable, I guess resolved!
 

    base_layer = map.createLayer('base_layer');    ground_layer = map.createLayer('ground_layer');    ground_layer.resizeWorld();
Link to comment
Share on other sites

Ps I'm still impressed that you made 18,000 different graphics for your game, it must be a big game! Have you thought of splitting it up, so you're only loading certain subsets at any one time? Surely you don't need all 18,000 tile-types in one level?

If they're not all being used at once, I'd use copyRect or something to make myself a new spritesheet internally out of those that I need. (Depending on how it's laid out of course)

Without seeing it , I can't really be sure what you're doing

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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