简体   繁体   English

Flash AS3将变量从子类传递到父类

[英]Flash AS3 Passing Variable From Child Class to Parent

I need to somehow do this, I know that I can use a localconnection, but I don't think that that's really necessary. 我需要以某种方式做到这一点,我知道我可以使用本地连接,但是我认为那不是真的必要。 I should be able to do something just as simple as parent.foo() to call the function foo. 我应该能够做像parent.foo()一样简单的操作来调用函数foo。

This is my code that isn't working. 这是我的代码工作。

        if(this.parent)
            (this.parent as MovieClip).foo();

The error I'm getting is saying that the function foo doesn't exist, but I am sure that it does exist. 我收到的错误是说函数foo不存在,但是我确定它确实存在。

I don't know if this matters, but the child is a class, not a SWF or anything, I'm only saying this though because when I Googled possible solutions, the methods people use are with SWF's. 我不知道这是否重要,但是孩子是一个班级,而不是SWF或任何东西,我只是说这是因为,当我搜索可能的解决方案时,人们使用的方法就是SWF。

Anyways, thanks for your help 无论如何,谢谢您的帮助

Despite the correctness of abhinav answer, I would encourage you to use an Event or Signal instead of this method. 尽管abhinav回答是正确的,但我还是鼓励您使用事件或信号代替此方法。 You will couple your classes to tightly with this approach. 您将使用这种方法将您的课程紧密结合在一起。

MovieClip nested: MovieClip嵌套:

//use bubbling to enable event listening in deeper displayObjects
dispatchEvent(newEvent("somethingHappened", true));

MovieClip parent MovieClip父级

child.addEventListener("somethingHappened", handlerFunction);

function handlerFunction(event:Event):void
{
     this.foo();
}

I believe that your class is added to the parent as a child. 我相信您的班级是作为孩子添加到家长的。

Then, you can just say YourClass(parent).foo(); 然后,您可以说YourClass(parent).foo();

Additionally, any deeply nested class can also call any parent in the hierarchy of display objects. 此外,任何深度嵌套的类都可以在显示对象的层次结构中调用任何父级。 For example : ParentOfParentClass(parent.parent).foo(); 例如: ParentOfParentClass(parent.parent).foo();

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

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