У меня есть игра с библиотекой Phaser3 .Эта игра находится в файле phaser-game.component.ts
.Класс внутри предназначен для взаимодействия с другими компонентами и сервисами.Функциональность игры находится в этом файле, но НЕ в классе.Мне нужно получить доступ к определенному методу в классе или к свойству scoreService
класса из файла phaser-game.component.ts
.Как мне ссылаться на *. 1008 *
1) функцию внутри класса (preferable
) вне класса и в том же файле или
2) на оценку (typeof "ScoreService"
)класс из-за пределов класса и в том же файле?
- ссылаясь на эти свойства как "this.updateHighscore"
- декларируя новый scoreService
вне класса для доступа к требуемой функции
-движущийся код для тега сценария внутри .html
документа, а не связанный с ним .ts
файл
Phaser-game.component.ts:
import { ApiService } from './../api.service';
import { ScoreService } from './../score.service';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { Physics } from 'phaser';
@Component({
selector: 'app-phaser-game',
templateUrl: './phaser-game.component.html',
styleUrls: ['./phaser-game.component.css']
})
export class PhaserGameComponent implements OnInit, OnDestroy {
user: any;
constructor(private api: ApiService, private score: ScoreService) {}
ngOnInit() {
game = new Phaser.Game(config);//Makes game
console.log("Component loaded");//Debug
this.api.getUser().subscribe(res => {//This works, gets user data
res == {} ? (
this.user = this.api.getUser().subscribe(res => {
this.user = res;
})
) : this.user = res;
});
}
ngOnDestroy(): void {
console.log("component being destroyed");
game.destroy();
moveSpeed = 200;
}
//This is the function that needs to be accessed
updateHighscore(): void {
this.score.updateHighscore(dataToSend);//This function works
}
}
//End of class
/*
Bunch of code relating to function of the game
*/
//This is the method that needs to do the update
//It's the games loop
update() {
if (gameOver) {
//Somehow get to the updateHighscore() method
return; //This ends the game loop
}
}
Если требуется какой-либо другой код, дайте мне знать.Насколько я смог протестировать, все работает как задумано, я просто не могу добраться до updateHighscore
в классе в этом файле.Спасибо!
Ожидаемые результаты:
функция updateHighscore в PhaserGameComponent класс успешно вызывает функцию updateHighscore из ScoreService объявлено в конструкторе.
Фактические результаты:
все, что я пробовал, не может получить доступ к функции updateHighscore в PhaserGameComponent учебный класс.Обычно "X is undefined"
, где «x» - то, что я использовал, чтобы попытаться добраться до этой функции.