I'm loading swf externally. I need to access it's methods and variables. This is the code I'm using:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;
import flash.display.MovieClip;
var loader:Loader;
var req:URLRequest = new URLRequest("aaa.swf");
var mc:MovieClip;
createLoader();
function createLoader():void {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.INIT, onSWFLoad);
loader.load(req);
}
function onSWFLoad(e:Event):void {
mc = MovieClip(loader.content);
addChild(mc);
trace(loader.content)
}
EM is loaded SWF's document class. If questioned if variable gameEnded is declared publicly, the answer is yes. The game loads, but there is an error when calling the variable :
ReferenceError: Error #1069: Property gameEnded not found on EM__Preloader__ and there is no default value.
at main_fla::MainTimeline/onSWFLoad()
Check your preloader setting in the ActionScript settings panel of your game.fla - EM__Preloader__ looks to me like an auto-generated class (and not your main EM class).
Also, your EM class should implement a simple interface that you can cast your loader.content to (instead of MovieClip). This way, you can be sure that the methods you need are available, and your code is type-safe. Embed this interface into your preloader.fla instead of the EM class to prevent unnecessary bloating of your SWFs.
It turned out that I had to embed all the code in SWF (I was using some TLF in it)
Related
Is it possible to load on frame 2 a swf embed in another swf?
I need to produce a single, main swf that contains a sound swf. The sound swf contains exported sound files for instantiation in code. The sound swfs vary based on the version of the main swf hence the desire to embed at compile time. I currently embed the sound swf in the main swf code using an [Embed] metadata tag which all works. However, this seems to load the swfs on frame 1 and I'd rather it load on frame 2 after my preloader runs.
I've checked around the web and this question seems to get asked but never really answered.
To clarify, I am embedding a swf and its symbols in a main class with code like this:
[Embed(source = 'sound.swf', symbol = 'music')] public var music:Class;
The embed swf and symbols load on the initial main swf load. If I set the sound swf to export on frame 2, it does not compile. Library contents in the main swf load on frame 2 to allow the preloader in frame 1 to run. I'm trying to find out if there is a way to make the assets in the sound.swf to also load on frame2.
This seems to solve the issue for me. Sample code below. I embed a swf containing sound assets, but use "application/octet-stream" for the mimeType. Then later I can "load" the swf using the ActionScript Loader class. A symbol in the loaded swf can then be instantiated using "getDefinitionByName". Also, you can instantiate using "getDefinition" if you have a reference to the loaded swf, see the commented out line below. So far this works for me.
package
{
import flash.display.Loader;
import flash.display.MovieClip;
import flash.media.Sound;
import flash.events.Event;
import flash.system.LoaderContext;
import flash.system.ApplicationDomain;
import flash.utils.getDefinitionByName;
public class TestEmbed extends MovieClip
{
[Embed(source="EmbedAudio.swf", mimeType="application/octet-stream")]
public var EmbedAudio : Class;
public function TestEmbed()
{
super();
// load embed swf
var context : LoaderContext = new LoaderContext (false,ApplicationDomain.currentDomain);
context.allowCodeImport = true;
var loader : Loader = new Loader ();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loader_complete);
loader.loadBytes (new EmbedAudio (), context);
}
private function loader_complete(evt:Event):void {
// instantiate the class
/*var ClassDefinition:Class = evt.target.applicationDomain.getDefinition("k3n1_01") as Class;*/
var ClassDefinition:Class=getDefinitionByName("k3n1_01") as Class;
var k3n1_01:Sound = new ClassDefinition();
k3n1_01.play();
}
}
}
Note: Yes, I know that similar questions have been asked before. However, after following the answers in such questions I'm still stuck and can't find a solution to my problem.
I'm having a problem which requires adding DisplayObjects to the Flash stage. Since I have to Display elements of several different classes, I decided to create a class to work as an intermediary between the .as files and the addChild function called "Displayer" as shown below:
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Stage;
public class Displayer extends Sprite //I read somewhere that DisplayObject
//as an extension can't be used for this, so Sprite will have to do.
{
private var _stage:Stage;
function Displayer()
{
_stage = new Stage;
}
public function displayElement(displayable:DisplayObject)
{
_stage.addChild(displayable);
}
}
}
I compile it and there appears a problem that I don't understand: Error #2012: Can't instantiate Stage class. Evidently, something in this code is either missing or out of place, but since it's so straightforward I fail to see what the problem can be. I'm sure that it's not very complicated, I probably just need an outsider's perspective.
The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance.
refer a following docs.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Stage.html
package
{
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.display.Stage;
public class Displayer extends Sprite
{
var isAddedToStage:Boolean;
public function Displayer()
{
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE, init);
}
private function init(e:Event=null):void
{
removeEventListener(Event.ADDED_TO_STAGE, init);
isAddedToStage = true;
}
public function displayElement(displayable:DisplayObject):void
{
if(isAddedToStage)
this.stage.addChild(displayable);
}
}
}
You don't instantiate the Stage class, as the error says. Just like you cannot instantiate the DisplayObject class (which is why you have to extend Sprite).
Basically, you have two options here:
1) You add the child from a DisplayObjectContainer instance.
var displayerInstance:Displayer = new Displayer();
this.addChild( displayerInstance );
You would run this from a DisplayObjectContainer object that has already been added to the global stage. There is only a single stage in every project, even if you embed SWFs, the stage property of the SWF is actually the stage property of the top level application. So if you have this Displayer instance nested inside a class which is nested inside another class that is created in your main application, you would have to run "addChild" in each of those classes to get the Displayer to show.
2) You cheat. This is not recommended, at all. Basically, you pass in the stage object of an object when you instantiate the Displayer class.
var displayerInstance:Displayer = new Displayer( this.stage );
public function Displayer( stage:Stage ) {
this.stage = stage;
if ( this.stage ) {
this.stage.addChild( this );
}
}
This is a method that is good for adding Singletons to the stage (except there is not constructor for a Singleton). I created a profiler just before Christmas that was a Singleton (And later found Scout, damnit) that used this method for adding things to the stage when appropriate.
Again, that second option is not recommended for this situation, but it is a possibility.
As an aside, you should never add things directly to Stage, unless there is a clear reason for doing so (such as a popup). You should follow the display list methodology, where a DisplayObjectContainer adds another DisplayObject or DisplayObject container as a child and so on and so forth so that they are all connected to the TopLevelApplication.
Ok, I think instantiating a stage class won't do because as the as3 documentation says: "The Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance."
You should instead pass a reference to the Stage object to your Displayer class and you can get a reference to the stage object, as the docs say, via a display object instance.
So the constructor might now look like:
function Displayer( stage:Stage )
{
_stage = stage;
}
Assuming that the object which instantiates your Displayer is a child of the stage you can instantiate the Displayer by
displayer = new Displayer( stage );
If you use this approach there is no need for the Displayer class to extend anything or be added to the stage ( which is required btw in the approach of bitmapdata.com ).
There is always a simple solutions.if you need to add a child element into a stage from your class you can just pass the stage into your class as a object and add the child element into it, i did this for adding an image into my stage like this.
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.net.URLRequest;
import flash.display.Bitmap;
public class ImageLoader extends Sprite{
private var stageObj:Object; //create local variable to refarance stage
public function loadeimage(StageObject:Object, Url:String){ //StageObject will bring the stage refarance into the class
var reQuest:URLRequest = new URLRequest(Url);
loader.load(reQuest);
stageObj=StageObject; //make local refarance for stage inside the class
var image:Bitmap;
image=Bitmap(loader.content);
image.x = 100;
image.y = 100;
stageObj.addChild(image); // add whatever object into stage refarance and this means the real stage..
}
}
}
only the things with comments are important and you can save this file as ImageLoader.as and import it and use it like this.
import ImageLoader;
var IL:ImageLoader = new ImageLoader();
IL.loadeimage(this,"img.jpg");
its simple as that. i think this is what you have search for... good luck. (you can pass any container or parant container this or this.stage it doesn't matter your child will be a part of it.
Greatings!
I have yet another question concerning the loading of a .swf inside a existing one.
In my game. I have a introduction screen in which I load another .swf (which is a movie).
This all works fine and the URLRequest is this:
request = new URLRequest("Movie.swf");
As I said, this works fine. However when I copy my game.swf and movie.swf to a USB stick.
(I put them in the same directory to prevent other issues).
It doesn't seem to find the movie.swf.
Now I know that it has to do with the path given in the URLRequest and/or the publish settings. But I do not know how to make this so that it searches in the same directory
as the game.swf is in.
I hope you guys have an answer for this issue.
Thanks in advance,
Matti.
Matti, I believe Lukasz's comment is correct about it being a security error.
You can avoid this security error by embedding Movie.swf instead of using a Loader. If you do this, then at compile-time the Movie.swf needs to sit next to the Game.as file, and it will be included in the Game.swf (no need to deliver both files, just Game.swf).
The syntax is:
package
{
import flash.display.Sprite;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf")]
private var myMovieClass:Class;
private var myMovie:DisplayObject;
public function Game():void
{
myMovie = new myMovieClass();
// Technically myMovie is now a Loader, and once
// it's loaded, it'll have .content that's a
// MovieClipLoaderAsset, and .content.getChildAt(0)
// will be your MyMovie.swf main timeline.
}
}
}
Alternately, if you embed it as mimeType="application/octet-stream", you can get the bytes of the SWF and use it in your existing Loader's .loadBytes() method:
package
{
import flash.display.Sprite;
import flash.utils.ByteArray;
public class Game extends Sprite
{
[Embed(source="MyMovie.swf", mimeType="application/octet-stream")]
private var movieBytes:Class;
private var myMovie:DisplayObject;
public function Game():void
{
// Treat this loader the same as your current loader,
// but don't call .load(url), call loadbytes():
var l:Loader = new Loader();
l.loadBytes(new movieBytes() as ByteArray);
}
}
}
I have a game that I've made that runs in two files, one the preloader and one the swf. When I use these two files together they work fine, however some flash portals require only one SWF, so I'm trying to find the shortest-path way to convert it to use only one.
The game swf uses a Document Class that includes the package, class constructor, and tons of functions, and runs fine. I have lots of movieclips on the stage that I refer to in my code. The code is something like this (abridged):
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
...
public class Game extends MovieClip {
var EnemyArray:Array;
var HeroArray:Array;
...
public function Game() { // class constructor
EnemyArray = new Array();
addEventListener(Event.ENTER_FRAME,onEnterFrame);
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
...
}
public function KeyPressed(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{
attack();
...
}
} // End of class
} // End of package
I also have another swf, my preloader, which has the following code in frame 1:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.ProgressEvent;
import flash.events.Event;
var myLoader:Loader = new Loader();
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoading);
var myURL:URLRequest = new URLRequest("Game.swf");
myLoader.load(myURL);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
function onLoading(event:ProgressEvent):void {
var loaded:Number = event.bytesLoaded / event.bytesTotal;
percent_txt.text = "Loading: " + (loaded*100).toFixed(0) + "%";
}
function onComplete(event:Event):void {
myLoader.contentLoaderInfo.removeEventListener(ProgressEvent.PROGRESS, onLoading);
myLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
this.addChild(myLoader);
}
I've been through the gamut of solutions online and none of them seem to work with the way that my code is set up, such as: How to create Preloader in AS3
I tried his option 1 by taking all the code in my Document Class other than the package and constructor and pasted it into frame 2. However it returns "Error #1009: Cannot access a property or method of a null object reference." for any references to items on the stage, such as:
mainRestartButton.addEventListener(MouseEvent.CLICK, RestartLevel);
As he mentions, "This works well if you organized your project in a way that the most of the assets (images, etc) are in the Flash IDE Library and are not loaded on the first frame (you can check that in each library item's properties)." My project is NOT organized in such a way.
What is the easiest way for me to reorganize my FLA/code so they can have a preloader in a single swf?
Thanks so much for your time and help!
I use FlashDevelop (no FLAs) so I'm not sure if it'll work for you but I use the solution described here.
I am loading one swf into another using the Loader class, but when the child swf finishes loading and is added to the display list, its Document Class is not instantiated. I have a few trace statements that should execute when the object is created, but nothing is happening when loaded into the parent SWF. When I compile the child SWF on its own, the Document Class runs as expected.
So I'm wondering... how do I associate a child SWF's Document Class with Loader.content?
Code updated with a solution from Kishi below.
public class Preloader extends Sprite {
import flash.net.*;
import flash.display.*;
import flash.events.*;
// code in parent SWF's Document Class (Preloader.as)
private var swfLoader:Loader;
public var mainMovie:MovieClip;
public function Preloader(){
swfLoader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderDone);
swfLoader.load(new URLRequest("mainmovie.swf"));
}
private function loaderDone(e:Event):void {
// Cast Loader.content to MovieClip
mainMovie = MovieClip(swfLoader.content);
mainMovie.addEventListener(Event.ADDED_TO_STAGE, mainMovieAddedListener);
addChildAt(mainMovie, 0);
}
private function mainMovieAddedListener(e:Event):void {
// init() not necessary
}
}
// MainMovie.as runs after casting swfLoader.content to MovieClip
public class MainMovie extends Sprite {
public function MainMovie(){
trace('MainMovie WHATTUP');
}
public function init():void {
trace('init');
}
}
Cheers!
The problem is how you are trying to access the swf instance.
First, the document class instance is referred by Loader's content property. You'd reference it like this:
var swf:DisplayObject = swfLoader.content;
But, even then you'd have to cast the DisplayObject either to it's real class (MainMovie, in this case) or a dynamic class (such as MovieClip), as you are trying to use a custom property, that's not part of DisplayObject itself. Therefore, you could call MainMovie.init() like so:
var swf:MovieClip = MovieClip(swfLoader.content);
swf.init();
Hope that helps.
You're adding the mainMovie to stage, then you add a listener which inites it. The event is not fired as it's added after the movie is on stage.
What Cristi said is probably right, for the reason your init method isn't firing, but the more strange issue is that your loaded child swf's Constructor, MainMovie, should be firing as soon as that object is created.
Whenever I've done things like this, I've never created a new Sprite object from the contents of the loaded swf. Seems what you're doing there is using the swf like it's BitmapData, to create the Sprite mainMovie.
Try this: remove your statement that says swfLoader = null, and instead say addChild(swfLoader);. If you still want that event listener checking for it being added to the stage, put it before you do the addChild(swfLoader);, and of course put it on the swfLoader not the mainMovie object:
swfLoader.addEventListener(Event.ADDED_TO_STAGE, mainMovieAddedListener);
addChild(swfLoader);
See what you get then. [also can you paste the exact error you got from trying to access init?]
debu