【问题标题】:Phaser Global Variable when using Typescript使用 Typescript 时的 Phaser 全局变量
【发布时间】:2019-02-24 14:35:44
【问题描述】:

我正在使用 ionic 框架来开发我的游戏。

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

【问题讨论】:

  • 您应该按照stackoverflow.com/help/mcve 的说明显示一个好的问题、错误等,以便人们提供帮助。
  • 请告诉我您需要什么详细信息?如果你在 Phaser+Typescript 工作,你应该明白我的问题。
  • 我在 Phaser 和 TypeScript 工作,也不懂你的问题。您能否为您的设置方式和检索方式添加代码?您使用的是 Phaser 2 还是 Phaser 3?
  • @JamesSkemp 我添加了示例代码。请看。如果您需要更多详细信息,请告诉我。我被困在这里,非常需要帮助。
  • 这看起来是特定于使用 Ionic,但是......在你的构造函数中,你传入了gvp。您可以将gvp 添加为GamePage 的属性并以这种方式访问​​它吗?我有点困惑,因为它几乎看起来像 GamePage 包含你的 gamepreload/create/update 函数应该在 Phaser Game 的状态...是您的 createupdate 在 Phaser 中的工作方式与您期望的一样,还是与 Ionic 相关?

标签: typescript ionic3 phaser-framework


【解决方案1】:

如果你使用的是全局库,你可以使用这个:

declare var Phaser: any;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-06-02
    • 2017-07-25
    • 1970-01-01
    • 2020-08-16
    • 2022-01-06
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多