Tilemap碰撞在Phaser中不起作用 [英] Tilemap Collisions don't work in Phaser
问题描述
<!DOCTYPE html>
< html>
< head>
< title> RPG游戏< / 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});
函数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游标;
函数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>
瓷砖地图确实会加载,我可以移动,但我不会与任何瓷砖相撞碰撞层。
在create()函数中创建collisionLayer对象后,您需要添加此代码:
game.add.existing(collisionLayer);
或者map.createLayer构造函数中的这段代码:
this.game.add.existing(this);
但取决于您的代码。问题是你必须明确告诉Phaser该对象正在被添加到游戏中。
I'm a complete newbie in Phaser, and I've been having this problem for the last couple of days. Basically, I want my player to collide with the CollisionsLayer in my .json tilemap, but it doesn't work and the player goes right through. I have tried multiple versions of Phaser and none of them seem to work. Here's my code:
<!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>
The tilemap does load and I am able to move but I just don't collide with any tiles on the collision layer. Thanks in advance.
You need to add this code after creating the collisionLayer object in the create() function:
game.add.existing(collisionLayer);
Or this code in the map.createLayer constructor:
this.game.add.existing(this);
But depends on your code. The thing is that you have to explicitly tell Phaser that the object is being added to the game.
这篇关于Tilemap碰撞在Phaser中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!