Actionscript 3:在类中创建字典?

时间:2014-02-06 14:51:20

标签: actionscript-3 class dictionary public var

我正在尝试将我的actionscript 3代码从我之前使用的时间轴转换为包。

我需要在代码的开头定义一些字典。

但是当我在类中运行以下代码时,actionscript会返回错误。

public var S_USA:Dictionary = new Dictionary();
    S_USA["x"] = -299;
    S_USA["y"] = -114;
    S_USA["bynavn"] = "New York";

这是错误:“1120:访问未定义的属性S_USA。


编辑:发布整个代码:

package 
{
import fl.motion.MatrixTransformer;
import flash.display.MovieClip;
import flash.utils.Dictionary;
import flash.display.Shape;
import fl.transitions.Fly;
import fl.motion.MatrixTransformer;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.events.KeyboardEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import flash.ui.Mouse;
import flash.text.TextField;
import flash.display.SimpleButton;
import fl.controls.List;

public class Main extends MovieClip
{
    public static var bg_width = 980;
    public static var bg_height = 541;
    public var spImage:Sprite;
    public var mat:Matrix;
    public var mcIn:MovieClip;
    public var mcOut:MovieClip;

    public var externalCenter:Point;
    public var internalCenter:Point;

    public var scaleFactor:Number = 0.8;
    public var minScale:Number = 0.25;
    public var maxScale:Number = 10.0;

    /*public static var startList:List;
    public static var sluttList:List;*/
    public var bynavntxt:TextField;
    public var intervall:TextField;
    public var create_route:SimpleButton;
    public var confirm_button:SimpleButton;
    public var beregn_tid:SimpleButton;

    public static var S_Norway:Dictionary = new Dictionary();
    S_Norway["x"] = -60;
    S_Norway["y"] = -183;
    S_Norway["bynavn"] = "Oslo";

    public static var S_Australia:Dictionary = new Dictionary();
    S_Australia["x"] = 307;
    S_Australia["y"] = 153;
    S_Australia["bynavn"] = "Sydney";

    public static var S_China:Dictionary = new Dictionary();
    S_China["x"] = 239;
    S_China["y"] = -98;
    S_China["bynavn"] = "Beijing";

    public static var S_South_Africa:Dictionary = new Dictionary();
    S_South_Africa["x"] = -26;
    S_South_Africa["y"] = 146;
    S_South_Africa["bynavn"] = "Cape Town";

    public static var S_Brazil:Dictionary = new Dictionary();
    S_Brazil["x"] = -210;
    S_Brazil["y"] = 73;
    S_Brazil["bynavn"] = "Rio de Janeiro";

    public static var S_USA:Dictionary = new Dictionary();
    S_USA["x"] = -299;
    S_USA["y"] = -114;
    S_USA["bynavn"] = "New York";

    public static var S_France:Dictionary = new Dictionary();
    S_France["x"] = -79;
    S_France["y"] = -135;
    S_France["bynavn"] = "Paris";

    // ------------------------------------------------------

    public static var Flyplasser:Dictionary = new Dictionary();
    Flyplasser["USA"] = S_USA;
    Flyplasser["Norway"] = S_Norway;
    Flyplasser["South Africa"] = S_South_Africa;
    Flyplasser["Brazil"] = S_Brazil;
    Flyplasser["France"] = S_France;
    Flyplasser["China"] = S_China;
    Flyplasser["Australia"] = S_Australia;

    public function Main()
    {
        // ------------------------------------

        startList:List = new List();
        sluttList:List = new List();
        bynavntxt:TextField = new TextField()  ;
        intervall:TextField = new TextField()  ;
        create_route:SimpleButton = new SimpleButton()  ;
        confirm_button:SimpleButton = new SimpleButton()  ;
        beregn_tid:SimpleButton = new SimpleButton()  ;

        this.addChild(startList);
        this.addChild(sluttList);
        this.addChild(bynavntxt);
        this.addChild(intervall);
        this.addChild(create_route);
        this.addChild(confirm_button);
        this.addChild(beregn_tid);
        // -----------------------------------------
        // We use the ctrl and shift keys to display the two different cursors that were created on the stage.
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);

        this.addEventListener(MouseEvent.CLICK, mouseCoordinates);

        trace("Main spawned");
        // --------------------

        this.width = bg_width;
        this.height = bg_height;

        // --------------------

        for (var k:Object in Flyplasser)
        {
            var value = Flyplasser[k];
            var key = k;
            trace(key);
            startList.addItem({label:key, data:key});
            sluttList.addItem({label:key, data:key});
            var airport:flyplass = new flyplass(key,Flyplasser[key]["bynavn"]);
            airport.koordinater(Flyplasser[key]["x"], Flyplasser[key]["y"]);
            this.addChild(airport);
        }

        var mcOut = new OutCursorClip();
        this.addChild(mcOut);

        var mcIn = new InCursorClip();
        this.addChild(mcIn);

        startList = new List();
        this.addChild(startList)

        sluttList = new List();
        this.addChild(sluttList)

        bynavntxt = new TextField;
        this.addChild(bynavntxt)

        intervall = new TextField;
        this.addChild(intervall)

        create_route = new SimpleButton;
        this.addChild(create_route)

        confirm_button = new SimpleButton;
        this.addChild(confirm_button)

        beregn_tid = new SimpleButton;
        this.addChild(beregn_tid)

2 个答案:

答案 0 :(得分:0)

您是在构造函数或函数中设置此数据吗?

修改

嗯,从哪里开始...

1)你知道如果你要创建超过1个类的实例,静态的技巧就不会起作用吗?在这种情况下,如果你只有1个主要类,那么它会起作用,但是omg ... Imho在静态变量中存储数据并不好......

2)如果您已经宣布:

public var bynavntxt:TextField;
public var intervall:TextField;

稍后再做

bynavntxt = new TextField()  ;
intervall = new TextField()  ;

不需要输入。

3)稍后在Main,你有类似的东西:

var mcOut = new OutCursorClip();
this.addChild(mcOut);

var mcIn = new InCursorClip();
this.addChild(mcIn);

为什么要声明具有相同名称但没有Type?

的新变量

Waaaaaay你有:

public var mcIn:MovieClip;
public var mcOut:MovieClip;

因此已经声明了这些变量。通过再次声明它们,你正在创造本地的。谨防!!在那之后,顶部的那些将是空的。

答案 1 :(得分:0)

1)在构造函数中填写字典(我已经通过评论看到你已经这样做了。)

2)您已经使用public var bynavntxt:TextField;为bynavntxt分配了类型,您不应该只使用bynavntxt:TextField = new TextField(); bynavntxt= new TextField();再次执行此操作。

这与类似类型的所有其他变量相同。

3)确保此类将成为您的“文档类”/“编译类”,否则您将无法访问“stage”变量。为了确保您可以访问舞台变量,您可以随时执行:

/* Inside Constructor */
addEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);

/* Outside Constructor */
private function _onAddedToStage(e:Event):void
{
    removeEventListener(Event.ADDED_TO_STAGE, _onAddedToStage);
    stage.addEventListener(KeyboardEvent.KEY_DOWN, keyHandler);
    stage.addEventListener(KeyboardEvent.KEY_UP, keyHandler);
}

同样,如果这将是编译的类,可以跳过它。