【发布时间】:2011-10-24 14:23:34
【问题描述】:
我正在尝试将通过 Flash IDE 拖放到 MovieClip 中创建的实例替换为实际类,以便我可以将它们添加到游戏循环中并将它们作为实际实体。换句话说,我正在尝试创建一种简化的方式,让开发人员可以直观地将他们的实体添加到我正在开发的平台引擎中。
这是我第三次尝试它,我完全被卡住了。
下面的代码循环播放一个包含导出符号的影片剪辑,该符号带有一个链接到它的名为MyEntity 的类。但是,它失去了对BaseClass 的扩展名,因此在编译时不会移动。
它继承:MovieClip > BaseClass > MyEntity。但是,当使用 IDE 编译时,它会忽略 BaseClass 而只是执行MovieClip > MyEntity。
我的代码旨在查找和存储MyEntity 的位置,将其从容器movieclip 中删除,添加它的全新实例(基础正确),然后将该新实例设置为与原创。
for ( var i:int = 0; i < LayerInIDE.numChildren; i++ )
{
// first we want to get all the display objects in the layer
// these are objects that were placed from within the Flash IDE (ie. dragged and dropped into the MovieClip
var original:DisplayObject = LayerInIDE.getChildAt( i );
// we want to get the class of the display object so we can recreate it as a specific entity class that it
var originalName:String = getQualifiedClassName( original );
var originalClass:Class = getDefinitionByName( originalName ) as Class;
// debug trace, see output below
trace( originalName, originalClass, originalClass is BaseClass );
// filter out movieclips
if ( originalClass != MovieClip )
{
// remove the original
LayerInIDE.removeChild( original );
// recreate the class with the correct extension
var newEnt = originalClass();
newEnt.x = original.x; newEnt.y = original.y;
LayerInIDE.addChild( newEnt );
}
}
这不起作用。
它输出Game.entities::MyEntity [class MyEntity] false。 MyEntity 是一个适当的类,并且确实从 BaseClass 扩展。但是,问题是 IDE 奇怪地删除了对基类的引用——就好像 MyEntity 从来没有基类一样。我似乎无法重新创建它,因为获取对类的引用也返回 MyEntity 从未有过 BaseClass。但是,如果我输入 var newEnt = MyEntity(); 而不是通过 getDefinitionByName 获取类名,它可以正常工作并从 BaseClass 扩展。
我需要它从 BaseClass 扩展,因为这是我的游戏引擎中所有实体都需要使用的主类。
有什么想法吗?还是有更好的办法?
【问题讨论】:
-
库中 MyEntity 剪辑的链接属性如何?你能上传截图吗?你如何创建你的类的第一个对象?
-
当然,MyEntity 的属性窗口如下所示。这也是code for MyEntity 和BaseClass。这些被削减只是为了证明这个问题。这就是LayerInIDE contains。在进一步研究这个问题后,我相信这是上课的问题。它似乎删除了对象的所有属性。
-
您可以尝试将 MyEntity 设置为 BaseClass 并将任何内容设置为“Class”。
标签: flash actionscript-3