PDA

View Full Version : Accessing stage from within an embedded SWF?


pinkshiro
09-02-2008, 10:31 PM
Morning,

How can I access the stage from within an embedded SWF? I have a scrollbar which checks for a MOUSE_UP anywhere on the stage. But the scrollbar is sitting inside of my content.swf, which is being loaded by statesman.swf.

Is there any way for the child SWF access the stage property of the parent SWF?

Anil_kumar
09-03-2008, 06:39 AM
code for your parent swf
var swfLoader:Loader = new Loader();
stage.frameRate = 30;

button.addEventListener(MouseEvent.CLICK,CLICKHand er);
button.buttonMode = true;

function CLICKHander(e:MouseEvent):void {
swfLoader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event .COMPLETE, gotSwf);
swfLoader.load( new URLRequest("swf/swfFile.swf"));
}


function gotSwf(e:Event):void {
uILoader.source=swfLoader.content;
swfLoader.contentLoaderInfo.removeEventListener(Ev ent.COMPLETE, gotSwf);
}

code for your embedded swf

var MyRoot = stage.root;
var target = MyRoot.getChildAt(0);
target.MyObject.x+=5;


Anil
Flash Workshop (http://flash-workshop.blogspot.com/)
anilkumarnd@gmail.com

pinkshiro
09-03-2008, 09:45 AM
Thanks for your help there :) Tried it out, was unsuccessful though.

I did, however, come across this little beauty:

this.addEventListener(Event.ADDED_TO_STAGE,onAdded ToStage, false, 0 , true);public function onAddedToStage(e:Event):void{ trace(this.stage);}

andyroerig
09-13-2008, 02:13 PM
I am having trouble targeting the stage from my embedded swf.. im new to actionscript 3 so I hope that someone can give me a few pointers..

i have a preloader (index.swf) that loads rollin84z.swf in a uiloader.. when either the slabfest button or slab ridaz button is clicked it embeds slabfest.swf and slabridaz.swf respectively into rollin84z.swf.. within both slabfest.swf and slabridaz.swf i have a scrollbar that needs to access the stage for the thumbUp and thumbDown functions/event listeners..

this is the scrollbar as on the main timeline of slabfest.swf/slabridaz.swf with 3 instances of stage that need to be modified..
import caurina.transitions.*;

var yOffset:Number;
var yMin:Number = 0;
var yMax:Number = sb.track.height - sb.thumb.height;

sb.thumb.buttonMode = true;

sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);

function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
yOffset = mouseY - sb.thumb.y;
}

function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
}

function thumbMove(e:MouseEvent):void
{
sb.thumb.y = mouseY - yOffset;
if(sb.thumb.y <= yMin)
sb.thumb.y = yMin;
if(sb.thumb.y >= yMax)
sb.thumb.y = yMax;
var sp:Number = sb.thumb.y / yMax;
Tweener.addTween(content, {y:(-sp*(content.height-masker.height)),time:1});
e.updateAfterEvent();
}

Thank you for any help you can provide!!

Mazoonist
09-13-2008, 02:53 PM
You just need to listen for the ADDED_TO_STAGE event. Here's your code for slabfest, with an ADDED_TO_STAGE event listener. Notice that even MOUSE_DOWN on the scroll bar can't happen until you have access to the stage, preventing any possible errors:
import caurina.transitions.*;

var yOffset:Number;
var yMin:Number = 0;
var yMax:Number = sb.track.height - sb.thumb.height;

sb.thumb.buttonMode = true;

this.addEventListener(Event.ADDED_TO_STAGE, addedHandler);

function addedHandler(event:Event):void
{
sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
}

function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
yOffset = mouseY - sb.thumb.y;
}

function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
}

function thumbMove(e:MouseEvent):void
{
sb.thumb.y = mouseY - yOffset;
if(sb.thumb.y <= yMin)
sb.thumb.y = yMin;
if(sb.thumb.y >= yMax)
sb.thumb.y = yMax;
var sp:Number = sb.thumb.y / yMax;
Tweener.addTween(content, {y:(-sp*(content.height-masker.height)),time:1});
e.updateAfterEvent();
}
Now you just need to rewrite the code in a similar manner for slabridaz. Don't forget to compile (test movie) on the two modules, to generate a new SWF, before you run the main movie.

Mazoonist
09-13-2008, 03:35 PM
In further testing, I discovered another glitch. If you never remove the listener for the thumbUp from the stage, it stays active, and causes "null object reference" errors on your navigation bar. So, change the "thumbUp" function in both modules to this (I added a line that removes the thumbUp listener):
function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, thumbUp);
}
Once again, after you change both modules, run them to generate new SWF's, then try running the main file. You should be error free now.

andyroerig
09-13-2008, 10:28 PM
first off, thank you!

secondly, it works perfectly.. i cant thank you enough. action script 3 has a learning curve but the more i learn about it, the more i am loving it!

andyroerig
09-14-2008, 12:51 PM
Can I bounce one more question off of ya?

So when the stage is added, the event listener for MOUSE_UP is added.. but when it calls the thumbUp function it will remove that event listener.. the swf is never added again to the stage so the event listener is never re-added - the user is only able to mouse up once.. so as the mouse moves on the y axis - the thumb will follow.. it would appear that the event listener for mouse up should be (re)added.

any advice?

Mazoonist
09-14-2008, 03:14 PM
Yep, sorry. My mistake, I didn't test it quite thoroughly enough.

1. When the stage is added, the only listener that should be added is the one for MOUSE_DOWN.

2. When you get a MOUSE_DOWN event, inside the handler function, you want to add the two listeners, MOUSE_MOVE and MOUSE_UP.

3. Finally, when you get the MOUSE_UP event, inside its handler function, you want to remove the listeners for both MOUSE_MOVE and MOUSE_UP.

The setup was almost right. The setting of the MOUSE_UP listener should be moved from the ADDED_TO_STAGE handler to the MOUSE_DOWN handler. Here's the corrected code:
function addedHandler(event:Event):void
{
sb.thumb.addEventListener(MouseEvent.MOUSE_DOWN, thumbDown);
}

function thumbDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
stage.addEventListener(MouseEvent.MOUSE_UP, thumbUp);
yOffset = mouseY - sb.thumb.y;
}

function thumbUp(e:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, thumbMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, thumbUp);
}

andyroerig
09-15-2008, 01:52 AM
i really appreciate all your help Mazoonist.. now it looks like its working perfectly. That did the trick - :D