可以将对象添加到舞台的 AS3 全局类
所以我最近了解到,通过将一个类导入到我的主类中,我可以从任何其他类访问它的函数。但是......我在导入类中的函数之一需要将显示对象添加到舞台。我访问静态函数很好,但它无法将对象添加到舞台。它甚至似乎无法识别 addChild。这是因为它不在显示列表本身中吗?
我在这里缺少什么?大家会怎么解决这个问题呢。我是如此接近,却又如此遥远!
代码如下:
package {
import flash.display.Sprite;
import PopHandler;
public class MyMainClass extends Sprite {
public function MyMainClass():void {
PopHandler.LaunchPop();
}
}
}
这是导入的类,不会向舞台添加任何内容。
package {
import flash.display.Sprite;
public class PopHandler extends Sprite {
public function PopHandler():void {
}
public static function LaunchPop() {
var bp:BreakPop = new BreakPop();
bp.x = 500;
bp.y = 347;
addChild(bp);
}
}
BreakPop
是我图书馆中的一个项目。
提前致谢。
So I recently learnt that by importing a class into my main class I can access its functions from any other class. BUT.... one of my functions in the imported class needs to add display objects to the stage. I accessed the static function just fine but it can't add objects to the stage. It doesn't even seem to recognise addChild. Is this because it is not in the display list itself?
What am I missing here? How would you guys solve this issue. I was so close, yet so far!
Code is like this:
package {
import flash.display.Sprite;
import PopHandler;
public class MyMainClass extends Sprite {
public function MyMainClass():void {
PopHandler.LaunchPop();
}
}
}
This is the imported class that doesn't add anything to stage.
package {
import flash.display.Sprite;
public class PopHandler extends Sprite {
public function PopHandler():void {
}
public static function LaunchPop() {
var bp:BreakPop = new BreakPop();
bp.x = 500;
bp.y = 347;
addChild(bp);
}
}
}
BreakPop being an item in my library.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
扩展您的主文档类并添加 static var ,然后在 init 上添加 stage :
然后在应用程序中的任何位置您都可以访问 stage :
extend You main document class and add there static var , than assing stage on init :
and then anywhere in app You can access to stage :
由于您使用的是静态方法,因此您的
PopHandler
不是精灵的实例,因此无法访问stage
属性。如果您想将其保留为静态方法,那么您基本上可以跳过extends Sprite
并直接使用public class PopHandler {
这是一个简单的方法问题的解决方案:为什么不传递您想要添加
BreakPop
的DisplayObjectContainer
作为静态方法的参数?示例:
然后你这样称呼它(一些示例):
Since you're using a static method, your
PopHandler
isn't an instance of a sprite and therefore doesn't have access to thestage
property. If you want to leave it as a static method, then you can basically skip theextends Sprite
and just go forpublic class PopHandler {
That out of the way, here's a simple solution to your problem: Why not pass the
DisplayObjectContainer
you'd like to add theBreakPop
to as a parameter of your static method?Example:
Then you call it like this (some examples):