Jump to content

Beginner Collision with Tile Layer Issue


heisenthurg
 Share

Recommended Posts

Hi!

 

First time using Phaser (and beginner to JS!) - I have a map with a ground layer, and also a water layer. I would like to kill the player and send them back to the start when they collide with the water. However, I have tried using .collide and passing it a function to kill the player, but it doesn't seem to work. Could someone give me some guidance on where I am going wrong please?

var game = new Phaser.Game(936, 914, Phaser.CANVAS, '', { preload: preload, create: create, update: update });function preload() {	this.game.load.spritesheet('character', 'assets/player-sheet.png', 32, 48);	this.game.load.tilemap('tilemap', 'assets/level.json', null, Phaser.Tilemap.TILED_JSON);	this.game.load.image('tiles', 'assets/tiles-sheet.png');}var map;var facing = 'right';var jumpTimer = 0;var cursors;var jumpButton;function create() {	//Start the Arcade Physics systems    this.game.physics.startSystem(Phaser.Physics.ARCADE);    //Map setup    this.game.stage.backgroundColor = "#a9f0ff";    this.map = this.game.add.tilemap('tilemap');    this.map.addTilesetImage('map_tileset', 'tiles');    this.backgroundLayer = this.map.createLayer('BackgroundLayer');    this.groundLayer = this.map.createLayer('GroundLayer');    this.waterLayer = this.map.createLayer('WaterLayer');    this.map.setCollisionBetween(1, 153, true, 'GroundLayer');    this.map.setCollision(44, true, 'WaterLayer');    this.groundLayer.resizeWorld();    game.physics.arcade.gravity.y = 800;    //Create Sprite    player = this.game.add.sprite( 200, 300, 'character' );    game.physics.enable(player, Phaser.Physics.ARCADE);    player.body.collideWorldBounds = true;    player.animations.add('left', [0, 1, 2, 3], 10, true);    player.animations.add('turn', [4], 20, true);    player.animations.add('right', [5, 6, 7, 8], 10, true);    cursors = game.input.keyboard.createCursorKeys();    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    //Make the camera follow the sprite    game.camera.follow(player);}function update() {    this.game.physics.arcade.collide(player, this.groundLayer);    this.game.physics.arcade.collide(player, this.waterLayer, killPlayer());    player.body.velocity.x = 0;    if (cursors.left.isDown)    {        player.body.velocity.x = -450;        if (facing != 'left')        {            player.animations.play('left');            facing = 'left';        }    }    else if (cursors.right.isDown)    {        player.body.velocity.x = 1450;        if (facing != 'right')        {            player.animations.play('right');            facing = 'right';        }    }    else    {        if (facing != 'idle')        {            player.animations.stop();            if (facing == 'left')            {                player.frame = 0;            }            else            {                player.frame = 5;            }            facing = 'idle';        }    }        if (jumpButton.isDown && player.body.onFloor() )    {        player.body.velocity.y = -950;    }}function killPlayer() {    player.kill;} 

Thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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