【问题标题】:ANGULAR AND PHASER CE How call Angular function in Phaser CE update functionANGULAR AND PHASER CE 如何在 Phaser CE 更新函数中调用 Angular 函数
【发布时间】:2020-11-11 22:20:23
【问题描述】:

我需要调用一个函数来抛出一个用 NgbModal 创建的模态。在 Phaser CE 管理的 update() 函数中,当条件为真时,应用程序崩溃抛出:ERROR TypeError: this.gameOver is not a function I'已经测试了所有。 gameOver() 函数退出类,但构造函数中创建的modalService未定义......

有人可以帮助我吗?

mit ts 文件:

@ViewChild('msgGameOver') public modalmsgGameOver:TemplateRef<any>;

constructor( public modalService: NgbModal, private router:Router, private storage:Storage, 
private endpoint:EndpointsService){}

ngOnInit(){
  
game = new Phaser.Game(
  window.innerWidth, window.innerHeight,
  Phaser.AUTO, 
  'garrioCatapulta',
  {preload: this.preload, create: this.create, update: this.update, render: this.render},
  true
);

that = Object.create(this.constructor.prototype);    
}

gameOver() {
this.msgGameOver = "lo sentimos, no has acertado la casilla ganadora"
this.headerGameOver="¡Has perdido!";
this.iconGameOver = "assets/error.svg";
this.modalService.open(this.modalmsgGameOver);
}

preload(){

  game.load.image('roca', 'assets/aislada.svg');
  game.load.image('rebote','assets/rebote.svg');
  game.load.image('catapulta-dispara','assets/componentes_juego_catapulta_disparo.svg');
  game.load.image('colina', 'assets/derecha.svg');
  game.load.image('castillo', 'assets/izquierda_castillo.svg');
  
}
create(){

  game.scale.scaleMode = Phaser.ScaleManager.EXACT_FIT;     
  game.scale.pageAlignHorizontally = true;      
  game.scale.pageAlignVertically = true;        

  game.scale.refresh();

  game.physics.startSystem(Phaser.Physics.ARCADE);

  roca = game.add.sprite(200, 500, 'roca');
  castillo = game.add.sprite(0,350,'castillo');
  catapulta=game.add.sprite(300,525,'catapulta-dispara');
  
  colina = game.add.sprite(280,565,'colina');
  colina.alignIn(game.world.bounds, Phaser.BOTTOM_RIGHT);
  catapulta.alignTo(colina,Phaser.TOP_LEFT);
  roca.alignTo(catapulta,Phaser.TOP_RIGHT);

  game.physics.enable(roca, Phaser.Physics.ARCADE);
  game.physics.enable(colina, Phaser.Physics.ARCADE);
  game.physics.enable(catapulta, Phaser.Physics.ARCADE);
  game.physics.enable(castillo, Phaser.Physics.ARCADE);

  obstaculos = game.add.group()
  obstaculos.enableBody = true;

  colina.body.immovable = true;
  castillo.body.immovable = true;

  colina.body.immovable = true;
  castillo.body.immovable = true;
  catapulta.body.immovable = true;

  for (var i = 0; i < 3; i++)
  {

      var s = obstaculos.create(game.rnd.integerInRange(75, 270),game.rnd.integerInRange(250, 500), 'rebote');
      s.body.collideWorldBounds = true;
      s.body.immovable = true;
      s.body.bounce.set(0.2);
  }

}



update(){
  
  if(game.physics.arcade.collide(roca, obstaculos)){
    console.log("choca roca")
  }
  
  if(game.physics.arcade.collide(roca, castillo)){
    c++;
    if(c >= 20){
      roca.body.stop();
      console.log("A pis pos");
      game.paused=true;
      this.gameOver();
    }
  }

  if(game.physics.arcade.collide(roca, colina)){
    co++;
    if(co >= 20){
      roca.body.stop();
      console.log("A pis pos");
      game.paused=true;
      this.gameOver();
    }
  };

  if(roca.body.onFloor()){
    i++;
    if(i >= 20){
      roca.body.stop();
      console.log("A pis pos");
      game.paused=true;

      this.gameOver();
    }
  }

}

render() { 

}

}

【问题讨论】:

  • 你需要展示更多代码,很难判断你的 dom 是否准备好了,或者它是否与 promise 相关。还是与 Phaser 生命周期有关。你的 this.modalService 包含什么?它是否准备好在调用 create() 时使用,或者它仍然被 Angular 延迟加载?还有你的 modalService.open 做什么?在调用 GameOver() 时,模态 DOM 是否准备好被 Angular 调用?还是等待 modalmsgGameOver 解决的承诺?

标签: angular function phaser-framework


【解决方案1】:

在gameOver()中,重写:

this.modalService.open(this.modalmsgGameOver);

that.modalService.open(this.modalmsgGameOver);

【讨论】:

  • 感谢您的贡献。当您提供代码时,您可以在 stackOverflow 中编辑时使用代码示例、功能,以便您的代码看起来比使用斜体格式更好。要使用代码示例,请通过选择源代码单击编辑器中的 {} 图标,以便 StackOverflow 自动检测和格式化。谢谢。
猜你喜欢
  • 2019-03-16
  • 2015-07-05
  • 2015-07-15
  • 2022-09-29
  • 1970-01-01
  • 2017-09-19
  • 1970-01-01
  • 2016-12-21
  • 1970-01-01
相关资源
最近更新 更多