簡體   English   中英

我可以通過使用外部類屬性的值來獲取主類變量嗎

[英]can i reach a main class variable, by using the value of an external class property

好吧,我的問題是這個。

在我的主類中,我有一個布爾類型變量。 在外部類中,我有一個String類型的變量。

通過使用外部類中變量的字符串值,是否可以訪問我的主類中的變量。 請注意,外部類屬性的字符串值與主類變量匹配。

我只是嘗試這樣做:

主類CardGame.as具有變量var slot1:Boolean

在外部類中,有變量var slot:String = slot1; 我也有以下代碼行: CardGame['slot'] = false;

它似乎不起作用:(。任何幫助將不勝感激。謝謝

主類文件的一部分:

function drawCard():void
    {

        var card:Card = new Card();
        if(slot1 == false)
        {
            card.x = 30;
            slot1 = true;
            card.slot = "slot1";
        }
        else if(slot2 == false)
        {
            card.x = 190;
            slot2 = true;
            card.slot = "slot2";
        }
        else if(slot3 == false)
        {
            card.x = 350;
            slot3 = true;
            card.slot = "slot3";
        }
        else if(slot4 == false)
        {
            card.x = 510;
            slot4 = true;
            card.slot = "slot4";
        }
        else if(slot5 == false)
        {
            card.x = 670;
            slot5 = true;
            card.slot = "slot5";
        }
        else
        {
            card.x = 830;
            slot6 = true;
            card.slot = "slot6";
        }
        card.y = cardY;
        cardContainer.addChild(card);
    }

和外部文件:

import flash.display.MovieClip;
import flash.events.MouseEvent;
import CardGame;

public class Card extends MovieClip
{
    public var slot:String;

    public function Card()
    {
        // constructor code
        addEventListener(MouseEvent.CLICK, removeCard)
    }

    function removeCard(event:MouseEvent):void
    {
        this.parent.removeChild(this);
        CardGame['slot'] = false;

    }


}

您將在外部類中需要幾行代碼:

// in CardGame.as

// declare slot1 as a public var right after the class declaration to insure the 
// correct scope

public class CardGame extends MovieClip {

     public static var slot1:Boolean;

     ....

// in external class
import CardGame // depends on where this is in relation to the external class

function theFunction():void {
    // somewhere in your external class
    var slot:String = CardGame.slot1.toString();
}



// Example classes that show how it works
// Main Class - instantiates ExtClass
package {
    import flash.display.Sprite;

    public class MainVar extends Sprite {
        public static var slot1:Boolean;
        private var extClass:ExtClass;

        public function MainVar() {
            slot1 = true;
            this.extClass = new ExtClass();
        }
    }
}

// External Class accesses static var, modifies static var
package {
    public class ExtClass {
        public function ExtClass() {
            var slot:String = MainVar.slot1.toString();
            var index:String = "1";
            var slotVar:String = "slot" + index;
            trace(slot);
            // get the value using a string
            trace("String access: ", MainVar[slotVar])
            MainVar.slot1 = false;
            slot = MainVar.slot1.toString();
            trace("Var access: ", slot);
        // get the value using a string
            trace("String access: ", MainVar[slotVar]);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM