虽然对Phaser和Java脚本来说还很陌生,但我正在try 创建几个处理玩家、敌人和硬币的类,每当我创建对象时,它们的更新函数似乎从来没有被调用过,尽管我的理解是它们应该被调用(尽管很差)
下面是我的createGooBoi类:
export default class createGooBoi extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
super(scene, x, y, "GooBoi");
this.scene.add.existing(this);
this.setScale(2);
this.scene.physics.world.enable(this);
this.scene.add.existing(this);
this.body.setGravityY(660);
this.body.setCollideWorldBounds(true);
}
create() {
console.log("Create GooBoi");
this.anims.create({
key: "idle",
frames: [
{ key: "GooBoi", frame: 0 },
{ key: "GooBoi", frame: 1 },
{ key: "GooBoi", frame: 2 },
{ key: "GooBoi", frame: 3 },
],
frameRate: 8,
repeat: -1,
});
}
update() {
console.log("Update GooBoi");
this.body.play("idle");
}
}
export default class createGooBoi extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
super(scene, x, y, "GooBoi");
this.scene.add.existing(this);
this.setScale(2);
this.scene.physics.world.enable(this);
this.scene.add.existing(this);
this.body.setGravityY(660);
this.body.setCollideWorldBounds(true);
}
create() {
console.log("Create GooBoi");
this.anims.create({
key: "idle",
frames: [
{ key: "GooBoi", frame: 0 },
{ key: "GooBoi", frame: 1 },
{ key: "GooBoi", frame: 2 },
{ key: "GooBoi", frame: 3 },
],
frameRate: 8,
repeat: -1,
});
}
update() {
console.log("Update GooBoi");
this.body.play("idle");
}
}
这是我的1级:
import createGooBoi from "../Main/GooBoi.js";
import createHal from "../Main/Hal.js";
//----Varibles----//
var GooBoi_1;
var Coin_1;
var Coin_2;
var Hal;
export class Level_1 extends Phaser.Scene {
constructor() {
super({ key: "Level_1" });
}
create() {
this.cameras.main.fadeIn(1000, 0, 0, 0);
//----Level----//
this.add.image(480, 200, "Level_1_Background");
var platform = this.physics.add.staticGroup();
var floor = this.physics.add.staticGroup();
//let PlatformSmall_1 = platform.create(300, 300, 'PlatformSmall');
let PlatformLong_1 = platform.create(475, 250, "PlatformLong");
PlatformLong_1.setScale(1.5);
//PlatformSmall_1.setScale(1.2);
floor.create(480, 440, "Floor").refreshBody();
//----Hal----//
this.Hal = new createHal(this, 200, 200);
Hal;
//----GooBoi----//
this.GooBoi_1 = new createGooBoi(this, 500, 200);
GooBoi_1;
//----Coin's----//
this.Coin_1 = new createCoin(this, 500, 400);
this.Coin_2 = new createCoin(this, 400, 400);
Coin_1;
Coin_2;
//----Collision Mesh----//
this.physics.add.collider(this.Hal, floor);
this.physics.add.collider(this.Hal, platform);
}
update() {}
}
如果这一点很重要,index.html:
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Attack of The GooBoi's</title>
<script src="https://cdn.jsdelivr.net/npm/phaser@3.60.0/dist/phaser.js"></script>
<link rel="stylesheet" href="main.css" />
</head>
<body>
<div id="Game">
<script type="module">
import { Boot } from "./scenes/Boot.js";
import { MainMenu } from "./scenes/MainMenu.js";
import { Level_1 } from "./scenes/Level_1.js";
import { Level_2 } from "./scenes/Level_2.js";
import { Level_3 } from "./scenes/Level_3.js";
const config = {
type: Phaser.AUTO,
width: 960,
height: 440,
scene: [Boot, MainMenu, Level_1, Level_2, Level_3],
pixelArt: true,
physics: {
default: "arcade",
arcade: { debug: true },
},
};
new Phaser.Game(config);
</script>
</div>
</body>
</html>
不知道我哪里错了,但如果有人能解释并帮助我理解为什么我所做的事情不起作用,我会很高兴的!
我试图在Level_1更新中调用类的更新函数,但这似乎让事情变得很生气(我也很有可能做错了).