Changing the variable value of a scope using Phaser and typescript within an Ionic 2 application

I have been working on developing a game application using a combination of Ionic 2 with TypeScript and Phaser.io. Everything has been going well and the game is functioning properly, however there is one issue that I have encountered...

While I am able to trigger Phaser actions from within a TypeScript function, I am unable to do the reverse.

For instance, I have a score displayed in my HTML as {{ score }}. In my TypeScript controller, I initialize this score with this.score = 0;. Although I can update the score within a TypeScript function, it does not seem to work within any Phaser functions such as preload, create, update, or render. An example showcasing this problem is depicted in the image below (a simplified version of my code).

view image example

Appreciate any assistance you can provide.

Answer №1

As a newcomer to the world of Ionic and Phaser, I've discovered an interesting relationship between the two. When initializing a new game instance in Phaser using:

this.game = new Phaser.Game(width, height, renderer, parent, state, transparent, antialias, physicsConfig)

The state parameter actually creates a separate scope for Phaser. This means that you cannot access the same 'this' object within Ionic's 'ionViewDidEnter()' function as you can in Phaser's 'create()' function.

To circumvent this issue (though it may not be the optimal solution), I decided to include the following code:

this.game = new Phaser.Game(width, height, renderer, parent, state)
this.game.ionic = this; // linking Ionic Object with Phaser Object

This allows me to call Ionic methods from inside Phaser, like so:

create() {
  this.game.ionic.alert.present();
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

There was an issue encountered when creating the class: The parameters provided do not correspond to any valid call target signature

I am facing an issue with my code. Here is the scenario: export class MyClass { public name:string; public addr:string; constructor() {} } I have imported MyClass and trying to use it like this: import { MyClass } from './MyClass' ...

Replacing the '+' character with a space in HttpParams within Angular 6

When passing a JSON object using HttpParams, the + character is automatically converted to a space before being sent to the backend. Despite trying multiple solutions, I have been unable to resolve this issue for a JSONObject string. this.updateUser({"nam ...

Error: The global variable "Stripe" has not been defined

Having trouble with integrating the Stripe API. Development Environment: "devDependencies": { "@angular-devkit/build-angular": "~0.803.20", "@angular/cli": "~8.3.23", "@angular/compiler": "~8.2.14", "@angular/compiler-cli": "~8.2.14", ...

The type 'Readonly<Ref<Readonly<any>>>' does not have the property 'forEach' available

Having an issue with state management in Vue3 (Pinia) using the Composition API. I successfully retrieved an array called countryCodes and now I want to copy all items from this array into another array called countries defined in the state. However, whe ...

Obtaining file image input in TypeScript

I am struggling to integrate a file uploaded via the input type "file" into my Fabric JS canvas. The steps of the process are as follows: User initiates action by pressing a button (calls onAddImage) User selects an image to upload Selected image is adde ...

When using TypeScript, how can I effectively utilize the Component property obtained from connect()?

Software Development Environment TypeScript 2.8 React 16 Redux Foo.tsx interface IFooProps{ userId:number } class Foo extends Component<IFooProps>{ render(){ return <p>foo...</p> } } const mapStateToProps = (state: I ...

Inheritance of Generic Types in TypeScript

Could someone assist me in understanding what is incorrect with the code snippet provided here? I am still learning Typescript. interface ICalcValue { readonly IsNumber: boolean; readonly IsString: boolean; } interface ICalcValue<T> ex ...

Automated Import Feature in Visual Studio Code

I'm currently transitioning from Webstorm to Visual Studio Code due to the poor performance of Webstorm. However, I'm facing issues with Visual Studio Code not being very efficient at detecting and importing the dependencies I need. I find mysel ...

Utilizing const as the iteration variable in a for loop

I've grasped the concept of using var and let in a for loop in typescript/javascript, but can someone shed light on how and why a const variable as a loop variable behaves? for (const i = 0; i < 5; i++) { setTimeout(function() { console.log( ...

Create an array interface to begin

According to the information provided in the Handbook, it is possible to define a custom array interface with either string or number index: interface StringArray { [index: number]: string; } To demonstrate this concept, I created the following inter ...

Is it advisable to specify data types for my JSON data in TypeScript?

For the shopping application in my project, I am utilizing a JSON structure to categorize products as either hot or branded. However, I encountered an issue when trying to define a type for the entire JSON object labeled "full". Despite my attempts, it app ...

Cross-origin request error persists despite configuring headers on the server. Unable to successfully relocate image to designated directory on the server

I am encountering a CORS error specifically when sending delete requests from Angular to Laravel. Additionally, I am facing issues with moving car model images to the directory during posting, resulting in errors. I have implemented a CORS middleware and a ...

Using Generic Types in TypeScript Files for React Components

I have encountered an issue that I haven't been able to find a solution for online. When I define a function in a ts file like this: const lastGeneric = <T>(arr: Array<T>): T => { return arr[arr.length - 1]; } But when I try to do ...

Why is it considered an error when an index signature is missing in a type?

Consider the TypeScript code snippet below: type Primitive = undefined | null | boolean | number | string; // A POJO is simply meant to represent a basic object, without any complexities, where the content is unknown. interface POJO { [key: string]: ...

Troubleshooting a problem with testing an AngularJS controller in TypeScript using angular.mock.inject with Karma and Jasmine

Currently, I am facing a challenge while attempting to test an AngularJS 1 controller that is written in TypeScript using Jasmine + Karma. Unfortunately, I keep encountering an error that I am struggling to resolve. The error message itself is not very des ...

A guide on resolving TypeScript monorepo webpack loader problems

I recently created a typescript monorepo here with the following organized folder structure: . └── packages ├── package.json // Contains the monorepo workspace configuration ├── web-app // Includes the NextJS website files └ ...

Develop an Angular 6 application that utilizes an observable to monitor changes in a variable

I am working with Angular 6 and I need to monitor a variable for any changes and then stop or unsubscribe when the variable has a value. My initial thought was to use an Observable: myValue; // The variable that needs to be monitored myObservable = Obse ...

AngularJS secureHTML verify image click event

Just starting out with AngularJS and hoping for some assistance :) I have a div that has the details of an activity, formatted in HTML. To display this content correctly, I am using trustAsHTML: <div class="description-div"> <div ng-bind-htm ...

Similar to `util.inspect` in Node.js, Deno also has a function

Is there a utility function in Deno that can stringify an Object or primitive similar to Node.js util.inspect? For instance, if I have a JSON object in Node.js and want to display its contents: > m = {k1:'v1', k2:'v2'} { k1: ' ...

Angular 6 introduces a new component with cascading comboboxes for easier data organization

In my Angular 6 project, I have successfully implemented a shared component called country-state. It is functioning perfectly. VIEW MY PERFECT WORKING EXAMPLE However, upon dividing the country-state component into separate country and state components, ...