proyecto26 / ion-phaser

A web component to use Phaser Framework with Angular, React, Vue, etc 🎮
https://market.ionicframework.com/plugins/ionphaser
MIT License
251 stars 39 forks source link

Calling some function from outside #3

Closed r0m1n closed 4 years ago

r0m1n commented 4 years ago

Hello. How could I call some function inside a scene from a component itself? Tried to do something like "this.game.instance.someMethod()" or " this.game.instance.scene.someMethod()" but it gives me error "someMethod() is not a function". full code:

` import { Component, OnInit } from '@angular/core'; import * as Phaser from 'phaser';

@Component({ selector: 'app-dancefloor', templateUrl: './dancefloor.component.html', styleUrls: ['./dancefloor.component.scss'] }) export class DancefloorComponent implements OnInit {

map = new Array(); initialize = false; game = { width: "50%", height: "50%", type: Phaser.AUTO, scene: { init: function() { this.cameras.main.setBackgroundColor('#24252A') }, create: function() { this.helloWorld = this.add.text( this.cameras.main.centerX, this.cameras.main.centerY, "Hello World", { font: "40px Arial", fill: "#ffffff" } ); this.helloWorld.setOrigin(0.5); }, update: function() { this.helloWorld.angle += 1; }, someMethod: function () { this.helloWorld.angle -= 180; } }, instance: null }

constructor(){ }

initializeGame() { this.initialize = true;

setTimeout(() => {
  console.log(this.game.instance);
  this.game.instance.scene.someMethod();
}, 3000)

}

ngOnInit() { this.initializeGame();

for(let i=0; i<3; i++){
  let newArr=[];
  for(let j=0;j<5;j++)
  {
    newArr.push(this.generateNewTile());
    console.log('');
  }
  this.map.push(newArr);
  console.log('');
}

}

generateNewTile(isHero:boolean = false) { return new Tile('1', isHero, this.getRandomInt(1,4)); }

getRandomInt(min, max) { return Math.floor(Math.random() * (max - min)) + min; }

}

class Tile { type:string; isHero:boolean; score: number;

constructor(type, isHero,collectableItem){ this.type= type; this.isHero = isHero; this.score = collectableItem; }

} ``

jdnichollsc commented 4 years ago

Let me check 👍

jdnichollsc commented 4 years ago

It looks like you can't do that because scene property of the game instance is a SceneManager in Phaser 3

jdnichollsc commented 4 years ago

I'm learning Phaser 3, in the meantime you can do this => https://github.com/proyecto26/ion-phaser/blob/master/demo-angular/src/app/app.component.ts#L54

r0m1n commented 4 years ago

I'm learning Phaser 3, in the meantime you can do this => https://github.com/proyecto26/ion-phaser/blob/master/demo-angular/src/app/app.component.ts#L54

thanks!

jdnichollsc commented 4 years ago

I'm reopening the issue because I have the same question 😅

r0m1n commented 4 years ago

What wrong with this.game.instance.scene.getAt(0).someMethod()? It works for me at least.

jdnichollsc commented 4 years ago

woww, really? I was debugging but maybe I didn't see because TypeScript haha

jdnichollsc commented 4 years ago

@r0m1n how are you adding that method? I have an error warning like this: ERROR TypeError: scene. someMethod is not a function

r0m1n commented 4 years ago

I have created new class for the scene that extends PhaserScene. Something like this:

class TestScene extends Phaser.Scene{

  constructor() {
    super({key: 'TestScene'});
  }

    preload() {....
}
someMethod(param){
}

and in the config I just passing it to the "scene" field

 game = {
    width: "100%",
    height: "50%",
    type: Phaser.AUTO,
    scene: [TestScene],
    physics: {
      default: 'arcade',
      arcade: {
        gravity: { y: 200 }
      }},
    instance: null
  }

not exactly sure is it necessary to create a new class for the scene to make thing works, but for me it seems like more neat way to do this then just writing full scene code into the config.

jdnichollsc commented 4 years ago

Awesome and yes, you're right! <3

jdnichollsc commented 4 years ago

yeah, it's working here https://github.com/proyecto26/ion-phaser/blob/master/demo-angular/src/app/app.component.ts#L91