2013-03-05 122 views
0

嘿家伙我正在为一个类项目的一些代码工作,由于某种原因,我在课堂上得到的演示代码的工作方式不一样。错误没有做任何,因为我可以得到一些帮助调试我知道它可能会减速。非常感谢。其中ifFaceUP和IMG定义为什么变量没有显示为对象的成员?

class Card extends System.Object 
{ 
//is the card face up 
var ifFaceUp:boolean = false; 

//has the card been matched 
var ifMatched:boolean = false; 

//image for the card 
var img: String; 

//constructor 
function Card(img : String) 
{ 
    this.img = img; 

} 
} 

错误

function BuildGrid() 
{ 
//begin with a BeginVertical() call so that controls 
//are stacked vertically 
GUILayout.BeginVertical(); 
GUILayout.FlexibleSpace(); 

//begin loopping for the rows 
for(var i=0; i<rows; i++) 
{ 
    //call BeginHorizontal() so that controls are stacked 
    //horizontally 
    GUILayout.BeginHorizontal(); 
    GUILayout.FlexibleSpace(); 

    //begin looping for the columns 
    for(var j=0; j<cols; j++) 
    { 
     //getting the card object that resides 
     //at this location in the array 
     var card:Object = aGrid[i][j]; 

     //the definition for the backside (visible part for the card 
     var img : String; 

     //check if the card is face up, if so, show the robot part 
     //if not show the wrench 
     if(card.ifFaceUp) 
     { 
      img = card.img; 
     } 
     else 
     { 
      img = "wrench"; 

     } 

     //create a button using a picture instead of 
     //text. Getting the picture from the Resources 
     if(GUILayout.Button(Resources.Load(img), 
      GUILayout.Width(cardW))) 
      { 
       flipCardFaceUp(card); 
       //print to the debug the name of the picture 
       Debug.Log(card.img); 
      } 
    } 
    GUILayout.FlexibleSpace(); 
    GUILayout.EndHorizontal(); 
} 
GUILayout.FlexibleSpace(); 
GUILayout.EndVertical(); 
}//end buildGrid 

类:http://puu.sh/2clHw

+0

var card:Object = aGrid [i] [j]; aGrid包含哪些类型的对象?后来你尝试:card.ifFaceUp,但ifFaceUp不是Object的成员,所以它不会工作吗?您应该将卡转换为正确的类型:var card:Card = aGrid [i] [j]; – HMR 2013-03-05 04:50:23

+1

也许会添加C#标记,看起来像您发布的代码是C# – HMR 2013-03-05 04:51:24

+0

aGrid [i] [j]是一个2d对象数组。 – 2013-03-05 04:52:24

回答

0

看剧本你发电子邮件,我可以看到aGrid包含类型卡的对象。在ECMA JavaScript中没有强类型。 ECMA是在浏览器中运行的JS。您的JS是.net代码,当浏览器请求某个页面或用作桌面应用程序时(不知道它是什么类型的应用程序),它会在服务器上运行。尽管它被称为JavaScript,但它仅仅意味着语法看起来像JavaScript,但因为它的.net代码与JavaScript(ECMA)完全不同。

一两件事你可以与ECMA JS做的是:

var notStronglyTyped=22; // is int 
notStronglyTyped = "hello";// changed to string 
notStronglyTyped.subString();// can call subString now because it's a method of string 

这不会在.NET中工作,因为.NET是强类型(以及基于类,但是这不是手头的问题)。因此,当INT声明变量:

var i:int=0;// the :int gives a syntax error in ECMA JS since it doesn't exist. 

强类型意味着变量需要声明或在执行被accociated与该类型的功能或访问属性之前被转换为特定类型。子串是String的一种方法,但我不能在一个int上调用它:

var i:int=0; 
i.subString();//won't work in .net unless you cast it to another type. 

((String)i).subString();//might work but I don't know the exact syntax for .net JS. 

总之;当从网格中检索卡片时,我会建议将它们声明为卡片,因为当您将其声明为Object时,不需要稍后再进行类型转换。该阵列aGrid包含类型卡的对象反正所以你不应该得到一个警告或错误有:

card = new Card("robot" + (i+1) + "Missing" + theMissingPart); 
    //instance card is of type Card 
    aCards.Add(card);//aCards contains items of type Card 
       .... 
    aGrid[i][j] = aCards[somNum]; 
//aGrid is filled with items from aCards which in turn contains items of type Card 

在代码中的某些时候,你做的事:

var card:Object = aGrid[i][j]; 

我不知道为什么,因为aGrid包含Card类型(所有东西都是Object类型,因为所有东西都从Object继承)。那么为什么不把变量卡声明为卡的一个实例呢?

var card:Card = aGrid[i][j]; 

这应该可以解决您的问题。我无法在这里运行代码,因为我没有建立.net开发环境,但我确信解决它。

你可以在类型转换(wikipedia)上查找谷歌上的文章,你可以想象当你使用混合类型填充数组或列表时可能伴随的头痛。这是generics可以伸出援手,但它可能为时过早,因为它可以使你的头部爆炸:-)

+0

请不要那张卡片:对象在您发布的两个文件中出现4次。 – HMR 2013-03-05 06:06:57

相关问题