Phaser 3 中 setCollideWorldBounds 和重叠的问题
2020-09-20
564
我在使用 Phaser 3 时遇到了问题。
当我设置 CollideWorldBounds 时,它给我错误“无法读取 null 的属性‘setCollideWorldBounds’”,并且重叠功能不起作用。最奇怪的是,在我的游戏配置中,我的调试选项为 true,但仅在这个精灵中它不可见。
游戏配置
export const config = {
type: Phaser.AUTO,
backgroundColor: "#125555",
width: 1200,
height: 900,
scene: Battle,
physics: {
default: "arcade",
arcade: {
debug: true,
},
},
};
const game = new Phaser.Game(config);
Player.ts
export class Player extends Phaser.Physics.Arcade.Sprite {
constructor(scene: Phaser.Scene, texture: string) {
super(scene, config.width / 2, config.height - 150, texture);
scene.add.existing(this);
}
....
我的场景
export class Battle extends Phaser.Scene {
private player: Player;
private enemy1: Enemy;
private enemy2: Enemy;
private enemy3: Enemy;
constructor() {
super("playGame");
}
preload() {
this.load.image("background", "assets/space.png");
this.load.image("player", "assets/playerShip.png");
this.load.image("bullet", "assets/laserBullet.png");
this.load.image("enemy1", "assets/enemy1.png");
this.load.image("enemy2", "assets/enemy2.png");
this.load.image("enemy3", "assets/enemy3.png");
}
create() {
this.player = new Player(this, "player");
this.player.setScale(0.09);
this.player.setCollideWorldBounds(true);
this.enemy1 = new Enemy(this, "enemy1");
this.enemy2 = new Enemy(this, "enemy2");
this.enemy3 = new Enemy(this, "enemy3");
this.enemies = this.physics.add.group();
this.enemies.add(this.enemy1);
this.enemies.add(this.enemy2);
this.enemies.add(this.enemy3);
this.playerCollision = this.physics.add.overlap(
this.player,
this.enemies,
this.hitPlayer,
null,
this
);
hitPlayer(player: Phaser.Physics.Arcade.Sprite, enemies) {
this.resetEnemy(enemies);
}
resetEnemy(enemy) {
enemy.y = 0;
let randomX = Phaser.Math.Between(0, config.width);
enemy.x = randomX;
}
....
1个回答
您需要将玩家添加到场景的物理中。 这应该可以解决问题。
export class Player extends Phaser.Physics.Arcade.Sprite {
constructor(scene: Phaser.Scene, texture: string) {
super(scene, config.width / 2, config.height - 150, texture);
scene.add.existing(this);
scene.physics.add.existing(this);
}
Ahmed Tounsi
2020-09-21