Phaser Specific Tile“Collision”

时间:2016-12-30 19:30:54

标签: javascript phaser-framework

Simple Maze, created with Tiled

所以,最初的计划是通过Tilemaps创建一个迷宫,如果玩家(从左上角开始)触及右下角的东西,则应加载下一个级别。 到目前为止,我所做的是创造墙壁之间的碰撞(黑色),现在我真的不知道如何从右下方的东西“碰撞”开始。

这是我目前的代码

var labGame = labGame || {};

labGame.Game = function() {};

var map;
var layer;

var player;
var controls = {};
var playerSpeed = 350;
var jumpTimer = 0;

labGame.Game.prototype = {
  create: function() {
    map = this.add.tilemap("map", 64, 64);
    map.addTilesetImage("tileset");
    layer = map.createLayer(0);
    layer.resizeWorld();

    map.setCollisionBetween(2, 2);

    player = this.add.sprite(64, 384, "player");
    player.anchor.setTo(0,5, 0,5);
    this.physics.arcade.enable(player);
    this.camera.follow(player);
    player.body.collideWorldBounds = true;

    controls = {
      right: this.input.keyboard.addKey(Phaser.Keyboard.D),
      left: this.input.keyboard.addKey(Phaser.Keyboard.A),
      up: this.input.keyboard.addKey(Phaser.Keyboard.W),
      down: this.input.keyboard.addKey(Phaser.Keyboard.S),
    }
  },

  update: function() {
    player.body.velocity.x = 0;
    player.body.velocity.y = 0;
    this.physics.arcade.collide(player, layer);
    if(controls.right.isDown) {
      player.body.velocity.x += playerSpeed;
    }
    if(controls.left.isDown) {
      player.body.velocity.x -= playerSpeed;
    }
    if(controls.up.isDown) {
      player.body.velocity.y -= playerSpeed;
    }
    if(controls.down.isDown) {
      player.body.velocity.y += playerSpeed;
    }
  }
};

(有人在相位论坛上写道,Boot.js等事情后来更容易“处理”左右,所以我开始使用它了)

1 个答案:

答案 0 :(得分:0)

您可以使用Tiled对象图层。

在Tiled中,在显示图层的面板下,单击新图层按钮,但选择新对象图层。

然后确保选中图层,并将右下方的图块放在地图上。现在,所有的墙都在瓷砖层上,并且在对象层上会有一个瓷砖。

转到代码,你会添加如下内容:

    //Create Exit
    //Add the exit tile to a group
    exit = this.add.group();

        this.map.createFromObjects('Objects', 28, 'door', 0, true, false, exit);
        //Apply properties to the exit
        exit.forEach(function(exit) {
            exit.body.allowGravity = false;
            exit.body.immovable = true;
        },this);

this.map.createFromObjects参数是: createFromObjects(name,gid,key,frame,exists,autoCull,group,CustomClass,adjustY)

您可以在文档here.

中查看更多相关信息

现在插入了对象,就像在更新中调用碰撞函数一样简单。所以像:

    update: function() {
        this.physics.arcade.overlap(player,exit, this.endLevel)
    }
    endLevel: function(player,exit) {
        //Code for moving to next Level
    }