1

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

the code i posted is just an example. i did call the function
@Ricky Can you post exactly what you have? Because what you're telling me should work fine.
done. what should i do so that the trace outside the function show the same value as inside the function?
@Ricky Ah see, that is a completely different situation.
hmm i see.. any suggestion how to solve this? or should i post new question? thanks for the response by the way
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.