简体   繁体   English

在另一个框架上的AS3导入类

[英]AS3 Importing Class on Another Frame

I am using external AS files for a new project of mine, which involved creating a place to draw so i used the following tutorial to get a basic idea of what is need. 我正在为我的一个新项目使用外部AS文件,该文件涉及创建绘图位置,因此我使用了以下教程来了解所需的基本知识。

I got the hope application working and running smoothly until i decided i wanted to add in a preloading & Menu - putting the drawing part of the app on the third frame - which got me this error: 我希望应用程序能够正常运行并正常运行,直到我决定要添加预加载和菜单-将应用程序的绘图部分放在第三帧上-这使我遇到了这个错误:

TypeError: Error #1009: Cannot access a property or method of a null object reference. TypeError:错误#1009:无法访问空对象引用的属性或方法。
- at Main/convertToBMD() -在Main / convertToBMD()
- at Main() -在Main()

So I thought instead of adding the Main.as to the Class under properties i would import the file on the frame instead. 因此,我认为不是将Main.as添加到属性下的Class中,而是将文件导入框架中。 Using: 使用方法:

var main:Main = new Main();
addChild(main);

This worked apart from it loses all connections to the instance names. 除此之外,它失去了与实例名称的所有连接。

Line 64 1120: Access of undefined property pencil.
Line 65 1120: Access of undefined property eraser.
Line 65 1120: Access of undefined property txt.
Line 82 1120: Access of undefined property board.
Line 83 1120: Access of undefined property board.

Etc..... 等等.....

So what i would like to know, is there a better way of doing this and getting it working on any frame? 所以我想知道,有没有更好的方法可以做到这一点并使它在任何框架上都能工作?

By changing something in the external script or another way of importing onto frame? 通过更改外部脚本中的内容还是其他导入框架的方式?

Thank you 谢谢

Eli 以利

The problem is that named instances are properties of the MainTimeline instance, which is typically the first child of the stage. 问题在于命名实例是MainTimeline实例的属性,它通常是该阶段的第一个子级。 The properties are not properties of your Main class (anymore). 该属性不再Main类的属性。

So, when you want to access these properties from with Main , you could do something like: 因此,当您想通过Main访问这些属性时,可以执行以下操作:

var timeline:DisplayObject = stage.getChildAt(0);
var pencil:DisplayObject = timeline["pencil"];

// do stuff with 'pencil'
pencil.x = 500;
...

Note: This assumes Main will have its stage property set, which should be the case once you've added it through addChild(main); 注意:这假设Main将设置其stage属性,一旦通过addChild(main);将其添加,情况就会如此addChild(main); . But this means that, in the constructor of Main , you won't be able to access the stage property yet to do the above. 但这意味着,在Main的构造函数中,您将无法访问stage属性来执行上述操作。 If this is a problem, considering having Main wait for the ADDED_TO_STAGE event before using 'pencil', etc. 如果这是一个问题,请考虑让Main在使用'pencil'等之前等待ADDED_TO_STAGE事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM