Frage

I'm trying to transform my actionscript 3 code from the timeline, which I previously used, to packages.

I need to define some dictionaries in the beginning of the code.

But when running the following code inside my class, actionscript returns an error.

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

This is the error: "1120: Access of undefined property S_USA.


EDIT: Posting the entire code:

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)
War es hilfreich?

Lösung

Are you setting this data inside Constructor or function?

EDIT:

Hm, where to start...

1) You know that the trick with static is not gonna work if you'll create more than 1 instance of class? In this case if you have only 1 Main class it's gonna work, but omg... Imho it's not nice to store data in static vars...

2) If you already declared:

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

later just do

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

no need for type in there.

3) Later in Main you have something like:

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

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

Why are you declaring new variables with same name but without Type?

Waaaaaay up you have:

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

so those variables are already declared. By declaring them once again you are creating local ones. Beware!! The ones from the top would be null after that.

Andere Tipps

1) Fill the dictionary inside the Constructor (I seen by comments you have done this already).

2) you have already assigned type to bynavntxt with public var bynavntxt:TextField; you should not be doing it again with bynavntxt:TextField = new TextField(); just bynavntxt= new TextField(); will do.

This is the same with all other variables on similar type.

3) Make sure this class is going to be your "Document Class"/"Compiled Class" otherwise you will not have access to the 'stage' variable yet. To make sure you will have access to the stage variable you can always do:

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

Again, if this is going to be the compiled class, this can be skipped.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top