对于用Phaser制作的2D自上而下的游戏,玩家应该能够走过家具的前部和后部,这是我通过根据他们位置的y值来改变玩家的深度来实现的。然而,他们不应该通过向下移动家具(或向上通过它)来穿行家具。同样的y值(从高/低值到达家具也可以,然后他们就在家具前面走)时,他们也会被从侧面走到家具上。
我想这可以通过阻止播放器底部精灵和家具底部x像素之间的碰撞来实现。但我不知道如何做到这一点,在文档或网上找不到任何东西。我想我可以在播放器和家具的底部制作一些看不见的小东西,但是有更简单的方法来达到这个效果吗?
我有按深度分组的家具,每个深度都有如下代码:
create {
furnitureBack = this.physics.add.staticGroup();
furnitureBack.create(300, 80, 'furniture').setFrame(60).refreshBody();
furnitureBack.setDepth(2);
colliderBack = this.physics.add.collider(player, furnitureBack);
}
update {
colliderBack.active = false;
if (player.y > 75 && player.y < 85) {
colliderBack.active = true;
}
}这方面的问题是,只有当玩家进入75-85 y范围时,它才能工作,而不是已经与家具重叠。如果他们通过从底部向家具走来进入这个范围,那么对撞机就会变得活跃,但是玩家仍然能够通过这个项目。
发布于 2022-10-25 12:09:25
一个简单而快速的解决方案是使用相位物理学,只需改变body的大小。
在这里,与身体文件的链接 (您甚至可以用setCircle__,https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.Body.html#setCircle使碰撞盒变得又小又圆)
这里是一个工作演示:
(白色的盒子是玩家的棕色盒子,是一个盒子)
document.body.style = 'margin:0;';
var config = {
type: Phaser.AUTO,
width: 536/2,
height: 183/2,
zoom: 2,
physics: {
default: 'arcade',
arcade: {
debug: true
}
},
scene: {
create,
update
},
banner: false
};
function create () {
this.add.text(10, 10, 'Use cursor-keys to move')
this.player = this.add.rectangle(50, 80, 20, 40, 0xffffff)
.setOrigin(.5, 1)
.setDepth(1);
this.box = this.add.rectangle(100, 80, 20, 20, 0x933B26)
.setOrigin(.5, 1)
.setDepth(2);
this.physics.add.existing(this.box, true)
.body
//Setup physics-body size and position
.setSize(20, 10, false)
.setOffset(0, 10);
this.physics.add.existing(this.player)
.body
//Setup physics-body size and position
.setSize(20, 20, false)
.setOffset(0, 20);
this.keys = this.input.keyboard.createCursorKeys();
this.physics.add.collider(this.player, this.box)
}
function update(){
if(!this.player || !this.player.body){
return;
}
let body = this.player.body;
let speed = 50;
body.setVelocity(0, 0);
// Since the box depth is set to "2"
// you just have to alternate the depth of the player
let newDepth = (this.player.y > this.box.y) ? 3 : 1;
this.player.setDepth(newDepth);
if(this.keys.right.isDown){
body.setVelocity(speed, 0);
}
if(this.keys.left.isDown){
body.setVelocity(-speed, 0);
}
if(this.keys.up.isDown){
body.setVelocity(0, -speed);
}
if(this.keys.down.isDown){
body.setVelocity(0, speed);
}
}
new Phaser.Game(config);<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>
https://stackoverflow.com/questions/74193033
复制相似问题