0

I am using ionic framework to develop my Game.

I used Phaser as a plugin. Everything is going well but I am not able to use any external variable inside my game.

For example the highest score. I want to show it in my app menu.

I already tried:

this.game.global={ ... }; 

By refering this : http://www.html5gamedevs.com/topic/21143-global-variables/ but its showing compiletion error.

Please help

Edit: sample Code:

 import { Component } from '@angular/core';
    import { IonicPage, NavController, NavParams,Platform } from 'ionic-angular';

    import "pixi";
    import "p2";
    import * as Phaser from "phaser-ce";
    import {GlobalvarProvider} from '../../providers/globalvar/globalvar';
    import { create } from 'domain';

    @IonicPage()
    @Component({
      selector: 'page-game',
      templateUrl: 'game.html',
    })
    export class GamePage {


      width:any=0;
      height:any=0;
      game: Phaser.Game;


      heightscore:any;

      constructor(
        public navCtrl: NavController
        , platform: Platform
        ,public gvp:GlobalvarProvider
      ) {

        this.heightscore= this.gvp.highscore; // here I am able to assign value from the gvp:GlobalvarProvider

        this.width=platform.width();
        this.height=platform.height();

        this.totalscore = 0;
    this.game = new Phaser.Game(this.width, this.height, Phaser.CANVAS, 'phaser-example'
       , { create: this.create,preload:this.preload,update:this.update
      });
    }
    preload () {

                    this.heightscore = this.gvp.highscore; 
// Problem is here. here I am not able to assign value from the GlobalvarProvider.
   // also the value which I assined earlyer inside the constructor is not showing. 
  //I tried this.game.global={ this.heightscore = this.gvp.highscore; }; which is not working


                  }
    create() {
    ...
    }

    update(){
    ...
    }
Arijit
  • 1,633
  • 2
  • 21
  • 35
  • You should follow the instructions at https://stackoverflow.com/help/mcve to show a good question, errors, etc. to allow people to help. – Steven Scott Sep 20 '18 at 14:21
  • Please let me know what details you need? if you work in Phaser+Typescript, you should understand my question. – Arijit Sep 20 '18 at 14:43
  • 1
    I work in Phaser and TypeScript and don't understand your question either. Could you add the code for both how you're setting it and how you're retrieving it? Are you using Phaser 2 or Phaser 3? – James Skemp Sep 20 '18 at 16:57
  • @JamesSkemp I have added sample code. Please have look. Please let me know if you need further details. I am stuck here, need help badly. – Arijit Sep 20 '18 at 17:42
  • This looks to be specific to using Ionic, but ... in your constructor you're passing in `gvp`. Can you add `gvp` as a property of `GamePage` and access it that way? I'm kind of confused though because it almost looks like `GamePage` contains your `game` and the `preload`/`create`/`update` functions which should be on the Phaser `Game`'s state ... Is your `create` and `update` working as you'd expect in Phaser, or are those Ionic-related? – James Skemp Sep 20 '18 at 19:37
  • yes they are working as expected. – Arijit Sep 20 '18 at 19:44
  • I tried passing the globalprovider in my game. but not able to implement it properly. if possible can you please give me an example? – Arijit Sep 20 '18 at 19:45
  • Possibly just a case of failing to bind `this.preload`, `this.create`, and `this.update` to preserve the `this` context? See [this canonical question](https://stackoverflow.com/q/20279484). – Matt McCutchen Sep 22 '18 at 17:45

1 Answers1

0

If you are using the global library, you can use this:

declare var Phaser: any;
gabfiocchi
  • 190
  • 11