简体   繁体   中英

Phaser.js how to make vertical walls solid?

I created a little scene with walls. Bottom wall is solid. But left and right are not. Why? Can you please help me?

Game Scene

So as you can see I can stand on bottom wall. It's solid. Mario isn't falling down. But I can pass through left or rigth wall. I don't know what's the problem is because all these walls are in the same group.

So also I saw that when you are trying to pass through the left wall from the ground - You can do it. But when you want to do the same from top, I mean to fall on the top of the wall - you can't do that.

So how to make those walls (right and left) solid to not to go through from ground?

create: function()
{
    game.physics.startSystem(Phaser.Physics.ARCADE);
    game.stage.backgroundColor = "3984db";
    game.physics.arcade.gravity.y = 1200;
    this.cursor = game.input.keyboard.createCursorKeys();

    this.mario = game.add.sprite((game.width / 2) + 77, game.height / 2, 'mario');
    this.mario.anchor.setTo(0.5,0.5);
    this.mario.scale.setTo(0.15,0.15);

    this.walls = game.add.group();
    this.walls.enableBody = true;
    this.walls.enableBody = true;

    this.spaceBar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

    this.downWallH = game.add.sprite(0, 540, 'WallH', 0, this.walls);
    this.UpWallH = game.add.sprite(0, 0, 'WallH', 0, this.walls);
    this.leftWallW = game.add.sprite(0,0, 'WallW', 0, this.walls);
    this.rightWallW = game.add.sprite(870,0, 'WallW', 0, this.walls);

    this.enemy = game.add.sprite(300,300, 'enemy');
    this.enemy.scale.setTo(0.2,0.2);

    game.physics.arcade.enable([this.mario, this.enemy]);
    this.walls.setAll('body.allowGravity', false);
    this.walls.setAll('body.immovable', true);

},

Use:

create: function() 
{
   ...
   this.mario.body.collideWorldBounds = true;
   ...
}

update: function() 
{   
    ...
    game.physics.arcade.collide(this.mario, this.walls);
    ...
}

But I think it's a little easier, editable and presentable using a Tile map. You only need a small software and a set of sprites to draw the map you want, there are many examples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM