Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与此C#代码段等效的UnityScript_C#_Ios_Unity3d_Unityscript_Prime31 - Fatal编程技术网

与此C#代码段等效的UnityScript

与此C#代码段等效的UnityScript,c#,ios,unity3d,unityscript,prime31,C#,Ios,Unity3d,Unityscript,Prime31,这在UnityScript中是什么样子的 void playerDataLoaded( List< GameCenterPlayer > players ) { foreach( GameCenterPlayer p in players ) // do something with p } void playerDataLoaded(列出玩家) { foreach(玩家中的GameCenterPlayer p) //用p做点什么 } 玩家是一个列表而不是数组 我

这在UnityScript中是什么样子的

void playerDataLoaded( List< GameCenterPlayer > players )
{
    foreach( GameCenterPlayer p in players )
    // do something with p
}
void playerDataLoaded(列出玩家)
{
foreach(玩家中的GameCenterPlayer p)
//用p做点什么
}
玩家是一个列表而不是数组

我正在尝试处理来自Apple GameCenter的事件响应,该响应返回一个plater属性列表。以下是错误:

“length不是object player的成员”

“类型‘对象’(播放器)不支持切片”

使用iOS游戏中心、Unity和prime31游戏中心插件。

函数removePlayersDataLoaded(玩家){
function removePlayersDataLoaded(players){
  for(var i = 0; i < players.length; i++){
    var p = players[i];
    // do something with p
  }
}
对于(变量i=0;i
它将类似于:

var remotePlayersDataLoaded = function(players) {
  for (var i = 0; i < players.length; i++) {
    var player = players[i];
  }
};
var remoteplayersdataload=函数(玩家){
对于(变量i=0;i
使用
.forEach
为列表中的每个成员调用迭代器

function removePlayersDataLoaded(播放器){
players.forEach(doSomethingWithPlayer);
函数doSomethingWithPlayer(p){
//代码
}
}
或者在阵列上使用
for
循环

function removePlayersDataLoaded(播放器){
for(变量i=0,len=players.length;i
我相信是这样的:

function remotePlayersDataLoaded(players) {
for (p in players)
  {
  // do your thing with p
  } 
}
但是,可能是弄错了,它会抱怨吗?

而C#的列表与下面的列表不一样,类似这样的

function Player(playerName, gameCenterPlayer)
{
   this.playerName = playerName;
   this.GameCenterPlayer = gameCenterPlayer; // i'm going to assume this has been calculated elsewhere, and all we need now is a boolean.
}

function playerDataLoaded(players) {
  this.players = players
  this.getCenterPlayer = function() {
var i = 0
  while (i < players.length) {
  if (players[i].GameCenterPlayer) {
console.log(players[i].playerName + " is the center player!");
}
i++;
}
}

}

var player1 = new Player("Ted", 0);
var player2 = new Player("Jane", 1);
var player3 = new Player("Doug", 0);
Players = new playerDataLoaded([player1, player2, player3]);

Players.getCenterPlayer()
函数播放器(playerName、gameCenterPlayer)
{
this.playerName=playerName;
this.GameCenterPlayer=GameCenterPlayer;//我假设这是在别处计算的,现在我们只需要一个布尔值。
}
功能播放器已加载(播放器){
这个。玩家=玩家
this.getCenterPlayer=函数(){
变量i=0
while(i
你修复了语法,代码中仍然充满了反模式大括号,真的吗?我在看到你的留言之前就把它修好了。那你能指出我有什么反模式吗。我现在知道了两行,其中一行是签名,可能会充满反模式。信息仍然存在,这取决于你如何使用它。你使用
来。。。在
循环数组中,这是一种糟糕的做法,主要是因为性能原因。您没有声明局部变量
p
,因此它是一个隐含的全局变量。你有一个可怕的空白区风格。但是你应该知道所有这些,如果你在向其他人展示如何做之前没有真正学习javascript,那么从我看到的问题来看,提问者想要将方法签名和for循环转换为javascript。球员不也是一张名单吗?StackOverFlow是一个为需要帮助的任何人提供的免费网站。如果有人抽出时间来帮助我,我将不胜感激,而不是仔细检查他们的凭证。这是正确的,毫无价值,因为JavaScript没有一个好的
foreach
替代品。使用
for(玩家中的var p)
是正确的。对于一般情况,这可能是正确的,但对于我的具体情况(我可能应该更清楚地说明,我的错),它不起作用。如果不知道“玩家”是什么,我们就无法回答这个问题。你的问题是它是一个数组;如果它没有.length属性,那么情况就不是这样了…很抱歉之前的含糊不清。事实证明,玩家是一个列表,而不是一个数组。我认为首先列出的修改后的原始代码片段现在应该可以让您更好地了解细节了,也许?在C#中,列表和数组之间有区别;原生JavaScript没有“列表”的概念。您使用的是提供列表实现的库吗?值得注意的是,IE中的
foreach
支持非常糟糕。是的
foreach
传统浏览器不支持,您可以使用垫片来模拟它。