如何在当前类中调用不同类的方法。 AS3
我有一个类,我的文档类,名为 SilkRoadTweeter.as 我还有一个名为 User.as 的类 在我的 User 类中,我需要调用一个名为 nonce() 的方法 我已经尝试过这个,
trace(SilkRoadTweeter(root).nonce());
但我收到错误,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
I have a class, my document class, called SilkRoadTweeter.as
I also have a class called User.as
In my User class I need to call a method called nonce()
I have tried this,
trace(SilkRoadTweeter(root).nonce());
But I get the error,
TypeError: Error #1009: Cannot access a property or method of a null object reference.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您说nonce()函数位于SilkRoadTweeter类中,您需要从User类中调用它。您必须拥有对 SilkRoadTweeter 的引用才能调用其上的函数。该错误表明它不知道 SilkRoadTweeter 是什么。在我看来,使用 root 来获取引用很混乱,应该避免。我建议在创建 User 类时传递对它的引用。如果这不是一个选项,则将 SilkRoadTweeter 类上的函数 nonce() 设置为静态即可解决您的问题,只要该函数不需要访问 SilkRoadTweeter 的非静态属性。你说它所做的只是返回一个生成的数字,所以我猜它不需要访问非静态属性。如果您仍然感到困惑,我可以进一步详细说明我所说的内容。
You said that the nonce() function is in the SilkRoadTweeter class and you need to call it from the User class. You must have a reference to the SilkRoadTweeter in order to call functions on it. The error is saying it doesn't know what a SilkRoadTweeter is. Using root to get a reference is messy in my opinion and should be avoided. I would suggest passing a reference to the User class when you create it. If that is not an option, making the function nonce() on the SilkRoadTweeter class static would solve your problem, so long as the function doesn't need to access non-static properties of the SilkRoadTweeter. You said all it does is return a generated number so I would guess it doesn't need to access non-static properties. I can elaborate further on what I've said if your still confused.
编辑
我实际上忘记了最重要的一项,你可以调度事件!
编辑结束
您应该在 User 类中传递 SilkRoadTweeter 实例
例如:
如果将 User 类实例添加为 SilkRoadTweeter 类的子级,您还可以
做这个
Edit
I actually forgot one of the most important one, you could dispatch en event!
End of Edit
You should pass your instance of SilkRoadTweeter in the User class
For instance:
If the User class instance is added as a child of the SilkRoadTweeter class, you could also
do this
在这种情况下,“root”可能为空。
尝试调试一下看看。
In this case "root" is probably null.
Try debugging it to check.
尝试:
我感觉你需要这样做:
try:
I have feeling you need to do: