mrGott
mrGott

Reputation: 1076

Call variable inside a function AS3.0

I'm trying to pass a variable through a function, but I'm getting it's value 0

Here's my code:

thumbLoader.addEventListener(MouseEvent.CLICK, goToCategory);

function goToCategory(e:MouseEvent) {
    trace(c);
    gotoAndStop(2);
    doSMTH(0);
}

this trace gives me value of 0.

Normally I would do goToCategory(c) and inside that category I would get it's value, but in this case I'm calling this function with an event, how can that be done?

var c is declared globally, so I'm using it above this code in different place...

is there smth like global $c like in PHP.. or there's some other way to do it?

Thanks in advance!!!

Upvotes: 0

Views: 574

Answers (2)

catholicon
catholicon

Reputation: 1165

I assume variable c is available where you are adding mouse click listener. In that case, this should do what you want.

thumbLoader.addEventListener(MouseEvent.CLICK, function(e:MouseEvent)
                                               {
                                                   goToCategory(c);
                                               }
);

function goToCategory(c:*)
{
    trace(c);
    gotoAndStop(2);
    doSMTH(0);
}

Upvotes: 2

laurent
laurent

Reputation: 90736

You could try to not use globals, but if you really don't know how, then one solution is to use a static class, and add your globals to it:

package {

    public class Globals {

        static public var c:String = "";

    }

}

Then access the global with:

trace(Global::c);

Just writing this code makes me shiver but that's one way to have globals.

Upvotes: 3

Related Questions