使用Typescript时的Phaser全局变量

时间:2018-09-20 10:00:33

标签: typescript ionic3 phaser-framework

我正在使用离子框架来开发游戏。

我使用了Phaser作为插件。一切进展顺利,但我无法在游戏中使用任何外部变量。

例如最高分数。我想在我的应用程序菜单中显示它。

我已经尝试过:

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

通过引用:http://www.html5gamedevs.com/topic/21143-global-variables/ 但显示编译错误。

请帮助

编辑: 示例代码:

 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(){
    ...
    }

1 个答案:

答案 0 :(得分:0)

如果您正在使用全局库,则可以使用以下方法:

declare var Phaser: any;