我可以通过使用外部类属性的值来到达主类变量

时间:2012-12-27 13:09:17

标签: actionscript-3

好的,我的问题是这个。

在我的主类中,我有一个布尔类型变量。在外部类中,我有一个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;

    }


}

1 个答案:

答案 0 :(得分:0)

外部类中需要几行代码:

// 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]);
        }
    }
}