Jump to content

Phaser Arcade collision between sprite and layer not working


TheRoyalFool
 Share

Recommended Posts

When the game loads the player sprite falls to the bottom. Collides with the world fine but doesn't collide with the floor layer. I have tested the other layers both to see if they could be collided and by commenting them out to see if there was a multiple layer issue. No changes from doing either. Layers display fine and no errors appear in the console. debugger on player shows no collision except at world edges.

var game = new Phaser.Game(1800, 1400, Phaser.AUTO, '', {preload: preload, create: create, update:update, render:render});var map;var layers = [];var player;function preload() {    game.load.tilemap('LevelOne', 'TileMaps/Tutorial.json', null, Phaser.Tilemap.TILED_JSON);        game.load.image('Background', 'Assets/Background.png');    game.load.image('InterImages', 'Assets/InterImages.png');    game.load.spritesheet('MainCharacter', 'Assets/MainCharacter.png', 60, 80, 33);    game.load.spritesheet('StandardEnemyFull', 'Assets/StandardEnemyFull.png');    }   function create() {    //add tilemap, images and create layers, resize world    map = game.add.tilemap('LevelOne');    map.addTilesetImage('Background', 'Background');    map.addTilesetImage('Interaction', 'InterImages');    layers[3] = map.createLayer('Scenery');    layers[2] = map.createLayer('Activates');    layers[1] = map.createLayer('Floor');           //call in physics and assigns gravity    game.physics.startSystem(Phaser.Physics.ARCADE);    game.physics.arcade.gravity.y = 900;        //assign physics to the layer 'floor'    game.physics.arcade.enable(layers[3]);        map.setCollisionBetween(128,131, true, layers[3]);    map.setCollisionBetween(148,151, true, layers[3]);    map.setCollisionBetween(168,171, true, layers[3]);    map.setCollisionBetween(188,191, true, layers[3]);    map.setCollisionBetween(208,211, true, layers[3]);    map.setCollisionBetween(228,231, true, layers[3]);    map.setCollisionBetween(248,251, true, layers[3]);              //add player and enable player physics. Ensure player collides with wall    player = game.add.sprite(1040,1300,'MainCharacter');    player.anchor.x = player.anchor.y = 0.5;    game.physics.arcade.enable(player);    player.body.collideWorldBounds = true;    }  function update()    {    game.physics.arcade.collide(player, layers[3]);    }

Cheers in advance if anyone can help.

Link to comment
Share on other sites

You have to explicitly collide with the floor (layers[1]) just like you're colliding with the scenery (layers[3]). Add this to your update function:

game.physics.arcade.collide(player, layers[1]);

Also (and I know it's weird), arrays start from 0 in JavaScript. It doesn't hurt anything, exactly, but if you do "layers.length" it'll tell you 4 even though you only have three layers.

Link to comment
Share on other sites

I changed those like you said, thanks for pointing that out. However it did not initially work. I then messed around with the setcollisionbetween values and discovered they have to be the same values within my json file which was something ridiculous like 6,400 to 6,600 (I don't get why this is. I thought that the tileID was from the tileset used in Tiled?).

This now allows causes collision when I am on top of the tile, however when I move to the right or left I can phase through them. If I also set the upward velocity high enough I can jump through the tiles. The only thing that seems to be different is if I try and move down. Then I only move down something like five pixels and once I let go it reverts back to resting on the tile.
Below is my code;
 

function preload() 
{
    game.load.tilemap('LevelOne', 'TileMaps/Tutorial.json', null, Phaser.Tilemap.TILED_JSON);
    
    game.load.image('Background', 'Assets/Background.png');
    game.load.image('InterImages', 'Assets/InterImages.png');
    game.load.spritesheet('MainCharacter', 'Assets/MainCharacter.png', 60, 80, 33);
    game.load.spritesheet('StandardEnemyFull', 'Assets/StandardEnemyFull.png');
    
}

var map;
var player;
var layer1;
var layer2;
var layer3;
//var standardenemy;

    
function create() 
{
    //call in physics and assigns gravity
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.physics.arcade.gravity.y = 900;
    
    //add tilemap, images and create layers, resize world
    map = game.add.tilemap('LevelOne');
    map.addTilesetImage('Background', 'Background');
    map.addTilesetImage('Interaction', 'InterImages');
    
    layer = map.createLayer('Scenery');
    layer2 = map.createLayer('Activates');
    layer3 = map.createLayer('Floor');
    
    
    game.physics.arcade.enable(layer3);
   
    
    //add player and enable player physics. Ensure player collides with world bounds and the layer 'floor'
    player = game.add.sprite(1040,1300,'MainCharacter');
    player.anchor.x = player.anchor.y = 0.2;
    game.physics.arcade.enable(player);
    player.body.collideWorldBounds = true;   
    //set map collision for the sprites on floor of tileid x to x (Currently only works for standing on top of tile, can go through from side and bottom)
    map.setCollisionBetween(0, 10000, true, layer3);
    
    
    
    //game.camera.follow(player);
    
        
    //Add animations and assign a current animation to play   
    /*var idleright = player.animations.add('idleright', [0,1,2,3,4,5], 5, true);
    var idleleft = player.animations.add('idleleft', [6,7,8,9,10,11], 5, true);
    var walking = player.animations.add('walking', [12,13,14,15,16,17], 8, true);
    var wepready = player.animations.add('wepready', [18,19,20,21,22], 12, false);
    var swing = player.animations.add('swing', [24,25,26,27,28,29], 13, true);
    var dead = player.animations.add('dead', [30,31,32], 3, false);
    player.animations.play('idleright');*/
    
}  

function update()
    {
    game.physics.arcade.collide(player, layer3);
    
    
    if (game.input.keyboard.isDown(Phaser.Keyboard.A))
    {
        player.x -= 10;
    }
    else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
    {
        player.x += 10;
    }

    if (game.input.keyboard.isDown(Phaser.Keyboard.W))
    {
        player.y -= 10;
    }
     if (game.input.keyboard.isDown(Phaser.Keyboard.S))
    {
        player.y += 10;
    }
        
    
    }
function render()
    {
        game.debug.bodyInfo(player, 32, 32);
    }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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