0

こんにちは、私はクラス プロジェクトのコードに取り組んでいますが、何らかの理由で、クラスで取得したデモ コードが同じように機能しません。デバッグの助けが得られるので、おそらく遅れていることがわかっているので、エラーは発生していません。事前にどうもありがとう。

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

4

1 に答える 1

0

あなたが電子メールで送ったスクリプトを見ると、aGrid に Card 型のオブジェクトが含まれていることがわかります。ECMA JavaScript では、厳密に型指定されるものはありません。ECMA は、ブラウザーで実行される JS です。あなたのJSは、ブラウザが特定のページをリクエストしたり、デスクトップアプリケーションとして使用されたりしたときにサーバー上で実行される.netコードです(アプリの種類がわからない)。JavaScript といっても、構文が JavaScript に似ているというだけのことですが、.net コードなので JavaScript (ECMA) とはまったく異なります。

ECMA JS でできることの 1 つは、次のとおりです。

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 からカードを取得するときは、後でオブジェクトとして宣言するときに型キャストする必要がないため、Card として宣言することをお勧めします。とにかく、配列 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 を Card のインスタンスとして宣言してみませんか?

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

それはあなたの問題をすぐに解決するはずです。.net 開発環境をセットアップしていないため、ここでコードを実行することはできませんが、解決すると確信しています。

型キャスト ( wikipedia )に関する記事を Google で検索できます。配列またはリストに型が混在している場合の頭痛の種を想像できます。これは、ジェネリックが助けの手を差し伸べることができる場所ですが、頭が爆発する可能性があるため、それに入るには時期尚早かもしれません:-)

于 2013-03-05T05:07:42.607 に答える