Solved

Using a Variable Outside of a function

Posted on 2009-06-26
7
378 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
ID: 24726291
Hi greg7740,

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

Author Comment

by:greg7740
ID: 24726643
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
ID: 24726738
Hi greg7740,

That is the correct way to do it, you must be missing something out, can you post your code.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 4

Expert Comment

by:H01
ID: 24726771
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
ID: 24728629
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
ID: 24730036
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
ID: 24730314
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I know the transition can be hard. We got used to the ease of use ActionScript 2 had, but honestly, it became problematic later on, especially if designers were involved in the project and found it easy to add code as they saw fit. So, this artic…
The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
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…

776 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