AS3全球一流的可添加对象阶段

编程入门 行业动态 更新时间:2024-10-24 16:27:02
本文介绍了AS3全球一流的可添加对象阶段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以,我最近才知道,通过导入类到我的主类,我可以从其它类访问它的函数。但....我在导入的类函数需要显示对象添加到舞台。我访问的静态函数得很好,但它不能在舞台上添加对象。它甚至不似乎承认的addChild。这是因为它不是显示列表本身?在

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是这样的:

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是在我的库中的项目。

BreakPop being an item in my library.

在此先感谢。

推荐答案

由于您使用的是静态方法,你的 PopHandler 不是一个精灵的一个实例,因此没有访问期属性。如果你想离开它作为一个静态方法,那么你基本上可以跳过扩展Sprite ,只是去了公共类PopHandler {

Since you're using a static method, your PopHandler isn't an instance of a sprite and therefore doesn't have access to the stage property. If you want to leave it as a static method, then you can basically skip the extends Sprite and just go for public class PopHandler {

这出的方式,这里有一个简单的解决问题的方法:为什么不通过的DisplayObjectContainer 你想添加的 BreakPop 来作为你的静态方法的参数?

That out of the way, here's a simple solution to your problem: Why not pass the DisplayObjectContainer you'd like to add the BreakPop to as a parameter of your static method?

例如:

public static function LaunchPop(target:DisplayObjectContainer) { var bp:BreakPop = new BreakPop(); bp.x = 500; bp.y = 347; target.addChild(bp); }

然后调用它像这样(举例):

Then you call it like this (some examples):

PopHandler.LaunchPop(this); // <-- adding to current object PopHandler.LaunchPop(root as DisplayObjectContainer); // <-- adding to root PopHandler.LaunchPop(stage); // <-- adding to stage

更多推荐

AS3全球一流的可添加对象阶段

本文发布于:2023-11-06 22:37:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1564866.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:对象   阶段   全球

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!