Tilemap Collisions在Phaser中不起作用

时间:2015-05-10 18:08:23

标签: javascript html phaser-framework

我是Phaser的全新手,过去几天我一直有这个问题。基本上,我希望我的播放器在我的.json tilemap中与CollisionsLayer发生碰撞,但它不起作用,玩家也会直接通过。我已经尝试了多个版本的Phaser,但它们似乎都没有用。这是我的代码:

<!DOCTYPE html>
<html>
<head>
    <title>RPG Game</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.3.0/phaser.min.js"></script>
</head>
<body>
<script>
var game = new Phaser.Game(1000,600,Phaser.AUTO,'',{preload: preload, create: create, update: update});

function preload() {
    game.load.tilemap('level', 'assets/level1.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', 'assets/tiles.png');
    game.load.image('player', 'assets/star.png');
}

var map;
var backgroundLayer;
var collisionLayer;
var player;
var cursors;

function create() {
    game.physics.startSystem(Phaser.Physics.ARCADE);

    map = game.add.tilemap('level');
    map.addTilesetImage('tiles');

    backgroundLayer = map.createLayer("BackgroundLayer");
    collisionLayer = map.createLayer("CollisionLayer");
    map.setCollisionBetween(1,80);
    backgroundLayer.resizeWorld();

    player = game.add.sprite(200,200,'player');

    game.physics.enable(player);

    game.camera.follow(player);

    cursors = game.input.keyboard.createCursorKeys();

}

function update() {
    game.physics.arcade.collide(player, collisionLayer);

    player.body.velocity.x = 0;
    player.body.velocity.y = 0;

    if (cursors.right.isDown) {
        player.body.velocity.x = 200;
    }
    else if (cursors.left.isDown) {
        player.body.velocity.x = -200;
    }
    else if (cursors.up.isDown) {
        player.body.velocity.y = -200;
    }
    else if (cursors.down.isDown) {
        player.body.velocity.y = 200;
    }
}
</script>
</body>
</html>

tilemap确实加载了,我可以移动但是我不会碰撞碰撞层上的任何图块。提前谢谢。

1 个答案:

答案 0 :(得分:0)

您需要在create()函数中创建collisionLayer对象后添加此代码:

game.add.existing(collisionLayer);

或者map.createLayer构造函数中的代码:

this.game.add.existing(this);

但取决于你的代码。问题是你必须明确地告诉Phaser该对象正被添加到游戏中。