Timer not working inside function in Phaser 3

[亡魂溺海] 提交于 2020-01-06 06:48:20

问题


I have a spawn function which performs some task. Before the function returns, I would like to delay some another function call.

I tried using time.addEvent but with no luck as it does not seem to work within the spawn function. However the timer works perfectly inside the create function.

My code so far:

create(){
  newMethod = spawn.bind(this);
  newMethod();
}

function spawn(){
  //do stuff
  timer = this.time.addEvent({
    delay: 3000,
    callback: functionDelay,
    loop: false
  });
}

function functionDelay(){
  console.log("Works!");
}

回答1:


var delayText;
var delayedEvent;

class myScene extends Phaser.Scene {

    constructor (config)
    {
      super(config);
    }

    preload ()
    {
      this.load.image('dude', 'sprites/phaser-dude.png')
    }
   
    create () 
    {
      delayText = this.add.text(50, 50);
      delayedEvent = this.time.delayedCall(3000, this.spawn, [], this);
    }
    
    spawn()
    {
      var sprite = this.add.sprite(300, 50, 'dude')
    }
    
    update()
    {
      delayText.setText('Event.progress: ' + delayedEvent.getProgress().toString().substr(0, 4));
    }
}

var config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    loader: {
      baseURL: 'https://cdn.jsdelivr.net/gh/samme/phaser-examples-assets@v2.0.0/assets/',
      crossOrigin: 'anonymous'
    },
    width: 800,
    height: 600
};

var game = new Phaser.Game(config);

game.scene.add('myScene', myScene, true);
<script src="//cdn.jsdelivr.net/npm/phaser@3.17.0/dist/phaser.min.js"></script>


来源:https://stackoverflow.com/questions/56154888/timer-not-working-inside-function-in-phaser-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!