ion-phaser icon indicating copy to clipboard operation
ion-phaser copied to clipboard

Call an Angular function from inside Phaser scene

Open obrador opened this issue 5 years ago • 6 comments
trafficstars

Hi, I need to call an outside Angular function from inside a phaser scene. Can you please show me the way? I found this [https://www.html5gamedevs.com/topic/35570-calling-an-angular-function-from-within-phaser-3-scene/] :

But nothing said there have worked. It's claimed that it can be done with ion-phaser.

Thanks.

obrador avatar Mar 16 '20 21:03 obrador

If you assign the phaser code from an angular controller, you should be able to call methods of that controller, example:

import { Component } from '@angular/core'
import * as Phaser from 'phaser'
import { ApiService } from '../services';

@Component({
  selector: 'my-component',
  templateUrl: './my-component.html'
})
export class MyComponent {
  game: Phaser.Game
  initialize = false

  constructor(
    private readonly api: ApiService
  ){}

  initializeGame() {
    const context = this
    context.initialize = true
    context.game  = {
      width: "100%",
      height: "100%",
      type: Phaser.AUTO,
      scene: {
        create() {
          this.helloWorld = this.add.text(0, 0, 'Hello World')
        }
        update () {
          this.helloWorld.angle += 1;
        }
        saveGame() {
          const { angle } = this.helloWorld
          context.api.saveAngle(angle)
        }
      }
    }
  }
}

This is one option, exist a lot of alternatives (injecting the component context, using event emitters instead, etc) 👍 Let me know what you think

jdnichollsc avatar Mar 30 '20 00:03 jdnichollsc

Can you make a example of how to inject the component context to a scene? Injecting Context in the scene in diffirent file.

lalalalaluk avatar Apr 09 '20 12:04 lalalalaluk

Like this?

  • boot-scene.ts
import * as Phaser from 'phaser';
import { FirstScene } from 'first-scene.ts';
import { SecondScene } from 'second-scene.ts';

// Angular context
let context: any;

const SCENES = {
  FIRST: 'FirstScene',
  SECOND: 'SecondScene'
}
class BootScene extends Phaser.Scene {
  create() {
    this.scene.add(SCENES.FIRST, FirstScene, true);
    this.scene.add(SCENES.SECOND, SecondScene, false);

    this.scene.run(SCENES.FIRST);
    // Calling Angular functions
    if (context) context.initialized();
  }
}

// Inject Angular context and return scene
export const makeBootScene = (ctx) => {
  context = ctx;
  return BootScene;
}
  • app.component.ts
import { Component } from '@angular/core';
import * as Phaser from 'phaser';
import { makeBootScene } from './boot-scene.ts';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  game = {
    width: "100%",
    height: "100%",
    type: Phaser.AUTO,
    scene: makeBootScene(this)
  }

  initialized() {
    console.log('Boot Scene initialized!');
  }
}

jdnichollsc avatar Apr 09 '20 19:04 jdnichollsc

you saved my day! it works fine . thank you!

lalalalaluk avatar Apr 10 '20 17:04 lalalalaluk

@jdnichollsc Thanks man

shashankpenumatcha avatar Mar 15 '21 17:03 shashankpenumatcha

haha you're welcome! 😁

jdnichollsc avatar Mar 16 '21 05:03 jdnichollsc