问题描述
我是 Phaserjs 的新手,我正在尝试制作一款射击游戏。我希望当子弹接触到平面 2 时,伤害函数会触发,即绿色平面。有人可以告诉我我在这里做错了什么吗? 这是我的代码:
var config = {
type: Phaser.AUTO,width: 800,height: 800,parent:"game",physics: {
default: 'arcade',arcade: {
debugging:true,gravity: {y: 0}
}
},scene: {
preload: preload,create: create,update: update
}
};
var plane1
var plane2
var hp;
var botHp;
function preload (){
this.load.setBaseURL('https://shoot.abaanshanid.repl.co/assets');
this.load.image("bg","bg.jpg");
this.load.image("plane1","plane1.png");
this.load.image("plane2","plane2.png");
this.load.image("bullet","bullet.png");
}
function create (){
this.background = this.add.image(400,400,"bg");
plane1 = this.physics.add.sprite(400,700,"plane1");
plane2 = this.physics.add.sprite(400,100,"plane2");
plane1.enableBody = true;
}
function update(){
keys = this.input.keyboard.createCursorKeys();
if (keys.left.isDown) {
plane1.x = plane1.x - 7.5;
}else if(keys.right.isDown){
plane1.x = plane1.x + 7.5;
}else if(keys.space.isDown){
var bullet = this.physics.add.sprite(plane1.x,600,"bullet");
bullet.enableBody = true;
setInterval(function(){
bullet.y = bullet.y - 25;
},50);
this.physics.overlap(bullet,plane2,this.damage);
}
}
function damage(){
console.log("less HP")
}
var game = new Phaser.Game(config);
<!DOCTYPE html>
<html>
<head>
<Meta charset="utf-8">
<Meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.54.0/phaser.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.54.0/phaser-arcade-physics.min.js"></script>
</head>
<body>
<div id="game"></div>
<script defer src="script.js"></script>
</body>
</html>
如果需要,这里是游戏的链接https://shoot.abaanshanid.repl.co/
解决方法
这有效:
this.physics.add.overlap(bullet,plane2,damage);
但它有点滞后。我试图在撞击时摧毁子弹,我还发现:
this.physics.add.collider(bullet,damage);