Accessing external swf variables or functions
-migrated-
Accessing external swf variables or functions Posted on: 01/13/2012 9:39pm
Quote Post
I am trying to load a external swf but its not happening

Below is the code I am using to load & access external swf

Code: [Select]
import flash.events.KeyboardEvent;

var my_Loader:Loader = new Loader();
var mcAnk:MovieClip;
var my_url:URLRequest=new URLRequest("office.swf");
my_Loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoad);
my_Loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, errorHandler);
my_Loader.load(my_url);

mybtn.visible = false;

function fileLoad(e:Event):void
{
mcAnk = MovieClip(my_Loader.contentLoaderInfo.content);
my_Loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, fileLoad);
mcAnk.x = 0;
mcAnk.y = 0;
addChild(mcAnk);
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownListener);
}

function keyDownListener(e:KeyboardEvent)
{
    if (e.keyCode==49)
    {
        mcAnk.gameVars.changeLevel();;
    }
}

function errorHandler(errorEvent:Event):void {
trace("file missing");
}
after testing the swf I am getting the following out put errors

Code: [Select]
ReferenceError: Error #1065: Variable soundFX_7 is not defined.
ReferenceError: Error #1065: Variable mainSound is not defined.
TypeError: Error #2023: Class Saddlebag$ must inherit from Sprite to link to the root.

how can this be resolved.
Ask yourself these questions:

  • Are both SWFs AS3?
  • Is the code in the document class of the loaded swf run before it is added to the stage?
  • Is gameVars a static variable in the document class of the loaded swf?

I assume the code you tested is exactly the same as the one you've posted. If that's not the case, and the url for the swf you're trying to load is in another location, that might cause the errors.

Providing the swf would help me pinpoint the cause of the errors.

Also, remove the second semicolon in "mcAnk.gameVars.changeLevel();;".
Both are as3 files and running from the same directory, below is the link for .fla & .swf

pls help me out

Code: [Select]
http://www.mediafire.com/?nhwko07v2x5vn3k
Problem: The class TouchEvent doesn't exist in Flash Player 9 and earlier.
Solution: Set the Flash Player version in as3.fla to 10+.
thanks, now game is loading but when pressed the key getting error as

Code: [Select]
TypeError: Error #1006: ClickedNext is not a function. at as3_fla::MainTimeline/keyDownListener()
Quote from: "kavya"
thanks, now game is loading but when pressed the key getting error as

Code: [Select]
TypeError: Error #1006: ClickedNext is not a function. at as3_fla::MainTimeline/keyDownListener()
That's because the function is either not accessible (private/protected) or doesn't exist in the class.
The function ClickedNext exist in a class named Game and its public

Code: [Select]
public function ClickedNext()
{


}

How to access if its in another class rather than maintimeline
Quote from: "kavya"
The function ClickedNext exist in a class named Game and its public

Code: [Select]
public function ClickedNext()
{


}

How to access if its in another class rather than maintimeline
Try Google. I may look into it later, as I would also like to know if it's possible / how to do it.

Edit: this looks interesting.

Edit2:
Download the file below (broken image removed)

Pressing "1" resets the timer.
Thanks man its working,

can you tell me how it done apart from the attachments in detail, it would be helpful for me further

How can I use the same for future .swf loading & what are the points I need to take into consideration while making key event
Quote from: "kavya"
Thanks man its working,

can you tell me how it done apart from the attachments in detail, it would be helpful for me further

How can I use the same for future .swf loading & what are the points I need to take into consideration while making key event
When you use addChild to add the loader its class is MainTimeLine, which, in this case, didn't contain the code we needed. To set the timer we need to call a function from the instance of the Timer class in the Game class. Using ApplicationDomain you can create a new instance of the Game class, however that would be futile, as it wouldn't affect the one in the game. Therefore, we need to find the document class (i.e. Main) through the MainTimeLine. We'll use the scanForChildren function in the attachment for this purpose by changing it from:

Code: [Select]
function scanForChildren(obj:DisplayObjectContainer, depth:int):void {
if(depth > 5) return;

var prefix:String = "";
var j:int = 0;
while(j++ < depth){
prefix += "   ";
}

for(var i = 0; i < obj.numChildren; i++){
var child:DisplayObject = obj.getChildAt(i);
if(!child.name.indexOf("instance") == 0) trace(prefix, "[" + i + "]", child.name, "[" + getQualifiedClassName(child) + "]");

if(child is DisplayObjectContainer){
scanForChildren(DisplayObjectContainer(child), depth + 1);
}
}
}

to:

Code: [Select]
function scanForChildren(obj:DisplayObjectContainer, depth:int):void {
if(depth > 0) return;

var prefix:String = "";
var j:int = 0;
while(j++ < depth){
prefix += "   ";
}

for(var i = 0; i < obj.numChildren; i++){
var child:DisplayObject = obj.getChildAt(i);
trace(prefix, "[" + i + "]", child.name, "[" + getQualifiedClassName(child) + "]");

if(child is DisplayObjectContainer){
scanForChildren(DisplayObjectContainer(child), depth + 1);
}
}
}

What changed:
depth > 5 => depth > 0
if(!child.name.indexOf("instance") == 0)

And replace game with mcAnk in the following line in the keyListener function:
Code: [Select]
scanForChildren(game, 0);
It'll find all the DisplayObjects in MainTimeLine - in this case only the instance of the Main class. To access it we need to call:
Code: [Select]
DisplayObject(mcAnk.getChildAt(0))For other games you have to use the number given by the scanForChildren function.

From here we access the variable game, which contains the variable timer we were looking for. The function we want to call on timer is Init and the parameter we need to give it is time_total (or however long we want for the level, which doesn't matter as we can reset it as many times as we like). Finally we get:
Code: [Select]
DisplayObject(mcAnk.getChild(0)).game.timer.Init(DisplayObject(mcAnk.getChild(0)).game.timer.time_total);Which can also be written as:
Code: [Select]
var game:DisplayObject = DisplayObject(mcAnk.getChild(0));

game.timer.Init(game.timer.time_total);

Note: the reason we can call these functions and variables is because they're public. We can't call functions such as GameCompleted in the class Game.
(broken image removed) I have been looking for this from few days & finally I got here, thanks once again.
Hi KongregateHack
I tried to use the same code for keyevent on a external .swf & its throwing an error
Code: [Select]
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Sprite@2de0f51 to flash.display.MovieClip. at as3_fla::MainTimeline/keyDownListener()[as3_fla.MainTimeline::frame1:24]

Code: [Select]
http://www.mediafire.com/?l1qcdcjo2prw5ne
and also can you tell me whether the word used "game" in the loader is a variable define in the loader or its a class name exists in extenal .swf (Screen Snap Attached)
(broken image removed)
Quote from: "b_nany68"
Hi KongregateHack
I tried to use the same code for keyevent on a external .swf & its throwing an error
Code: [Select]
TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Sprite@2de0f51 to flash.display.MovieClip. at as3_fla::MainTimeline/keyDownListener()[as3_fla.MainTimeline::frame1:24]
Search and replace "MovieClip" with "Sprite". It's used for typecasting, which may not be necessary, so you can try removing them and see if it still throws an error.

Quote from: "b_nany68"
and also can you tell me whether the word used "game" in the loader is a variable define in the loader or its a class name exists in extenal .swf (Screen Snap Attached)
(broken image removed)
  • Yes.
  • mcAnk = the loaded movie.
    mcAnk.getChildAt(0) = the first child in mcAnk, which happens to be the document class "Main".
    mcAnk.getChildAt(0).game = variable in the document class.
  • Both. The variable we defined above is a reference to the one in the loaded SWFs document class.
Hi KongregateHack
when i replace MovieClip with Sprite the game is not loading and showing error
Code: [Select]
Scene 1, Layer 'Layer 1', Frame 1, Line 22 1119: Access of possibly undefined property LevelData through a reference with static type flash.display:Sprite.

Code: [Select]
Scene 1, Layer 'Layer 1', Frame 1, Line 28 1061: Call to a possibly undefined method unlockPage through a reference with static type flash.display:Sprite.
Pls find attachment
Code: [Select]
http://www.mediafire.com/?1p8nv6di8oi9836
bump