2014-02-06 51 views
0

我试图将我以前使用的时间线中的actionscript 3代码转换为包。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) 
+0

好的,当我添加了'static'时,它工作。 – user3257755

回答

0

你设置内部构造或者这个数据功能?

编辑:

嗯,从哪里开始?

1)你知道,与静态的诀窍就在于,如果你要创建一流的超过1个实例要去工作?如果您有它的会的工作,但OMG ......恕我直言,这不是很好的数据存储在静态瓦尔...

2)如果你已经宣布只有1个主类这种情况下:

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

后来只是做

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

没有必要在那里类型。

3)后来在主你有这样的:

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

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

你为什么声明具有相同名称,但没有新的类型的变量?

Waaaaaay了你就必须:

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

所以这些变量已经声明。通过再次宣布你正在创建本地的。谨防!!那之后从顶部的那些将是空的。

+0

下面的“公共类Main扩展MovieClip”和函数上面。不知道在哪里叫它:) – user3257755

+0

最好在构造函数或函数之一。你应该编辑你的问题,并粘贴更多的代码,所以我们可以肯定。 – 3vilguy

+0

现在我添加了一些代码。我修正了字典问题。但是现在我可能在添加按钮和文本框时出错了。 95行\t 1067:将类型为flash.text:TextField的值的隐式强制转换为无关类型的类。 – user3257755

0

1)填写构造函数内的字典(我看到你已经完成了这些评论)。

2)你已经分配了类型到bynavntxt与public var bynavntxt:TextField;你不应该再这样做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); 
} 

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