As3 - getting library characters from Assets class

I created an assets.swf file in which I want to save all my characters. Then I created an Assets class that does the nesting. It looks like this:

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        public static var MyBox:Class;

        public function Assets() 
        {

        }

    }

Now, in some other class, I want to create a new box:

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = new Assets.MyBox();
    addChild(myBox);

}

I know this is wrong, and I get "TypeError: Error # 1007: attempt to create on non-constructor". How to access assets in Assets class?

+3
source share
4 answers

Edit: I think you will find the answer here .

The following applies to using classes from SWF loaded with the class Loader.

private function onLoad(e:Event):void
{
 var domain:ApplicationDomain = LoaderInfo(e.target).applicationDomain;
 var Type:Class = domain.getDefinition("pack.MyComponent") as Class;
 var myBox:MovieClip = new Type();
 addChild(myBox);
}
+2
source

- SWC , .

, AS SWC, .

, SWF, .

+1

.swf, :

public class Assets extends MovieClip
    {
        [Embed(source="assets.swf", symbol="MyBox")]
        private static var _MyBox:Class;
        public static function get NewBox():MovieClip {
            return new _MyBox();
        }
    }
...

import com.company.Assets;
...
public function Game() 
{

    var myBox:MovieClip = Assets.NewBox;
    addChild(myBox);

}

, Action Script, .swc. , MyBox com.company.Assets.MyBox, , :

import com.company.Assets.*;
...
public function Game() 
{

    var myBox:MovieClip = new MyBox();
    addChild(myBox);

}
0

You can create new objects in the Asset .. class (which also gives you the ability to create a pool of reusable assets)

sort of:

public class Assets extends MovieClip
{
    [Embed(source="assets.swf", symbol="MyBox")]
    private static var MyBox:Class;

    public static function getNewBox():DisplayObject {
        return new MyBox();
    }

    public function Assets() 
    {

    }
}
0
source

Source: https://habr.com/ru/post/1718414/


All Articles