Increment variable from within MovieClip

Posted on 2008-11-12
Last Modified: 2013-11-11
I would like to check some syntax and procedure to get on the right path.

In simple terms I have a scene (Level A) with a movieclip (Level B) and inside that MC six more MCs (Level C). Also on level B above the MC is another MC with buttons that look exactly like the 6 movieclips. These buttons trigger the 6 MCs play each and then displays a greyed out button for ones that have completed. I have a hidden NEXT button that should show once all the clips have been played and I want to the 6 MCs to increment a counter to display the NEXT button.

See the sample in action at the URL given.

My thinking is that I have a specific syntax problem or it's in the pathing - the buttons being 2 levels deeper than the Var and the button I want to unhide when the counter reaches 6.

Here is the codel:

Level A:

On the last frame where I add the 6 MCs inside their level B parent:


var Counter:Number = 0; 

if(Counter == 6){ 

 _root.ButtonContinue._visible = true;    


_root.ButtonContinue._visible = false; 


Then on each of the buttons in Level C:

on (release) {;

	_root.FlyPastExp._visible=true // Unhide the MC and play it

	_root.FlyButtons._visible=false // Buttons are hidden while MCs play

	_global.Counter=Counter++ // increment the counter


Or their respective hide and play targets.

The 0 (Zero) in the sample page (see URL sample) is the variable Counter that I'm displaying to the stage to see if the counter increments but as you can see it does not. 

Open in new window

Question by:Gerano
    LVL 13

    Accepted Solution

    var and _global are two different types of variable.

    You should use either/or.

    _global.Counter  = 0;
    if(_global.Counter == 6){

    _global.Counter = Counter++

    var Counter:Number = 0;
    if(Counter == 6){

    _root.Counter = _root.Counter++ // assuming you initiated the Counter var on the _root timeline.

    LVL 39

    Expert Comment

    i also have a suspicion that when you refer to levelA, levelB etc, you're refering to Layers and not levels - are you refering to the layers on the timeline, or have you actually loaded items into different levels, they are not the same thing.


    Author Comment

    No, my reference to levels are the different parent child levels.

    I'm not using the correct terminology for this my apologies so let me state it this way which will hopefully make it more clear. I will also stay with my reference to A, B and C above so as not to confuse the ocde I posted.
    Level A = a frame on root (scene) timeline with var code
    Level A = 6 layers each with a movieclip called CardFly1 , CardFly2, etc.
    Level A = a movieclip called FlyButtons
    Leve B = 6 buttons inside FlyButtons that trigger 6 MCs on level A, hide FlyButtons MC and also have to increment the counter.
    Note that the last frame of each of the 6 MCs on level A sets FlyButton visible = true, hides the button that was clicked and shows the greyed out button for that clicked button.

    Wow, just writing that out made it a lot more clear in my mind. After days of this you lose track of everything you tried or did.

    I tried the code that crooksy88 posted but the button that should be hidden until all six buttons are clicked shows from the start and the variable dynamic text stays on 0
    LVL 13

    Expert Comment

    I've just spotted a typo on my part

    _global.Counter = Counter++

    should be:

    _global.Counter = _global.Counter++

    If you are using this _global method you need to check that all instances of your original Counter var are changed to _global.Counter

    Author Comment

    Nope, this is still not doing anything.

    Don't know if this is relevant but while these buttons are cllicked the scene remains static on the timeline, so there is no next frame where something can be checked as one would with a preloader.
    LVL 39

    Assisted Solution

    hey Crooks if i can jump in.
    gerano, i have no idea what your movieclip setup is in terms of your LevelA and LevelB, help us out, most of us are old and decrepit and geeks, so speak the correct geek to us.
    you're only going to confuse yourself and others more.
    in terms of which clip does what, i am clueless till I see it, but the counter thing, is as simple as the following.

    on your main timeline where you're declaring your variable

    var Counter:Number = 0;
    as crooks said, you haven't declared that as a global.
    you don't need a global in this case.

    in your button all you need to do is.

    on(release) {
    //all the other stuff

    and you'll see that increments by 1 each time you click it.

    (ps if you're going to be developing flash into the future, stop the habit now of putting code onto your objects)

    i.e. instead of selecting your button and putting onRelease code directly on it, give your button an instance name and put the code all in one central location (if not external files) then at least on the timeline

    buttonName.onRelease = function() {


    Author Closing Comment

    Thanks for the pointing me in the right direction, I made it all go on the timeline. Also, the tip about not assigning anything directly on the elements was a great help

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Do You Know the 4 Main Threat Actor Types?

    Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

    Suggested Solutions

    Title # Comments Views Activity
    Stock Videos 17 315
    Microsoft WSUS 4 79
    Adobe flash player memory exhaustion 6 73
    You Tube Videos not playing 9 97
    I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
    I have found that much of my time doing support ends up being a constant repetition of the same steps to different people.  Early on I stated using web pages with Frequently Asked Questions (FAQs) to alleviate most of the burden.  Sometimes this jus…
    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 set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now