为什么变量没有显示为对象的成员?

时间:2013-03-05 04:40:33

标签: javascript class object

嘿伙计们,我正在为类项目编写一些代码,由于某种原因,我在课堂上获得的演示代码的工作方式不同。错误没有任何问题,因为我可以得到一些帮助调试我知道它可能是迟钝的。非常感谢。

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

定义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;

}
}

错误:http://puu.sh/2clHw

1 个答案:

答案 0 :(得分:0)

查看您通过电子邮件发送的脚本我可以看到aGrid包含Card类型的对象。在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.

强类型意味着在执行函数或访问与该类型相关联的属性之前,需要声明变量或将其转换为特定类型。 substring是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.

总之;当从aGrid中检索卡片时,我建议将它们声明为卡片,因为当你将它声明为Object时不需要输入类型。数组aGrid无论如何都包含Card类型的对象,所以你不应该在那里收到警告或错误:

    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继承的)。那么为什么不将变量卡声明为Card的实例?

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

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

您可以在类型转换(wikipedia)上查找Google上的文章,您可以想象当您使用混合类型填充数组或列表时随之而来的麻烦。这是generics可以伸出援助之手的地方,但它可能为时尚早,因为它会让你的头爆炸: - )