【发布时间】:2017-12-19 09:32:43
【问题描述】:
我有以下问题,我的视图没有像应有的那样更新。如果玩家做了他的动作,触发器就会起作用(我对当前游戏的每次更新都进行了控制台登录[1]),但视图不会定期更新。所以我试图调查这个问题,并在 Game.html [2] 的页脚中添加了一个随机数,应该每秒更新一次 [3] 以检查视图是否正确更新。但它没有,所以每次随机值应该改变时我都会记录控制台。
我的整个 Gamelogic 都在工作,如果我点击无效的 Tile 什么都不会发生,如果我点击有效的 Tile Firebase 正在更新,有时两个玩家视图也是如此。有时一个视图更新,有时没有更新
此问题的视频:
你应该看看页脚,并且两个玩家应该对游戏有相同的看法(除了轮到谁和随机页脚编号)
https://youtu.be/Wa-P4tXh6Oo
灰色字段 == 要点击的有效字段
[1] 检查更新
checkForUpdate(key): void {
const query = firebase.database().ref("/games").child(key);
query.on('value', snap => {
console.log("update");
if (!snap.val()) return;
this.updateTurn(snap.val().turn);
this.updatewon_Fields(snap.val().won_fields);
this.updateFields(snap.val().fields);
this.updateNextField(snap.val().nextField);
});
}
我将此 Game.html 用于单人游戏(与机器人)、多人游戏(一台设备上 2 名玩家)和多人游戏(在线)。该问题仅发生在在线多人游戏中。即使传入的数据是正确的,它也不会像应该那样更新视图
[2]
Game.html
<ion-content padding>
<ion-grid [ngClass]="getPlayerTurnClass()">
<ion-row *ngFor="let tetrisIndex of index; let r = index; trackBy: trackByFn">
<ion-col *ngFor="let x of tetrisIndex; let x = index; trackBy: trackByFn" [ngClass]="getClassValue(x)">
<div [ngClass]="{'player1_won':gameStatus.won_fields[x]==1,'player2_won':gameStatus.won_fields[x]==2,'bordern':x%2==0,'bordernplus1':x%2!=0}">
<ion-row class="ctr fc tile" *ngFor="let y of index2; let y = index; trackBy: trackByFn">
<ion-col *ngFor="let z of index2; let z = index; trackBy: trackByFn" (click)="playerClick(x,y,z)">
<div class="tile fc">
<span class="ctr tile-text" [ngClass]="{'player1':gameStatus.fields[x][y][z]==1,'player2': gameStatus.fields[x][y][z]==2}">{{(gameStatus.fields[x][y][z]==0)? ' ': ((gameStatus.fields[x][y][z]==1)? 'x' : 'o')}}</span>
</div>
</ion-col>
</ion-row>
</div>
</ion-col>
</ion-row>
</ion-grid>
<div *ngIf="gameService.isGameOver()">
<br>
<button ion-button (click)="btnRestartGame()" full block>Restart Game</button>
<button ion-button (click)="btnReturn()" full block>Return</button>
</div>
<div *ngIf="!gameService.isGameOver()&&gameStatus.gameType&&gameStatus.symbol==gameStatus.turn" class="ctr tile-text"><br><strong>Your turn!</strong><br></div>
<div *ngIf="!gameService.isGameOver()&&gameStatus.gameType!=2" class="ctr tile-text" [ngClass]="{'player1': gameStatus.turn,'player2': !gameStatus.turn}"><br><strong>{{gameStatus.turn? gameStatus.players[0].name : gameStatus.players[1].name}}'s turn!</strong><br></div>
<ion-footer>
<ion-toolbar>
<ion-title>Footer{{gameStatus.random}}</ion-title>
</ion-toolbar>
</ion-footer>
</ion-content>
[3]游戏状态
setInterval(() => {
this.random = Math.random();
}, 500);
我已经尝试在 Game.ts 中添加 v
setTimeout(() => {
this._applicationRef.tick();
}, 200);
抱歉我的英语不好,我希望我的问题有点清楚。
周末愉快!
【问题讨论】:
-
你试过 NgZone 了吗?
-
@SwapnilPatwa 我是 ionic 新手,所以我不知道如何检查?我创建的任何文件或 app.component.ts 中都没有包含 NgZone
-
没问题。与您共享工作代码。
标签: html angular ionic2 angular2-template