Hello everyone,
first time here (so might excuse if its the wrong section) but I’m fricking desperate at the moment.
I’m trying for about 2 days now to get my player to collide with my “Obstacle” tilemap layer. I looked at almost every post about this here or elsewhere but I can’t get it to work. It might be a simple thing but I can’t find whats wrong and I want to throw my pc out of the window now. So maybe one of you guys have an idea and can help me. Any help is appreciated.
The code is the Following:
var game = new Phaser.Game(255, 255, Phaser.AUTO, '', { preload: preload,
create: create,
update: update
});
var player;
var speed = 2;
var floor;
var walls;
var obsts;
var map;
function preload()
{
game.load.spritesheet('player','assets/player_spritesheet.png', 48,48);
game.load.image("tileset", "assets/tileset.png");
game.load.tilemap(
"map",
"assets/test_map.json",
null,
Phaser.Tilemap.TILED_JSON
);
// Phaser.Canvas.setSmoothingEnabled(this.game.context, false);
this.game.scale.scaleMode = Phaser.ScaleManager.USER_SCALE;
this.game.scale.setUserScale(3, 3);
this.game.renderer.renderSession.roundPixels = true;
Phaser.Canvas.setImageRenderingCrisp(this.game.canvas);
}
function create()
{
// Create map
map = game.add.tilemap("map", 48, 48, 16, 16);
map.addTilesetImage('tileset');
// Use layers
floor = map.createLayer('Floor');
walls = map.createLayer('Walls');
obsts = map.createLayer('Obstacles');
map.setCollisionBetween(1,1000);
// create player
player = game.add.sprite(50,50,'player');
// all physics related stuff
game.physics.startSystem(Phaser.Physics.ARCADE);
game.physics.enable(player, Phaser.Physics.ARCADE);
player.body.setSize(10, 14, 2, 1);
player.body.collideWorldBounds = true;
// walk animations
player.animations.add('idle',[0,1,2],10,true);
player.animations.add('walk_right', [12,13,14,15],10,true);
player.animations.add('walk_left', [20,21,22,23],10,true);
player.animations.add('walk_down', [8,9,10,11],10,true);
player.animations.add('walk_up', [16,17,18,19],10,true);
//attack animations
player.animations.add('attack_down', [24,25,26,27],10,true);
player.animations.add('attack_right', [28,29,30,31],10,true);
player.animations.add('attack_left', [32,33,34,35],10,true);
player.animations.add('attack_up', [36,37,38,39],10,true);
player.animations.play('idle');
game.camera.follow(player);
}
function update()
{
game.physics.arcade.collide(player,obsts);
player.body.velocity.set(0);
// Walk mechanic
if(game.input.keyboard.isDown(Phaser.Keyboard.A))
{
player.animations.play('walk_left');
player.x -= speed;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
{
player.animations.play('walk_right');
player.x += speed;
}
if(game.input.keyboard.isDown(Phaser.Keyboard.W))
{
player.animations.play('walk_up');
player.y -= speed;
}
else if (game.input.keyboard.isDown(Phaser.Keyboard.S))
{
player.animations.play('walk_down');
player.animations.stop;
player.y += speed;
}
// attack mechanic
if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
{
player.animations.play('attack_left');
}
if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
{
player.animations.play('attack_right');
}
if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
{
player.animations.play('attack_up');
}
if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
{
player.animations.play('attack_down');
}
//jump back to idle when not walking or attacking
else if (
!game.input.keyboard.isDown(Phaser.Keyboard.A) && !game.input.keyboard.isDown(Phaser.Keyboard.D) && !game.input.keyboard.isDown(Phaser.Keyboard.W) && !game.input.keyboard.isDown(Phaser.Keyboard.S) &&
!game.input.keyboard.isDown(Phaser.Keyboard.UP) && !game.input.keyboard.isDown(Phaser.Keyboard.DOWN) && !game.input.keyboard.isDown(Phaser.Keyboard.LEFT) && !game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){
player.animations.play('idle');
}
}
Also, if you find any other mistakes, fell free to add suggestions to those as well. I’m new to phaser and have no clue what I’m doing.