2011-08-11 25 views
1

得到適當的類,我試圖取代通過拖放到動畫片段通過Flash的IDE與實際類,所以我可以將它們添加到遊戲循環,讓他們作爲實際的實體創建的實例。換句話說,我試圖創建一種簡化的方式,讓開發人員可以將他們的實體可視化地添加到我正在處理的平臺引擎中。無法從Flash IDE

這是我的第三次嘗試,我完全卡住了。

下面的代碼遍歷包含有鏈接到它命名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的延伸,因爲這是主類在我的遊戲引擎所有的實體需要使用。

任何想法?或者,還有更好的方法?

+0

您的鏈接屬性對於庫中的MyEntity剪輯的外觀如何?你能上傳截圖嗎?你如何創建你的班級的第一個對象? – pkyeck

+0

當然,以下是[MyEntity](http://i.imgur.com/BUWBN.png)的屬性窗口的外觀。這裏還有[MyEntity的代碼](http://i.imgur.com/6c8RV.png)和[BaseClass](http://i.imgur.com/qiOho.png)。爲了證明這個問題,這些被削減。這是[LayerInIDE包含](http://i.imgur.com/j3fKD.png)。 在進一步研究這個問題後,我相信這是獲得課程的一個問題。它似乎放棄了對象的所有屬性。 – MacDGuy

+0

您可以嘗試將MyEntity設置爲BaseClass,並將任何內容設置爲「Class」。 – pkyeck

回答

0

多經過修修補補,一般破Flash中,我得到了解決我的問題。感謝Stephen Braitsch的幫助讓我走向正確的方向。

好了,該解決方案是一個有點複雜(和哈克),但都繞我以前討論的問題。

首先,您需要掃描特定動畫片段中所有顯示對象的主函數。我已經命名了我的LayerInIDE

有兩個功能需要。 ReplaceEntsRecreateReplaceEnts掃描所有對象並確定它們是否從BaseClass延伸。如果他們這樣做,它會繼續並通過Recreate引發他們。Recreate獲取原始類別和位置並重新創建類,以便我們在運行時正確訪問資產。

import Game.entities.MyEntity; 
import Game.entities.BaseClass; 
import Game.entities.Layer; 

var LayerInIDE:Layer = new Layer(); 
stage.addChild(LayerInIDE); 

ReplaceEnts(LayerInIDE); 

function ReplaceEnts(layer:Layer):void 
{ 
    for (var i:int = 0; i < layer.numChildren; i++) 
    { 
     // Get the display object. 
     var original:DisplayObject = layer.getChildAt(i); 

     // Make sure we're only doing this to objects that extend from BaseClass 
     if (original is BaseClass) 
     { 
      // Get the original class 
      var originalName:String = getQualifiedClassName(original); 
      var originalClass:Class = getDefinitionByName(originalName) as Class; 
      // Remove the original 
      layer.removeChild(original); 

      // Recreate it with the original class 
      // Make sure we keep the original position and rotation. 
      var bc = Recreate(originalClass, original.x, original.y, original.rotation); 
      layer.addChild(bc); 

      // Test functions 
      trace(bc); // outputs: [object MyEntity] 
      bc.TestFunc(); // outputs: test 
      bc.BaseTest(); // outputs: base 
     } 
    } 
} 

// This is the core function that does all the work. 
// It recreates the entity with the proper class and sets it to its original position. 
function Recreate(entClass:Class, iPosX:int = 0, iPosY:int = 0, iRot:int = 0):BaseClass 
{ 
    var newEnt:BaseClass = new entClass(); 
    newEnt.x = iPosX; newEnt.y = iPosY; 
    newEnt.rotation = iRot; 

    return newEnt; 
} 

然後BaseClass包含:

package Game.entities 
{ 
    import flash.display.MovieClip; 

    public class BaseClass extends MovieClip 
    { 
     // The example function as mentioned above in the ReplaceEnts function. 
     public function BaseTest():void 
     { 
      trace("base"); 
     } 
    } 
} 

一旦是所有設置,我們需要也使MyEntity類:

package Game.entities 
{ 
    public class MyEntity extends BaseClass 
    { 
     // The example function as mentioned above in the ReplaceEnts function. 
     public function TestFunc():void 
     { 
      trace("test"); 
     } 
    } 
} 

只是最後一點,Games.entities.Layer是隻是一個簡單的MovieClip。我只爲此創建了自己的班級,以供組織之用。

這裏的解決方案很簡單。當用戶將從BaseClass延伸的對象拖入圖層時,該功能將在啓動時運行,並使用適當的類替換資產,並允許實體管理器接管並適當地處理資產。它支持我想要實現的功能,一個IDE驅動的關卡編輯器,可以自動完成將實體放置在所需位置的所有工作。

感謝所有的幫助傢伙。

0

所以,如果我理解正確的話,你現在要做的是讓人們拖庫資源上,你可以檢查並在運行時可能重鑄階段是什麼?例如,在IDE中,某人創建了一個只是藝術品的啞巴Sprite,將其拖動到舞臺上&將其設置爲導出爲「MyEntity」類。然後在運行時你想檢查它是否是「MyEntity」類型,因此你可以在BaseClass中定義注入功能。如果是這樣的話,你可以處理這個方式如下:

var do:DisplayObject = LayerInIDE.getChildAt(i); 
if (do is MyEntity){ 
    var bc:BaseClass = new BaseClass(do); 
    addChild(bc); 
} 

// then inside of BaseClass.as 

private var entity:MyEntity; 
public function BaseClass(view:MyEntity) 
{ 
    entity = view; 
    addChild(entity); 
// from here you can script your MyEntity object as needed. 
} 
+0

雖然這絕對有效並獲得了藝術作品,但這並不完全符合我的要求。我需要能夠使用從BaseClass擴展的任何類來完成此操作。換句話說,它可能不是MyEntity,它可能是MyCharacterEntity。我用你的例子遇到的另一個問題是,當你將它重寫爲BaseClass時,它會失去所有原始類的功能。 MyEntity具有使MyEntity成爲MyEntity的功能,而不僅僅是具有默認功能的BaseClass。儘管這肯定會讓我走向正確的方向。非常感謝你的幫助。 – MacDGuy