虽然对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更新中调用类的更新函数,但这似乎让事情变得很生气(我也很有可能做错了).

推荐答案

默认情况下,不会调用update函数.您可以在场景的update函数中自己点击它,如下所示:

//...
// "args" parameter might not be needed
update(...args){
    this.GooBoi_1.update(...args);
    //...
}

如果您的精灵使用相位器Groups,则可以在属性为runChildUpdate的组中激活automatic update功能,并将其设置为true.

Demo for Phaser Groups:

document.body.style = 'margin:0;';

class CustomSprite extends Phaser.GameObjects.Sprite {
    constructor (scene, x, y) {
        super(scene, x, y);
        this.setTexture('red-box');
        this.setPosition(x, y);
        this.count = 0;
    }

    update (time, delta) {
      this.count++;
    }
}

class Example extends Phaser.Scene {
 
    create ()     {
        this.label = this.add.text(10,10, 'Update-function-call-counter')
            .setScale(1.5)
            .setOrigin(0)
            .setStyle({fontStyle: 'bold', fontFamily: 'Arial'});

        let graphics  = this.make.graphics();
        graphics.fillStyle(0xff0000);
        graphics.fillRect(0, 0, 10, 10);
        graphics.generateTexture('red-box', 10, 10);

     
        this.group = this.add.group({
            defaultKey: 'red-box',
            classType: CustomSprite,
            maxSize: 100,
            // activate update calls
            runChildUpdate: true,
        });


        this.group.create(Phaser.Math.RND.between(50, 200), 0);
        

    
    }

    update () {
        Phaser.Actions.IncY(this.group.getChildren(), 1);
        
        this.label.setText(`Sprite Update calls: ${this.group.getChildren()[0].count}`)

        this.group.children.iterate(child =>
        {
            if (child.y > config.height)
            {
                child.y = 0;
            }
        });
    }
}

var config = {
    type: Phaser.AUTO,
    width: 536,
    height: 183,
    physics: {
        default: 'arcade',
        arcade: {            
            gravity: { y: 100 },
            debug: true
        }
    },
    scene: Example,
}; 

new Phaser.Game(config);
<script src="//cdn.jsdelivr.net/npm/phaser/dist/phaser.min.js"></script>

Javascript相关问答推荐

无法通过从我的 node 后台创建的预签名URL将PDF上传到S3桶

在类型脚本中使用浏览器特定属性的正确方法是什么?

React redux状态未在React-Router库中呈现

Cookie中未保存会话数据

用JavaScript复制C#CRC 32生成器

从包含数百行的表中获取更改后的值(以表单形式发送到后端)的正确方法是什么?

是什么导致了这种奇怪的水平间距错误(?)当通过JavaScript将列表项元素追加到无序列表时,是否在按钮之间?

在浏览器中触发插入事件时检索编码值的能力

如何发送从REST Api收到的PNG数据响应

未加载css colored颜色 ,无法将div设置为可见和不可见

TinyMCE 6导致Data:Image对象通过提供的脚本过度上载

为什么可选参数的顺序会导致问题?

MongoDB受困于太多的数据

传递方法VS泛型对象VS事件特定对象

在Vercel中部署Next.js项目时获取`ReferenceError:未定义文档`

用于部分字符串的JavaScript数组搜索

我正在试着做一个TicTacToe Ai来和我玩.但是,我试着在第一个方块被点击时出现一个X,然后在第二个方块之后出现一个O

Django导入问题,无法导入我的应用程序,但我已在设置中安装了它

在Java脚本中录制视频后看不到曲目

顶点图使用组标签更新列之间的条宽