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!!!

1
  • @Taurayi I have written includes in Fla file and then just include .as files... that variable is declared in 1st .as file, and the function is in another .as file. Commented Sep 25, 2011 at 13:38

2 Answers 2

3

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.

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

3 Comments

I have written includes in Fla file and then just include .as files... that variable is declared in 1st .as file, and the function is in another .as file.
Then you should probably try to learn how to use classes. It's really simple and it would make your app a lot easier to maintain. You can import a class using import Global;
yeah I know and I'm using OOP most of all, but I was watching a tutorial and the guys is writing this tutorial with procedural, so I followed it. Now I want to tune that application a bit, and want to include some features, and I ran into this shitty problem. Thanks man, anyways!!!
2

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);
}

Comments

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.