Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Phaser 3中每分钟运行一个功能?_Javascript_Phaser Framework - Fatal编程技术网

Javascript 如何在Phaser 3中每分钟运行一个功能?

Javascript 如何在Phaser 3中每分钟运行一个功能?,javascript,phaser-framework,Javascript,Phaser Framework,我正在用JavaScript制作一个游戏,但是在类power-up中,我想每分钟创建一个power-up。问题是,它在类的update方法中被调用,并且在主更新中被调用每个帧,因此一旦它运行,它不会再次停止并保持循环。我怎样才能让它停下来又重新开始呢 我试着得到这样的秒数: 方法创建通电: let time = new Date.getSeconds(); if (time >=59) {create power up} 通电类的方法更新: this.createpowerup();

我正在用JavaScript制作一个游戏,但是在类power-up中,我想每分钟创建一个power-up。问题是,它在类的
update
方法中被调用,并且在主更新中被调用每个帧,因此一旦它运行,它不会再次停止并保持循环。我怎样才能让它停下来又重新开始呢

我试着得到这样的秒数:

方法创建通电:

let time = new Date.getSeconds();
if (time >=59) {create power up}
通电类的方法更新:

this.createpowerup();
class Power_ups extends Phaser.GameObjects.Sprite {
constructor(scene) {
    super(scene, scene.player_spaceship.x, scene.player_spaceship.y, "power_up");
    //this.hi();
}
hi() {
    setInterval(this.hola,5000);
}
hola() {
    console.log("hola");
}
update() {
    this.hi();
}
}
现在,在主类中,我调用了我拥有的类中的所有更新方法:

update() {
    this.player_spaceship_object.update();    
    this.enemy_spaceship_object.update();
    this.power_up_object.update();
    this.movebackground();
}
所以每一帧它都会调用它们,但问题是,当通电结束时为零,它会创建大量的通电,直到它开始1,开始1需要毫秒,如何在循环中实现这一点

通电等级:

this.createpowerup();
class Power_ups extends Phaser.GameObjects.Sprite {
constructor(scene) {
    super(scene, scene.player_spaceship.x, scene.player_spaceship.y, "power_up");
    //this.hi();
}
hi() {
    setInterval(this.hola,5000);
}
hola() {
    console.log("hola");
}
update() {
    this.hi();
}
}
Main类,这是进入页面后运行的类:

class Scene2 extends Phaser.Scene {
constructor() {
    super("Scene2");
}
create() {
    this.scoreText;
    this.timedEvent;
    this.background = this.add.tileSprite(0,0,globalThis.config.width,globalThis.config.height,"space");
    this.background.setOrigin(0,0);
    this.player_spaceship = this.physics.add.sprite(globalThis.config.width/2-50,globalThis.config.height/2,"player_spaceship");
    this.player_spaceship_object = new Player_spaceship(this);
    this.enemy_spaceship_group = this.physics.add.group();
    this.enemy_spaceship_object = new Enemy_spaceship(this);
    this.physics.add.collider(this.player_spaceship,this.enemy_spaceship_group,this.collision_player_enemy,null,this);
    this.power_up;
    this.power_up_object = new Power_ups(this);
    this.power_up_object.createpowerUp();
    
}
collision_player_enemy(player, enemy) { //parameters tells the function the objects that will collide
    this.player_spaceship_object.damage();
    this.enemy_spaceship_object.resetShip(enemy);
}
movebackground(bool = "") {
    this.background.tilePositionY = (bool === true) ? this.background.tilePositionY += 1 : this.background.tilePositionY -= 1;
}
update() {
    this.player_spaceship_object.update();    
    this.enemy_spaceship_object.update();
    this.power_up_object.update();
    this.movebackground();
}
}

您需要使用
setInterval
, 请看示例,希望它对您有所帮助

函数更新(){
log(“您的代码”);
}

设置间隔(更新,2000年)我强烈建议使用requestAnimationFrame,以便在UI和逻辑之间有更好的时间安排

function powerUpEveryMinute() {
  // powerUp!!!!
  setTimeout(() => {
     requestAnimationFrame(powerUpEveryMinute);
  }, 60000);
}
requestAnimationFrame(powerUpEveryMinute);
这将调用animationFrame API。首先,它运行通电,等待1分钟,然后使用相同的函数再次调用animationFrame API,并在第一次通电后1分钟调用通电


让我知道这是否有效

您有几个与相位器3的选项

首先,您当然可以为场景中的内置
update()
功能添加一些逻辑。但是,如果这样做,则需要手动跟踪上次添加加电的时间以及需要添加另一个加电的时间

例如,可以将特性添加到场景中,以跟踪下一次应添加通电的时间,然后如果当前时间等于或大于该时间,则创建通电,然后更新特性值(当前时间+1分钟)

更简单的方法可能是使用Phaser 3的内置计时器功能

在场景中,添加要跟踪的属性,因为这将允许您稍后暂停或停止该属性

接下来,在场景的
create()
中创建计时器事件

类型脚本示例:

export default class MainGame extends Phaser.Scene {
    // Store the timer so we can access it later.
    private triggerTimer: Phaser.Time.TimerEvent;

    public create(): void {
        // ...
        this.triggerTimer = this.time.addEvent({
            callback: this.timerEvent,
            callbackScope: this,
            delay: 60000, // 1000 = 1 second
            loop: true
        });
        // ...
    }

    public timerEvent(): void {
        console.log('timerEvent');
        // Create your new object here.
    }
}
无论哪种方式,在游戏精灵中,您都应该删除
update()
中触发
hi()
的功能,因为这会有效地导致精灵在每次场景(以及每个精灵)更新时创建新的计时器


如果希望事件在创建精灵后x秒触发,则应在
构造函数中定义计时器(通过
setInterval
或相位器计时器)。

try setInterval,try it not work either它是如何失败的?你有错误吗?请张贴您的代码“也不起作用”。。。这是一个正确的工具,所以告诉我们您实际尝试了什么,以及遇到的错误,如果您阅读文档,也会看到有一个clearInterval()方法。还可以看看如何创建一个循环,只要取Date.now()的增量和msI know中最后保存的时间以及setInterval就可以了,但问题是它在一个无限循环中,类似这样的情况:当(true){run()}run(){setInterval(console.log(“hi”),5000}运行时,它将等待5秒,但是一旦在打印嗨,它不会停止你的完整代码块与循环更好地理解张贴:),希望能帮助我!这是使用定时器的“移相方式”。易于使用并按预期工作。