从 MXML 调用 ActionScript 包函数
如何从 MXML 调用 btn.as 中的函数?是否可以在不创建 btn 实例的情况下调用函数?
main.mxml 包含一个 Spark 按钮:
<s:Button text="Add Image"/>
btn.as 是一个包:
package {
public class btn extends Sprite {
public function btn() {
}
public function addImage():void {
var im:Image = new Image("background.png");
addChild(im);
}
}
}
How can I invoke a function in btn.as from MXML and is it possible to call a function without creating an instance of btn?
main.mxml which contain a Spark button:
<s:Button text="Add Image"/>
btn.as is a package:
package {
public class btn extends Sprite {
public function btn() {
}
public function addImage():void {
var im:Image = new Image("background.png");
addChild(im);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你到底想要什么有点令人困惑,但我会尝试一下。
我假设您想在单击按钮时在 btn 组件的实例上调用 addImage 函数。你可以这样:
如果你想要别的东西,你就必须详细说明。
是的,将其设为静态方法。像这样的事情:
然后你可以像这样调用静态方法:
需要注意的是,我不希望“addChild”在静态方法中做任何有用的事情。如果没有组件实例,则不在显示列表中;并且您的新“孩子”将永远不会被展示。事实上,没有办法引用新的孩子。我想你可以将一个容器传递给 addImage 函数并在那里添加孩子。从概念上讲是这样的:
不过,我最初对这样的方法持保留态度,因此在没有完全理解用例的情况下不会推荐它。
It is a bit confusing what exactly you're after, but I'll give it a shot.
I'm going to assume you want tocall the addImage function on an instance of the btn component when the button is clicked. You can it like this:
If you want something else, you'll have to elaborate.
Yes, make it a static method. Something like this:
then you can call the static method like this:
The caveat is that I don't expect "addChild" would do anything useful inside a static method. If there is no component instance, then it isn't on the display list; and your new "child" will never be displayed. In fact, there would be no way to reference the new child. I suppose you could pass in a container to the addImage function and add the child there. Conceptually like this:
I have initial reservations about an approach like that though, so would not recommend it without fully understanding the use case.