Ricky
Ricky

Reputation: 51

AS3 - using variable inside a function

I'm new in as3. I'm trying to use variable which i assign a value from a function. But the variable outside the function always shown as a null value. I tried using the global variable way, but seems not working too.

loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
function loadComplete(event:Event)
{
   // Save Kongregate API reference
   kongregate = event.target.content;


   // Connect to the back-end
   kongregate.services.connect();


   // You can now access the API via:

   // kongregate.user
   // kongregate.scores
   // kongregate.stats
   // etc...
   username = kongregate.services.getUsername();
   trace("inside:"+username);


}

trace("outside:"+username);

How to use the variable a outside the function?
The output:

outside:
Alert: Kongregate API shadow services loaded due to local testing. API will load when the game is uploaded.
Kongregate API: IKongregateServices.connect()
inside:Guest

Upvotes: 1

Views: 101

Answers (1)

Marty
Marty

Reputation: 39466

That function is called asynchronously. This means that the data you're trying to access isn't available until that loadComplete function is called, which could be at any time in the application flow.

This article will explain asynchronous events in more detail.

Upvotes: 1

Related Questions