簡體   English   中英

在AS3 / Flash CS5中全局導入類

[英]Import Class Globally in AS3/Flash CS5

我在ActionScript 3,Flash Professional CS5.5和AIR 3中有一個項目。

我有幾個需要訪問的自定義類。 目前,我可以通過傳統方法成功訪問:

import trailcrest.core.core;
var Core:core = new core();

但是,我在代碼中實現了一個實現...這將創建該類的COPY,並且無法訪問原始代碼。 當然,這是一個問題-我需要一個符號的腳本來修改類中的變量,而另一個符號的腳本可以訪問更改后的變量。 顯然,現在還沒有發生。

我該怎么做? 我應該以某種方式為該類創建一個“公共變量”嗎(盡管我需要有關如何操作的說明...我不能在階段或符號中使用“公共變量”)? 有什么方法可以直接訪問該類嗎?

救命! 並預先感謝。

使用全局變量通常被認為是不好的做法。 它通常導致代碼缺乏靈活性,並且在例如進行一些更改或修改時容易中斷。

import trailcrest.core.core;
var Core:core = new core();

這可以!

如果對象需要更改Core中屬性的值,則只需要通過調度事件來通知Core值的更改。

 var object:MovieClip = new MovieClip();
 object.dispatchEvent ( property );

在這種情況下,將Core用作您的文檔類似乎很有意義,在這種情況下,它將知道您應用中的所有對象,無論是孩子,大孩子等。

如果不是您的文檔類,那么您可以...

 //In the Document Class
 var Core:core = new Core();
 var object:MovieClip = new MovieClip();

 // CoreEvent being a Custom Event
 object.addEventListener( CoreEvent.CHANGE , changeListener );
 addChild( object );

 //in another part of the Document Class
 //after a value has changed
 object.dispatchEvent ( property );

 //a method of the Document Class
 private function changeListener( event:CoreEvent ):void
 {
      var propNewValue:Object = event.property;
      //If you're using a singe instane of Core in the Document
      //Class, any other symbol can now access the new value.
      core.property = propNewValue;
 }

如果對象是在應用程序的其他部分中創建的,則始終可以將Core實例作為參數傳遞。

  //In the Document Class
  var newobject:MovieClip = new CustomClass( core );

編輯

如果您發現此答案令人困惑,則可能應該閱讀有關OOP的基本原理以及AS3中的事件分發的信息。 令人感興趣的可能是信號庫 ,它是事件分派的不錯替代方案。

總體思路是避免將對象綁定到Singleton或任何類型的Global變量。 從理論上講,對象應該只了解自身,並且可以使用對象的事件在它們之間進行通信。

這可以通過使用Singleton設計模式很好地完成。 這是示例AS3實現:

package   {

public class SomeClass {

    /** Singleton instance */
    private static var instance : SomeClass;

    /** This instance variable will be accesible globaly by calling it SomeClass.getInstance().somePublicVar */
    public var somePublicVar    : * ;

    /**
     * Get singleton instance of class
     * @return  singleton instance  SomeClass
     */
    public static function getInstance () : SomeClass {
        return SomeClass.instance ? SomeClass.instance : ( SomeClass.instance = new SomeClass() );
    }

}

}

最好的方法是創建一個稱為“單例”的東西。 簡單來說,單例是靜態類,沒有靜態類的所有缺點。 它使全局一個實例(或單個副本)可用,然后它將像常規實例一樣工作(因為有)。

通過使用靜態變量和函數,可以實現單例。 靜態變量/函數是的一部分,而不是實例。 因此,每個變量只能有一個(只有一個類),並且它們都可以全局訪問。 內置的Math類是靜態函數和屬性的一個很好的例子。 您將獲得Pi的價值,如下所示:

Math.PI

不像這樣:

var math:Math = new Math();
math.PI

如您所見,具有方法的是類。 我們可以通過提供一個靜態的getInstance()函數來使它成為單例,該函數可以全局訪問並始終返回同一對象。 這是單例的示例實現:

package {

    public class SingletonSample {

        // The singleton instance
        private static sharedSingleton:SingletonSample = null;

        // The constructor. AS3 doesn't allow for private constructors
        // so we have to protect it manually
        public function SingletonSample() {
            if (sharedSingleton != null)
                throw new Error ("SingletonSample cannot be created with the new keyword. Use getInstance() instead.");
        }

        // The method that will get the actual instance
        public function getInstance():SingletonSample {
            if (sharedSingleton == null)
                sharedSingleton = new SharedSingleton();
            return sharedSingleton;
        }

    }

}

除了樣本中定義的那些方法和變量之外,該類的其余部分都可以正常編程。 然后,當您想在代碼中使用該類時,不要這樣做:

var instance:SingletonSample = new SingletonSample();
instance.doAThing(instance.aProperty);

做這個:

var instance:SingletonSample = SingletonSample.getInstance();
instance.doAThing(instance.aProperty);

實際上,當您只是快速調用方法時,根本不需要創建局部變量。 只是做這樣的事情:

SingletonSample.getInstance.aQuickFunction();

只要已導入SingletonSample類,這在全球范圍內都是可用的。 這種設計模式造就了出色的“經理”類,因此很可能會滿足您的需求。 但是請記住,單例通常不利於實際操作對象。 如果願意,可以將它們用作提供對其他事物(一種“中間人”類)的引用的管理器 但是,如果使用得當,它們可以成為程序員中強大而便捷的工具。

您可以考慮使用靜態類變量,例如:

package trailcrest.core {
    import trailcrest.core.core;
    public class YourCustomClass {
        public static var coreReachableFromAnywhere:Core //THE STATIC VARIABLE FOR CORE
    }
}

然后在您的孫代碼中:

import trailcrest.core.YourCustomClass;
yourCustomClass.coreReachableFromAnywhere = new Core();
yourCustomClass.coreReachableFromAnywhere.someMethod() ...

編輯

當然,按照其他人的建議添加單例類型的方法將使其變得更加簡潔,我也將對他們的答案進行投票。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM