Solved

Using a Variable Outside of a function

Posted on 2009-06-26
7
375 Views
Last Modified: 2013-11-11
I thought I got an answer to this earlier but I'm still having problems.  I have a urlrequest that loads a variable inside of a function.  I need to get the variable outside of the function so that I can use it.

I able to get the variables in the loaded swf, but If I trace them outside of the function they are null.  How do I use them outside of the function.

thanks.
var request:URLRequest = new URLRequest("myURL");

var swfLoader:Loader = new Loader();

swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);

var newID = 0;

function swfComplete(e:Event):void {

       MovieClip(swfLoader.content).innerVariable = newID; //(<-- Pass into Loaded swf)

       newID =  MovieClip(swfLoader.content).bus_ID;//(Pass Back to Main swf

}

swfLoader.load(request);

Open in new window

0
Comment
Question by:greg7740
  • 4
  • 2
7 Comments
 
LVL 4

Expert Comment

by:H01
Comment Utility
Hi greg7740,

just trace(MovieClip(swfLoader.content).bus_ID)  will get the variable inside the child.
0
 

Author Comment

by:greg7740
Comment Utility
HI H01
It traces fine inside the function and I get the variable.  But when I trace outside of the function, I get Error #1009: Cannot access a property or method of a null object reference.

Unless I'm not tracing it correctly.
thanks.
0
 
LVL 4

Expert Comment

by:H01
Comment Utility
Hi greg7740,

That is the correct way to do it, you must be missing something out, can you post your code.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 4

Expert Comment

by:H01
Comment Utility
Hi greg7740

I think is the variable name in the loaded swf, may be you refer to an undefine varible.

trace(MovieClip(swfLoader.content).innerVariable )  (<------------- should be ( innerVariable ) not bus_ID)

0
 

Author Comment

by:greg7740
Comment Utility
Hi Ho1

This error I get: Error #1009: Cannot access a property or method of a null object reference.

this is my code:

stop();
var request:URLRequest = new URLRequest("http://www.myURL.com/busLogin/loaderTest.swf");
var swfLoader:Loader = new Loader();
swfLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfComplete);
var newID = 5;
function swfComplete(e:Event):void {
       MovieClip(swfLoader.content).innerVariable = newID;   //(<-- Pass into Loaded swf)
       newID =  MovieClip(swfLoader.content).bus_ID;  //(Pass Back to Main swf
          
         }

         swfLoader.load(request);

trace(MovieClip(swfLoader.content).innerVariable )

Can you see anything I'm doing wrong?  Thanks.
0
 
LVL 4

Accepted Solution

by:
H01 earned 500 total points
Comment Utility
Hi greg7740,

The problem is you are tracing the innnerVariable before the load function completely load the inner swf, thats why AS3 cant find the varible so giving you the error.

You can create a button and wait until the inner swf have successfully loaded then only u click on the button to trace the innerVariable.

Hope this helps you out.
0
 
LVL 39

Expert Comment

by:blue-genie
Comment Utility
if you place your trace inside the swfComplete funtion - if your variables are correct that should be fine.

you shoul as h01 is saying only be trying to access the content from swfLoader when it's finished loading.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
Recently, I was asked to recommend a tracking system to be implemented on a clients website. As the entire site was built on flash, my first thought was to suggest custom built tracking system. However, our company at that point of time didn't h…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now