Jump to content

Phaser Tiled tilemap layers not colliding with player


joshcawthorne
 Share

Recommended Posts

I've been working on a Phaser game for a project at university. All of this is relatively new stuff to me, so I'm learning on the fly. I've managed to create a simple tilemap using Tiled, and got it to import to Phaser successfully. However, I can't make the player collide with the 'platform' layer, and I've tried just about everything I can find online.

I've cut down my game code to just what's needed for you to (hopefully) point out my error, but it all runs without errors usually. I can also upload any JSON or Image files that might be needed.

var SuddenGame = SuddenGame || {};

var char;
var map;

// GAMEPLAY STATE //
SuddenGame.playState = function() {};

SuddenGame.playState.prototype = {

    init: function() {
        this.game.renderer.renderSession.roundPixels = true;
    },

    create: function() {

        //Setup FPS Counter
        this.game.time.advancedTiming = true;

        //Stop game from pausing if a player tabs out
        this.stage.disableVisibilityChange = true;

        //Import and play background music
        music = this.game.add.audio('menumusic');
        music.play('', 0, 1, true);

        //Create Dotted Background
        this.background = this.game.add.tileSprite(0,
            this.game.height - this.game.cache.getImage('background').height,
            this.game.width,
            this.game.cache.getImage('background').height,
            'background'
        );

        this.dots = this.game.add.tileSprite(0,
            this.game.height - this.game.cache.getImage('dots').height,
            this.game.width,
            this.game.cache.getImage('dots').height,
            'dots'
        );

        //Enable Physics Engine
        this.game.physics.startSystem(Phaser.Physics.ARCADE);

        //Add level one tilemap 
        map = this.game.add.tilemap('levelone');

        map.addTilesetImage('scifi_platformTiles_32x32', 'scifi_platformTiles_32x32');
        backgroundTiles = map.createLayer('backgroundLayer');
        Objective = map.createLayer('Objective');
        Platform = map.createLayer('Platform');

        this.game.add.existing(Platform);
        this.physics.arcade.enable(Platform);

        map.setCollisionBetween(1, 1000, true, 'Platform');

        Platform.resizeWorld();
        Platform.debug = true;

        this.char = this.add.sprite(50, 470, 'hero');
        this.physics.arcade.enable(this.char);
        this.char.body.gravity.y = 1300;
        this.char.body.collideWorldBounds = true;
        this.char.scale.setTo(2, 2);

        //Create Player Animations
        var idle = this.char.animations.add('idle', [0]);
        var walk = this.char.animations.add('walk', [0, 1, 2, 1]);

        this.game.camera.follow(this.char);

    },

    update: function() {

        this.game.physics.arcade.collide(char, Platform);
        this.physics.arcade.TILE_BIAS = 40;


        if (this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT) && !this.char.body.touching.left) {
            this.char.body.velocity.x = -150;
            this.char.animations.play('backwalk', 10, true);
        } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) && !this.char.body.touching.right) {
            this.char.body.velocity.x = 150;
            this.char.animations.play('walk', 10, true);
        } else if (this.game.input.keyboard.isDown(Phaser.Keyboard.DOWN)) {
            this.char.animations.play('die', 10, true);
        } else {
            this.char.body.velocity.x = 0;
            this.char.animations.play('idle', 7, true);
        }

        if (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.game.time.now > jumpTimer) {
            {
                this.char.animations.play('jump', 10);
                this.char.body.velocity.y = -1000;
                jumpTimer = this.game.time.now + 750;
            }
        }

        //if (this.cursors.up.isDown && !this.char.body.touching.down ) {
        if (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.game.input.keyboard.isDown(Phaser.Keyboard.LEFT) && this.char.body.touching.left && this.game.time.now > jumpTimer) {
            this.char.body.velocity.y = -500;
            jumpTimer = this.game.time.now + 1;
        }
        if (this.game.input.keyboard.isDown(Phaser.Keyboard.SPACEBAR) && this.game.input.keyboard.isDown(Phaser.Keyboard.RIGHT) && this.char.body.touching.right) {
            this.char.body.velocity.y = -500;
            this.char.body.velocity.x = -2500;
            //jumpTimer = this.game.time.now + 1;
        }

        //More update code here
    },

    render: function() {
        this.game.debug.text(this.game.time.fps || '--', 2, 14, "#00ff00");
    }
}

Apologies if I've missed an extremely obvious mistake!

Link to comment
Share on other sites

I don't know if you've stripped this down and missed it out, but some of your variables aren't declared. But they might be in other files so it's hard to say.

I think I found the culprit: At the top you declare var char; but in your create function you assign things to this.char which is a new variable that belongs to SuddenGame.playState and then in the update function your code tries to collide char (not this.char aka SuddenGame.playState.char) with this.game.physics.arcade.collide(char, Platform);

try:

this.game.physics.arcade.collide(this.char, Platform);

Java script's this is pretty wonky... maybe comb through your code and make sure everything is declared and used exactly where you want it. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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