as3 access variable from parent swf - actionscript-3

I'm trying to let the child read its parent's vars and vice versa.
The parent has no problems reading the child's vars, but for some reason the child gets only "undefined" as an answer...(instead of the "456")
Parent script
var mySwf
var masterVar=456
function startLoad() {
var myLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("test1.swf");
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
myLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
myLoader.load(mRequest);
}
function onCompleteHandler(loadEvent:Event) {
mySwf=loadEvent.currentTarget.content
addChild(mySwf);
trace(mySwf.slaveVar)//123
}
function onProgressHandler(mProgress:ProgressEvent) {
}
startLoad()
Child(test1.swf) script
var slaveVar=123
trace(Object(parent))//[object Loader]
trace(Object(parent.parent))//[object Object]
trace(Object(parent.parent).masterVar)//undefined
trace(Object(this.parent.parent).masterVar)//undefined
parent.parent.parent is null
and MovieClip(parent.parent) only spits out an error
I have no clue what's wrong...
am I missing something?

Try this:
In child swf:
var theParent:Object;
addEventListener(Event.ADDED_TO_STAGE, onAdded);
function onAdded(e:Event):void
{
theParent = this.parent as Object
trace(theParent.masterVar);
//will work after child swf has been added to the display list of the parent file.
}

Related

ActionScript 3.0 import swf

i've little bit trouble with imported swf. Problem is that imported swf has movie clip which exported as movie clip class. Code is here:
Imported swf:
class myBooks(){
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
and HOLDER flash file:
var txt:String = "Project/book2.swf";
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest(txt);
myLoader.load(url);
addChild(myLoader);
when i try to run , it shows me error like this:
TypeError: Error #1009 cannot access a property or method of a null object reference at actions::myBooks()
Try the following
class myBooks()
{
public function myBooks():void
{
if(stage)
{
init(null);
}
else
{
addEventListener( Event.ADDED_TO_STAGE,init)
}
}
private function init( e:Event = null )
{
var myBg:MovieClip = new backGround();
stage.addChild(myObjects);
}
}
Hope that help

ActionScript3 remove child error

I recently have been converting an as2 fla to as3 (new to AS3) and have the entire thing working on export, but I am getting an error when I try to remove previously loaded swf's before a new swf is loaded
ArgumentError: Error #2025: The supplied DisplayObject
must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at MethodInfo-11()
I know the error relates to my removeChild code here:
`stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}`
However, I cannot seem to find a suitable rewrite for this code that will work and not have an error. This code IS working, so I'm not sure if it would be worth my time to fix this error, or just leave it. I've already messed with it for a couple days, so at this point it's just frustrating me that I cannot fix it.
The stage mouse click listener is useful in this case because I have a back button not shown in this code that clears the loaded swf's before moving to another scene.
Does anyone see a simple solution for this, or do you think it is unnecessary to pursue since the code does what I require?
ENTIRE CODE:
function launchSWF(vBox, vFile):void {
var swfLoader:Loader = new Loader();
var swfURL:URLRequest = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
}
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
//NEW ERRORS BUT WORKING
stage.addEventListener(MouseEvent.CLICK, removeSWF);
function removeSWF (e:MouseEvent):void
{
if(vBox.numChildren !=0){
// swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
}
var container:MovieClip = new MovieClip();
var currentSWF:MovieClip = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
function fall_bClick(e:MouseEvent):void {
var swfFile:String = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
function face_bClick(e:MouseEvent):void {
var swfFile:String = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
function rott_bClick(e:MouseEvent):void {
var swfFile:String = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
//MORE SWFS...
Any advice anyone has is appreciated
First of all function launchSWF(vBox, vFile):void { isn't closed. You've also got function inside functions which is easy enough for you to solve if you click the lines the curly brackets start and end on to track them.
I can't see anything wrong with the code you said has an error but I'm guessing this isn't all the code. If you using Flash Professisonal you can use permit debugging to show the line the error is on.
EDIT: Please note this hasn't been tested as I'm on my mobile writing out code. That being said this should now work:
var container:MovieClip;
var currentSWF:MovieClip;
var swfFile:String;
var swfLoader:Loader;
var preLoader:Loader;
var swfURL:URLRequest;
init();
function init():void {
preLoader = new Loader();
preLoader.x = 450;
preLoader.y = 280;
vBox.addChild(preLoader);
container = new MovieClip();
currentSWF = new MovieClip();
fall_b.addEventListener(MouseEvent.CLICK, fall_bClick);
face_b.addEventListener(MouseEvent.CLICK, face_bClick);
rott_b.addEventListener(MouseEvent.CLICK, rott_bClick);
stage.addEventListener(MouseEvent.CLICK, removeSWF);
}
function launchSWF(vBox, vFile):void {
swfLoader = new Loader();
swfURL = new URLRequest(vFile);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
swfLoader.load(swfURL);
}
function loadProdComplete(e:Event):void {
trace("swf file loaded");
vBox.removeChild(preLoader);
vBox.addChild(swfLoader);
currentSWF = MovieClip(swfLoader.content);
currentSWF.gotoAndPlay(1);
currentSWF.addEventListener(Event.ENTER_FRAME , checkLastFrame);
swfLoader.x = 165;
swfLoader.y = 15;
}
function checkLastFrame(e:Event):void {
if (currentSWF.currentFrame == currentSWF.totalFrames) {
currentSWF.stop();
// trace("DONE");
}
}
function onProgressHandler(event:ProgressEvent) {
var dataAmountLoaded:Number = (event.bytesLoaded / event.bytesTotal * 100);
//preLoader.bar.scaleX = dataAmountLoaded/100;
preLoader.lpc.text = int(dataAmountLoaded)+"%";
//trace(preLoader.bar.scaleX );
}
function removeSWF (e:MouseEvent):void {
if(vBox.numChildren !=0){
//swfLoader.unloadAndStop();
vBox.removeChild(swfLoader);// empty the movieClip memory
}
}
function fall_bClick(e:MouseEvent):void {
swfFile = 'load/fall.swf';
launchSWF(container, swfFile);
addChild(container);
}
function face_bClick(e:MouseEvent):void {
swfFile = 'load/face.swf';
launchSWF(container, swfFile);
addChild(container);
}
function rott_bClick(e:MouseEvent):void {
swfFile = 'load/rottgut.swf';
launchSWF(container, swfFile);
addChild(container);
}
I have this rewritten. I could not get the vBox errors cleared in the original code, and I was getting many other errors with what was posted. The vBox code was seen on a tutorial. I think it was supposed to reference the loader for the preloader and the swf, and vFile was for the actual .swf. The following code preloads multiple swfs and clears them with no errors. I appreciate your help AntBirch. I'm beginning to understand loaders in as3 a little more now.
//LOAD FIRST PIECE ON OPEN (required to removeChild later)
var swfLoader:Loader = new Loader();
var defaultSWF:URLRequest = new URLRequest("load/fall.swf");
swfLoader.load(defaultSWF);
swfLoader.x = 165;
swfLoader.y = 15;
addChild(swfLoader);
//PRELOADER
var preLoader:loader = new loader();
preLoader.x = 450;
preLoader.y = 280;
function loadProdComplete(e:Event):void {
trace("swf file loaded");
removeChild(preLoader);
addChild(swfLoader);
}
function onProgressHandler(event:ProgressEvent){
var dataAmountLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
preLoader.lpc.text= int(dataAmountLoaded)+"%";
}
//BUTTONS
function btnClick(event:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
swfLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadProdComplete);
addChild(preLoader);
var newSWFRequest:URLRequest = new URLRequest("load/" + event.target.name + ".swf");
swfLoader.load(newSWFRequest);
swfLoader.x = 165;
swfLoader.y = 15;;
addChild(swfLoader);
}
// BUTTON LISTENERS
fall.addEventListener(MouseEvent.CLICK, btnClick);
face.addEventListener(MouseEvent.CLICK, btnClick);
rott.addEventListener(MouseEvent.CLICK, btnClick);
angel.addEventListener(MouseEvent.CLICK, btnClick);
ratts.addEventListener(MouseEvent.CLICK, btnClick);
metal.addEventListener(MouseEvent.CLICK, btnClick);
//etc...
//BACK BUTTON
BB3.addEventListener(MouseEvent.CLICK, BB3Click);
function BB3Click(e:MouseEvent):void {
swfLoader.unloadAndStop();
removeChild(swfLoader);
this.gotoAndPlay(1 ,"Scene 2")
}

How to load external swf and use the child class

I would like to load external child swf into the parent swf (under same directory).
I did try, the child loaded but the class in it didn't work.
1) How can i load the class of child swf?
2) How can i unload the class of child swf?(becoz there could be many ext. swf with different class)
thx
main.fla
function startLoad(){
var mLoader:Loader = new Loader();
var mRequest:URLRequest = new URLRequest("game1.swf");
var mLoaderContext:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain);
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load(mRequest, mLoaderContext);
}
function onCompleteHandler(loadEvent:Event){
var keyManager:Class = ApplicationDomain.currentDomain.getDefinition("net.keithhair.KeyManager") as Class;
addChild(loadEvent.currentTarget.content);
}
game1.fla
import net.keithhair.KeyManager;
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething);
function doSomething():void {
//do something
}
Result:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at net.keithhair::KeyManager/removeListeners()
at net.keithhair::KeyManager/addListeners()
at net.keithhair::KeyManager()
at game1_fla::MainTimeline/frame1()
function Constructor(){
if (stage){
onAddedToStage();
} else {
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}
}
function onAddedToStage(evt:Event=null):void {
var keyManager:KeyManager;
trace("here!")
keyManager=new KeyManager(stage);
keyManager.addKey(["a"], doSomething
}
Constructor();
The above code solve all my problem.
thank you very much destinier
& senocular

access of content of loaded swf

I want to access content of a loaded SWF file. I used following code,
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
trace(target_clip.getChildAt(0));
trace(target_clip.getChildAt(1));
}
Please help me.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener( Event.COMPLETE, handleComplete );
var request:URLRequest = new URLRequest("artwork3.swf.swf");
loader.load(request);
protected function handleComplete(event:Event):void
{
DisplayObject loadedSwf = target_clip.addChild(event.currentTarget.content as DisplayObject) as DisplayObject;
//you can access variables from loaded swf
trace(loadedSwf.name);
}
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
var myLoadedSwf:MovieClip = loader.content as MovieClip;
trace(myLoadedSwf.getChildAt(0));
trace(myLoadedSwf.getChildAt(1));
//trace(target_clip.getChildAt(0));
//trace(target_clip.getChildAt(1));
}
Try MovieClip(loader.content) instead of target_clip like so,
function _browse(e:MouseEvent):void
{
loader.load(new URLRequest("artwork3.swf.swf"));
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,loading);
target_clip.addChild(loader);
}
function loading(event:Event)
{
trace(MovieClip(loader.content).getChildAt(0));
trace(MovieClip(loader.content).getChildAt(1));
}
Best luck.

AS3 Image Loader - It loads itself (the swf)

My LoaderInfo will return the swf itself rather than the image given via the FileReference and Loader. I had problems debugging it, as LoaderInfo.content returns [Object Main] (My document class). After investigation, I discovered LoaderInfo.content is a swf file, according to contentType.
The problem is, the file reference for the image is correct (It is an image, and it is not the swf).
My code:
private function onAction(e:MouseEvent){
if(e.currentTarget.name == 0){
myFileReference = new FileReference();
myFileReference.browse(getTypes());
myFileReference.addEventListener(Event.SELECT, loadedImage);
myFileReference.addEventListener(Event.COMPLETE, loadImage15);
}
}
private function loadedImage(e:Event){
var imgHolder:ImageHolder = Main.imageHolder;
while(imgHolder.numChildren > 0){
imgHolder.removeChild(imgHolder.getChildAt(0));
}
myFileReference.load();
}
private function loadImage15(e:Event){
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadImg2);
loader.loadBytes(myFileReference.data);
trace(myFileReference.type); // .JPG
}
private function loadImg2(e:Event){
var lInfo:LoaderInfo = (e.target as LoaderInfo); //e.target is indeed LoaderInfo
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
trace(loaderInfo.contentType); //application/x-shockwave-flash
var newSprite:MovieClip = loaderInfo.content as MovieClip;
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
}
private function getTypes():Array {
return [new FileFilter("Images","*.jpg;*.jpeg;*.gif;*.png")];
}
EDIT
I originally had a very complicated answer - which was wrong...
You simply have an error in your program:
// here you reference the Loader's contentLoaderInfo
var lInfo:LoaderInfo = (e.target as LoaderInfo);
lInfo.removeEventListener(Event.COMPLETE, loadImg2);
// but from here on out, you reference your parent class' "loaderInfo" property!
trace(loaderInfo.contentType);
var newSprite:MovieClip = loaderInfo.content as MovieClip; // <- this is your Main class!
Main.imageHolde.addChild(newSprite); //Error as you can't addChild Main to Main
Change loaderInfo to lInfo, and you should be fine.
I ask about this issue before :
loading image by Loader.loadBytes(byteArray)
Noone knows :]
If You loadBytes of image with Loader , You will recive :
Loader.content is MovieClip
Loader.content.getChildAt(0) is Bitmap