【问题标题】:Actionscript 3: Create dictionary inside class?Actionscript 3:在课堂内创建字典?
【发布时间】:2014-02-06 14:51:19
【问题描述】:

我正在尝试将我之前使用的时间线中的 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)

【问题讨论】:

  • 好的,当我添加static时它工作了。

标签: actionscript-3 class dictionary public var


【解决方案1】:

您是在构造函数还是函数中设置这些数据?

编辑:

嗯,从哪里开始……

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

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);

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

Waaaaaaay 你有:

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

所以这些变量已经被声明了。通过再次声明它们,您正在创建本地的。谨防!!在那之后,顶部的那些将是空的。

【讨论】:

  • 在“public class Main extends MovieClip”下方和函数上方。不知道在哪里调用它:)
  • 最好在构造函数或函数之一中进行。您应该编辑您的问题并粘贴更多来自此类的代码,以便我们确定。
  • 现在我添加了一些代码。我修复了字典问题。但现在我可能在添加按钮和文本字段时做错了。第 95 1067 行:将 flash.text:TextField 类型的值隐式强制转换为不相关的类型 Class。
【解决方案2】:

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

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

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

3) 确保该类将成为您的“文档类”/“编译类”,否则您将无法访问“阶段”变量。为确保您可以随时访问阶段变量:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多