gpt4 book ai didi

javascript - Phaser 3 碰撞器剪辑

转载 作者:行者123 更新时间:2023-11-28 03:41:25 25 4
gpt4 key购买 nike

所以我对 Phaser 有点陌生,我发现很难找到关于这个主题的任何好的信息。我的问题是我可以将碰撞器推向彼此(参见 gif)。

https://imgur.com/KI1RJQA.gif “我的意思是”

所以现在我有一个系统,我可以跟踪玩家的最后一个有效位置,如果它们发生碰撞,我会将它们设置回原来的位置。但我认为必须有更好的方法来做到这一点,所以这就是我来这里的原因。

let TopWall = this.add.tileSprite(400, 100, 1800, 100, 'wall');
this.physics.add.existing(TopWall);

this.walls = this.add.group();
this.walls.addMultiple([TopWall]);

this.walls.children.iterate(function(wall) {
wall.body.immovable = true;
})

this.player = this.physics.add.sprite(300, 300, 'idel', 0);

this.physics.add.collider(this.player, this.walls,(player, wall) => {
// this.player.x = this.player.lastPos.x;
// this.player.y = this.player.lastPos.y;
})

顺便说一句,我使用街机物理。

最佳答案

var config = {
type: Phaser.AUTO,
parent: 'phaser-example',
width: 800,
height: 600, loader: {
baseURL: 'https://raw.githubusercontent.com/nazimboudeffa/assets/master/',
crossOrigin: 'anonymous'
},
scene: {
preload: preload,
create: create,
update: update
},
physics: {
default: 'arcade'
}
};

var game = new Phaser.Game(config);

var topWall;
var player;
var alien1;

function preload ()
{
this.load.image('dude', 'sprites/phaser-dude.png');
this.load.image('alien1', 'sprites/phaser-alien.png');
this.load.image('wall', 'sprites/block.png');
}

function create ()
{
topWall = this.add.tileSprite(0, 0, 2000, 95, 'wall');
this.physics.add.existing(topWall);
topWall.body.immovable = true;

player = this.physics.add.sprite(300, 100, 'dude');
alien1 = this.physics.add.sprite(400, 100, 'alien1');

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

player.body.setCollideWorldBounds(true);
this.physics.add.collider(player, topWall);
this.physics.add.collider(player, alien1);

}

function update ()
{
if (cursors.left.isDown)
{
player.setVelocityX(-160);
}
else if (cursors.right.isDown)
{
player.setVelocityX(160);
}
else if (cursors.down.isDown)
{
player.setVelocityY(160);

}
else if (cursors.up.isDown)
{
player.setVelocityY(-160);
}
}

function myCollisionHandler ()
{
//console.log("collision")
}
<script src="//cdn.jsdelivr.net/npm/phaser@3.18.1/dist/phaser.min.js"></script>

关于javascript - Phaser 3 碰撞器剪辑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57273036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com