从 as3 类创建的 mxml 组件调用方法?
好吧,我总是用 as3 而不是 mxml 来开发我的项目,我通常只是设置我的应用程序来运行一个主动作脚本文件作为我的主文档根,有点像你在 flash 中的做法,但在 flash builder 中我做了一些事情像这样:
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
.....etcetc
>
<com:MyAS3DocClass>
</com>
<fx:Script>
<![CDATA[
public function call_from_outside():void
{
//Some code stuff here...
}
]]>
</fx:Script>
</s:Application>
现在说 MyAS3DocClass 中有一个公共函数:
public function hitme():void
{
trace('ouch');
}
我的问题是,我如何在 call_from_outside()
函数中调用该函数 hitme()
之间的
标签?
我这样做的原因是因为我有一些 flash swfs,我将它们加载到另一个 swf 文件中,并且我可以访问这些 flash swfs 的顶层公共函数,但是 flash builder/flex swfs 的顶层是主要的mxml 文件不是我的假 as3 类根。当然,我应该认为有一种方法可以让我访问 mxml 组件的 as3 类方法,但为了保持相同的结构,我可以绕过修改我的加载器脚本。有人有想法吗?
Okay, I've always just developed my projects in as3 instead of mxml and I usually just setup my application to run a main actionscript file as my main doc root, sort of like how you do it in flash but in flash builder I do something like this:
<s:Application
xmlns:fx="http://ns.adobe.com/mxml/2009"
.....etcetc
>
<com:MyAS3DocClass>
</com>
<fx:Script>
<![CDATA[
public function call_from_outside():void
{
//Some code stuff here...
}
]]>
</fx:Script>
</s:Application>
Now say MyAS3DocClass has a public function within it:
public function hitme():void
{
trace('ouch');
}
My question is, how can I call place a call to that function hitme()
within the call_from_outside()
function between the <fx:Script>
tags?
The reason why I'm doing this is because I have some flash swfs that I load into another swf file and I can access the top level public functions of those flash swfs, however the top level of the flash builder/flex swfs is the main mxml file not my fake as3 class root. Of course I should think there is a way for me to access the as3 class methods of the mxml component but for keeping things with the same structure, I can bypass having to modify my loader scripts. Anyone have an ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(请注意,我在下面更改了您的结束标签。如果您忽略它,将会引起头痛)。
看起来您需要做的就是向您的自定义类添加一个 ID:
然后您可以简单地将该值作为脚本标记内的变量名称进行访问:
原因如下!
当您为 MXML 文件中的标记分配 ID 时,与将
public var:
添加到 AS 文件是一样的(当然这是由编译器完成的,所以你不需要关心)。一旦您将 ID 分配给 MyAS3DocClass,它就会立即成为可公开访问的变量。一旦它是一个可公开访问的变量,它就可以在公共、受保护、内部、命名空间和私有函数中使用!如果您不喜欢这个想法并且您的自定义类是 DisplayObject,您也可以这样做:
然后,在脚本标记中:
(Please note that I changed your closing tag below. It will cause headaches if you omit that).
It looks like all you need to do is add an ID to your custom class:
And then you can simply access that value as a variable name inside the script tag:
AND HERE'S WHY!
When you assign an ID to a tag in an MXML file, it is the same thing as adding
public var <your-variable-name-here>:<tag-class>
to an AS file (of course that's done by the compiler, so you shouldn't need to care). Once you assign an ID to MyAS3DocClass, it is instantly a publicly accessible variable. Once it is a publicly accessible variable, it can be used in public, protected, internal, namespace, and private functions!If you don't like this idea and your custom class is a DisplayObject, you can also do this:
then, in the script tag: