[英]Phaser Specific Tile “Collision”
因此,最初的计划是,如果玩家(从左上角开始)触摸右下角的东西,则通过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等东西以后更容易“处理”,所以我已经开始使用它了。)
您可以使用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(名称,gid,键,框架,存在,自动剔除,组,CustomClass,adjustY)
现在已经插入了对象,就像在更新中调用碰撞函数一样容易。 所以像这样:
update: function() {
this.physics.arcade.overlap(player,exit, this.endLevel)
}
endLevel: function(player,exit) {
//Code for moving to next Level
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.