I'm building a Flex project with a lot of embedded bitmaps and such. The usual method for getting to the bitmaps in Actionscript seems to be to do something like
[Bindable] [Embed(source = '../lib/WhiteFencePost.png')]
private static var clsObstacleFencePost : Class;
var bitmap : BitmapAsset = new clsObstacleFencePost();
I've already got several dozen of these things, and I can easily see ending up with hundreds of them by the time the project's done. Is there some way that I can avoid creating a Class for every bitmap?
Try to create an assets manager with static classes like this.
class AssetManager
{
[Bindable]
[Embed(source = '../lib/WhiteFencePost.png')]
public static var WhiteFencePost:Class;
[Bindable]
[Embed(source = '../lib/BlackFencePost.png')]
public static var BlackFencePost:Class;
}
Then you can use the images like this
myImage1.source = AssetManager.WhiteFencePost;
myImage2.source = AssetManager.WhiteFencePost;
myImage3.source = AssetManager.WhiteFencePost;
myImage4.source = AssetManager.BlackFencePost;
You do not need to define a new instance of the Class images that you want to use.
There are ways... Pack all files into zip - embed zip - unpack with any zip library for actionscript, then Loader.loadBytes pic needed. Zip contents can be enumerated, so if you know what to do with files by name, you don't even need file list in application.
Related
I am making an application to test art from a game I volunteered for. Right now the example I am posting will only touch the armors but the loading process is the same throughout the program. I have a movieclip ready to hold the loaded file but it adds it to the container via the class. It works how it should however my issue is that if you use another file with the same classes then it will default to the first file loaded. Even i use loaderr.unloadAndStop() and remove everything from the stage, it will always load the first file that corresponds to the class I am loading by. Since the armor pieces are loaded by class it makes it a hassle to test multiple changes to an armor file without changing the classes on each export. Here is an example of the code that is being used and I am curious if there is any way that I can improve this. `
public class Test extends MovieClip
{
public var mcChar:Display;
public var btnTest:SimpleButton;
public var btnTest2:SimpleButton;
public var ldr:Loader = new Loader();
public var strSkinLinkage:String;
public var strGender:String;
public function Test()
{
btnTest.addEventListener(MouseEvent.CLICK, TestP);
btnTest2.addEventListener(MouseEvent.CLICK, TestP2);
}
public function TestP(e:MouseEvent)
{
mcChar = new Display();
stage.addChild(mcChar);
mcChar.x = 789.6;
mcChar.y = 604.75;
mcChar.width = 667.15;
mcChar.height = 478.55;
strSkinLinkage = "CNC";
strGender = "M"
this.ldr.load(new URLRequest("CNC.SWF"), new LoaderContext(false, ApplicationDomain.currentDomain));
this.ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
public function TestP2(e:MouseEvent)
{
mcChar = new Display();
stage.addChild(mcChar);
mcChar.x = 789.6;
mcChar.y = 604.75;
mcChar.width = 667.15;
mcChar.height = 478.55;
strSkinLinkage = "CNC";
strGender = "M"
this.ldr.load(new URLRequest("CNC2.SWF"), new LoaderContext(false, ApplicationDomain.currentDomain));
this.ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
public function onLoadSkinComplete(e:Event):*
{
var AssetClass:Class;
try
{
AssetClass = (getDefinitionByName(((strSkinLinkage + strGender) + "Head")) as Class);
mcChar.head.addChildAt(new (AssetClass)(), 0);
}
catch(err:Error)
{
AssetClass = (getDefinitionByName(("mcHead" + strGender)) as Class);
mcChar.head.addChildAt(new (AssetClass)(), 0);
};
AssetClass = (getDefinitionByName(((strSkinLinkage + strGender) + "Chest")) as Class);
chest.addChild(ldr.content (AssetClass)());
mcChar.chest.addChild(new (chest)());
this.ldr.contentLoaderInfo.removeEventListener(Event.COMPLETE, this.onLoadSkinComplete);
}
}
`
I don't think its well formatted on this site but this is the core code. I have separate removal functions and my imports are all there. Like I said I cant seem to get it to format correctly. This is my test scenario and isn't my full dynamic tester where I can choose the file. Any help in figuring out how to use the most recent file is appreciated. Also for some background I am more of a self taught novice in as3.
When it gets to loading and unloading assets in AS3, there are several things to learn.
ApplicationDomain is a container for class definitions. The getDefinitionByName(...) method is basically the same as calling the ApplicationDomain.getDefinition(...) on the current ApplicationDomain (or maybe on the main ApplicationDomain, I never tried to do it in the loaded content). As the side result, you cannot have two classes with the same names inside the same ApplicationDomain (or rather you can, but one of them is inaccessible, who knows).
When you load another SWF which falls into the "same domain" category (same www domain, or same/nested local folder), AS3 automatically mixes all the definitions from the loaded SWF into the main ApplicationDomain. If you are willing to have some advanced control over loading/unloading stuff, or/and there are "skin" libraries that have similar sets of classes, you need to put the loaded files into separate ApplicationDomains or their definitions will collide and the result will be unpredictable (yet obviously not satisfactory).
The Loader.load(...) method has a second argument that allows you to do so:
// If there are no mandatory constructor arguments,
// you are free to omit the () brackets. I like doing so.
var aLoader:Loader = new Loader;
var aRequest:URLRequest = new URLRequest("mylibrary.swf");
// Documentation states that passing no argument here is
// the same as passing ApplicationDomain.currentDomain.
var childDomain:ApplicationDomain = new ApplicationDomain;
var aContext:LoaderContext = new LoaderContext(false, childDomain);
aLoader.load(aRequest, aContext);
Thus, when external SWF library is loaded, you can obtain its classes/definitions as following:
var aClass:Class;
// If you get things from the loaded SWF's Library
// then it is Sprite or MovieClip for the most cases.
var anAsset:Sprite;
aClass = aLoader.contentLoaderInfo.applicationDomain.getDefinition("MyAssetClass") as Class;
anAsset = new aClass;
When you do not longer need some of the loaded libraries, you call the Loader.unloadAndStop(...) method on the relevant Loader instance. Combined with the loading SWF into separate ApplicationDomain you can be sure that all of the loaded content (graphics, classes, sounds) is unloaded, destroyed and removed (that one I actually checked):
// Passing "true" also forces the Garbage Collector
// to actually do its job for a change.
aLoader.unloadAndStop(true);
I'm trying to add generic content while embedding my game assets but came up with nothing so far. What I usually do is:
public final class AssetsManager
{
[Embed(source="../assets/game1/icon.png", mimeType = "image/png")]
private static const Icon:Class;
}
However, I want to do something like this:
public final class AssetsManager(gameName:String)
{
[Embed(source="../assets/"+gameName+"/icon.png", mimeType = "image/png")]
private static const Icon:Class;
}
I'm not able to transfer params via the class, so I believe there's another way.
Embedding happens at compile-time so this isn't possible. Instead, you can load in assets at runtime, for example using a URLLoader as a ByteArray, or Loader as Bitmap, or a Flex Image control.
i have a little problem with getDefinitionByName.
My purpose is to instantiate an FXG object(Number10.fxg) in a document mxml on runtime.
The name of the Class is in a string variable that is used by getDefinitionByName
to return the name of the class to insantiate. The code doesn't work even if doesn't send an error message. The code is as follows:
import assets.Number10;
import flash.utils.getDefinitionByName;
import mx.core.IVisualElement;
private function onClick(event:MouseEvent):void
{
var value:String = "Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:IVisualElement = new ClassDefinition() as IVisualElement;
this.contentGroup.addElement(ten);
}
I tried also with... var ten:IVisualElement = new ClassDefinition();
but nothing. It Doesn't work!
Please, Help me!
First of all, i refer to the adobe documentation pages that covering the topic so telegraphic. Here it is:
Option includes class [...]
Description Links one or more classes to the resulting application SWF file, whether or not those classes are required at compile time.
To link an entire SWC file rather than individual classes, use the include-libraries option.
Ok.In Flash Builder i go to the Additional compiler arguments where there is just this option
-locale en_US
So i add my option under this
-includes class = assets.Number10
or
-includes class assets.Number10
or
-includes class Number10
When the application runs i get the Error #2032.
I think that the option declaretion is wrong. I do not have a good reference for using option.
So...Help me!
How can i declare the Number10 class or the assets package with the other fxg object using the includes class option?
Ok! I find the solution...
Is to put a reference to Number10 class somewhere in the code, for instance:
import assets.Number10;
import flash.utils.getDefinitionByName;
import spark.core.SpriteVisualElement;
//case1
var myNumber:Number10;
//or
//case2
Number10;
private function onClick(event:MouseEvent):void
{
var value:String = "assets.Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:SpriteVisualElement = new ClassDefinition() as SpriteVisualElement;
this.contentGroup.addElement(ten);
}
and the code works :-)
This is a problem that comes from the way that Flex compiles its code. Flex compiles its code so that if a class is not used, it will keep this class off the final compiled program.
But the problems are not over yet! If i have hundreds of Fxg objects that could be instantiate, declaring all classes is little difficult and tedious.
So, how i can delclare in one time all classes of a package?
You can add classes to SWCs and SWFs using the include and includeClasses compiler options. Using these, you don't have to reference the classes in the code. Consult the documentation for proper usage.
Be sure to use the fully qualifed class name.
Also, the approach of casting your FXG class as an IVisualElement is new to me. I thought you had to use real classes in casting and the sort. Try using a SpriteVisualElement.
private function onClick(event:MouseEvent):void
{
var value:String = "assets.Number10";
var ClassDefinition:Class = getDefinitionByName(value) as Class;
var ten:IVisualElement = new ClassDefinition() as SpriteVisualElement.;
this.contentGroup.addElement(ten);
}
I have two SWFs: main.swf and external.swf. main.swf needs to access some methods in external.swf, so it loads external.swf into itself and uses getDefinitionByName("package.Class") to access the class and one of its methods:
var ExternalClass = getDefinitionByName("package.Class") as Class;
var ClassInstance = new ExternalClass();
var NeededFunction:Function = ClassInstance["NeededFunction"] as Function;
var response:String = NeededFunction(param);
Now, I need to extend the functionality of NeededFunction (which is a public method)... I know it's possible to override public methods, but how would I go about this with a dynamically loaded class?
I was thinking I could do something like this, but it doesn't work:
var ClassInstance["NeededFunction"] = function(param1:uint):String {
var newString = "Your number is: "+param1.toString(); //New functionality
return newString;
}
Another way to deal with this could be to have the classes in a package that's accessible by both SWFs. Just add the classes' root folder to your Actionscript path .
Instead of getting a class by using getDefinitionByName , you simply import it. As for overriding , you can create a Class that overrides one of the classes , or you can create an Interface.
import com.yourlocation.ExternalClass;
var external:ExternalClass = new ExternalClass();
Using FlashDevelop this is pretty simple to fix.
Right click your included swc from the Project list.
Choose options then "include library (complete library)".
..you can now use getDefinitionByName to get a unreferenced class from your swc file.
I'm creating a game where a lot of images are being used in Actionscript / Flex 3 (Flash). Now that I've reached the designer stage, I have to work out a structural way of using embedded images (which have to be manipulated with rotation, color, etc.).
Unfortunately, after investigating a bit, it looks like you have to manually embed images before you can use them. I currently have it setup like this:
Resource.as class file:
package
{
public final class Resource
{
[Embed (source="/assets/ships/1.gif" )]
public static const SHIPS_1:Class;
}
}
So, just for one ship I so for have to:
Put the image in the correct folder with the correct name
Name it in the same way in the Resource.as file
Create the constant with the same name in the Resource.as file
Even though this should all be possible by simply putting the file in a specified folder.
To make things even worse, I still have to call it using:
var test:Bitmap = new Resource.SHIPS_1();
There must be better ways to handle resources when creating very huge applications? Imagine I need thousands of images, this system simply wouldn't fit.
If you need to handle a large number of resources you can follow these 3 steps:
Place them in an uncompressed zip archive
Embed the zip file as binary data:
[Embed(source = 'resources.zip', mimeType = 'application/octet-stream')]
Access the resources using FZip
If you choose a different method that involves loading external files be aware that some flash game websites require the games they host to be contained within a single swf file.
instead of
var test:Bitmap = new Resource.SHIPS_1();
Use
myImage.source = Resource.SHIPS_1;
The embedding is correct. :D the way you use it is wrong :)
Adrian
This is really what Flash CS4 is for. Your way seems fine to me though - although I wouldn't use all caps for a class name even if it is a constant. Just put your head down and get copy-pasting!
Alternatively you could load the files at runtime.
This is old but since I stumbled across it searching for something different I'll write here for future generations : )
I use a different approach. I create a swf movie with flash professional and import all graphics in it and then mark them all for "Export for ActionScript".
Compile the swf and in your main project embed only the swf and access all the graphics through it...
I find this approach much more organized. Why write the whole resources class when you can make it by importing files right? ;)
I just watched this great tutorial on the Starling framework:
http://www.hsharma.com/tutorials/starting-with-starling-ep-3-sprite-sheets/
It sounds like spritesheets are exactly what you're looking for:
You bundle all your individual textures into one big texture that is called spritesheet and create an xml file that contains information where the textures are within the spritesheet. In order to do that you can use this tool:
http://www.codeandweb.com/texturepacker
I'm not sure if you can use it for commercial projects and the amount of textures you're speaking of doesn't sound like you're doing this just as a hobby, so you might want to check the license. There's a pro version available, too.
Texturepacker creates two files: spritesheet.png and spritesheet.xml. You just copy them into your project.
Then you add this code to one of your classes.
private static var gameTextureAtlas:TextureAtlas;
[Embed(source="../media/graphics/mySpriteSheet.png")]
public static const AtlasTextureGame:Class;
[Embed(source="../media/graphics/mySpritesheet.xml", mimeType="application/octet-stream")]
public static const AtlasXmlGame:Class;
public static function getAtlas():TextureAtlas
{
if(gameTextureAtlas==null)
{
var texture:Texture=getTexture("AtlasTextureGame");
var xml:XML=XML(new AtlasXmlGame());
gameTextureAtlas=new TextureAtlas(texture,xml);
}
return gameTextureAtlas;
}
Now you can access all the textures of the spritesheet by calling
YourClass.getAtlas().getTexture("name");
It's simply awesome. When you're using texturepacker the filename of each of the sprites you bundled into the spritesheet becomes its texturename.
This is probably too late to help you, but I hope that future visitors can profit from this elegant solution.
I would like to emphasize that this answer is basically an excerpt from sharma's tutorial. I even felt free to reproduce the code he used in his screencast. All the credit goes to him
It depends how big your individual images are but you could put them all in one image like a spritesheet. If you want to draw a particular ship use the correct xy offset in the image for that ship and use copyPixels to draw it to your bitmap.
package
{
public final class Resource
{
[Embed (source="/assets/ships/1.gif" )]
public static const SHIPS_1:Class;
}
}
[Embed (source="/assets/ships/1.gif" )]
public static const SHIPS_1:Class;
I like to do my Library classes like this.
I took GSkinners code for the singleton: http://gskinner.com/blog/archives/2006/07/as3_singletons.html
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
public class Lib
{
/*
Make this an Singleton, so you only load all the images only Once
*/
private static var instance:Lib;
public static function getInstance():Lib {
if (instance == null) {
instance = new Lib(new SingletonBlocker());
}
return instance;
}
public function Lib(p_key:SingletonBlocker):void {
// this shouldn't be necessary unless they fake out the compiler:
if (p_key == null) {
throw new Error("Error: Instantiation failed: Use Singleton.getInstance() instead of new.");
}
}
/*
The actual embedding
*/
[Embed(source="assets/images/someImage.png")]
private var ImageClass:Class;
private var _imageClass:Bitmap = new ImageClass() as Bitmap;
[Embed(source="assets/images/someOtherImage.png")]
private var OtherImageClass:Class;
private var _otherImageClass:Bitmap = new ImageClass() as Bitmap;
public function get imgClass():Bitmap{
return _imageClass;
}
public function get imgClassData():BitmapData{
return _imageClass.BitmapData;
}
public function get otherImageClass():Bitmap{
return _otherImageClass;
}
public function get otherImageClassData():BitmapData{
return _otherImageClass.BitmapData;
}
}
}
internal class SingletonBlocker {}
[Embed (source="/assets/images/123.png" )]
public static const className:Class;
Good idea, lhk
That is nice solution like Source-Engine with vtf and vmt
vtf = image
vmt = script ( like xml or javascript )
Good i would like to suggest for TexturePacker, TexturePath or TextureTarget :P
Thanky ou for tip.
For example:
mytexture.js:
xml or javascript:
function mytexture(){ basedir = "/assets/mytexture.png", normalmap =
"/assets/mytexture_bump.png", normalcube ) [ 1, 1, 1 ] };
I don't think because default texture gets error somewhere mytexture.png doesn't exists than it happens again :)
[Embed(source="../assets/editors/error_texture.png")] public static
const ERROR_TEX:Class; ...
How do i know because Actionscript 3 should "read" to javascript like jsBirdge or ExternalInterface.call();
Is it possible?