我目前正在使用 Phaser IO 和 SignalR (+jQuery) 创建游戏, 我从服务器获取了一个玩家列表(现在包含一个 ID 和名称),我为每个玩家创建了一个文本字段,稍后我想对其进行操作(使用特定玩家拥有的票数),但是我不知道关于如何引用动态文本对象。
我也乐于接受新想法
var game = new Phaser.Game($window.innerWidth, $window.innerHeight, Phaser.Auto, 'gameCanvas');
var dayState = {
preload: function () {
// Preloaded stuff
},
create: function () {
var world = game.world;
// Players alive in game
var players = // Call to Server, retrieves list of players
// Add player
for (var i = 0; i < players.length; i++) {
var currentPlayer = players[i];
// Player name
game.add.text(world.width - 225, y, currentPlayer.Name);
// I WANT TO UPDATE THIS UPON CALLBACK
game.add.text(world.width - 175, y, 0)
// Vote button
game.add.button(world.width - 50, y + 2, //preloaded texture for button, voteFunction, currentPlayer.Id , 2, 1, 0);
}
}
};
game.state.add('DayState', dayState);
game.state.start('DayState');
function voteFunction() {
// Posts vote to server
};
function voteReturnedFromServer(amount){
// Server calls this function (SignalR)
// This is where I want to update text element created above with data from SignalR
// Update text with callback data "amount"
};
最佳答案
您可以继续定义一个与 game
相同级别的变量(为方便起见),然后设置一个等于您添加到游戏中的文本的变量。
var voteText;
// ...
voteText = game.add.text('world.width - 175, y, 0);
如果定义了 voteText,则只需更新 text
。
voteText.text = 'data returned from the server'
关于javascript - Phaser IO Update 在回调时动态创建文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45143699/