A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: this has stumped many.... has it?

  1. #1
    Junior Member
    Join Date
    Apr 2000
    Posts
    8
    Hey folks! I have a bit of a stumper programming question:

    Here's what's going on:

    The following code tries to make Flash update a movie clip alpha property while in a do...while loop. Flash updates the alpha value inside it's memory, but the graphical result (ie the mc changes transparency) does not happen until after the do..while loop is totally concluded. Does anyone know why this is happening? Is it a bug inside Flash 5???

    Here is the code: (I have created a mc named "round" whis is just a simple circle, and the script is inside a simple button. Both the button and mc are on the main timeline level:

    on (rollOver) {
    trans = 1000; //this is the loop count and alpha value
    do {
    trans = trans-1;
    trace ("trans counter is "+trans);
    setProperty ("round", _alpha, 20);
    trace ("Alpha Property has been set to "+getProperty("round", _alpha)); //returns alpha value to output window
    } while (trans>1);
    }
    on (rollOut) {
    setProperty ("round", _alpha, 100); // this just resets the "round" mc
    }

    So there it is. What happens on rollover is that the alpha value changes (numerically) in the output window, but not until the loop has finished counting from 1000 down to 1 does "round" actually change in appearance.

    Try it yourself, maybe I have something uniquely wrong going on here (besides my obvious computer ineptitude).

    Thanks in advance for any help!


  2. #2
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    it's not you, it's a basic Flash principle: Flash will only redraw the movie at the beginning of each frame or, in some cases, after an 'updateAfterEvent()' call.

    The way to get your effect is to either create a loop across two frames, or to use the onClipEvent(enterFrame) event handler.

    hope this helps!

  3. #3
    Junior Member
    Join Date
    Apr 2000
    Posts
    8

    Example of code I would use?

    I think I understand what you are saying... but I am confused on one point: all of this action takes place in a one frame movie. If what you were syaing is true (and I'm sure it is, considering that flash is based on a linear timeline, etc.) wouldn't the alpha not change at all, because it is not at the beginning of a frame, but rather et the end of the only frame? (currently, it does change, just not while inside the for...while loop)

    Perhaps you could give me a coded example, albeit brief, of the remedy that you suggest. I think I am confused about the context of updateAfterEvent that would be useful in my case.

    Thanks for all your help!

  4. #4
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    Before I give an example, can you give me just a little clarification of the desired effect? Are you trying to fade the 'round' clip gradually, or are you trying to fade it out and then hold it for a specific time?

  5. #5
    Junior Member
    Join Date
    Apr 2000
    Posts
    8
    The desired effect is a gradual fade up from 0 to 100 during a rollover. I know I could do this with a simple tween, but I would really like to try and do it in code. It would save me a lot of time if I could construct a function that would gradually and controlled change the alpha state of a mc.

    By the way, thanks for your kind attention. I know this sort of problem (tyring to do something in a seemingly roundabout fashion) can be frustrating. I really appreciate all your help!!

  6. #6
    proud new daddy! LuxFX's Avatar
    Join Date
    May 2000
    Location
    Dunsinane
    Posts
    1,148
    no problem!

    Well, like I said, there are two ways I generally go about doing this. The older way uses a loop that goes between two frames, and the second uses the onClipEvent(enterFrame) handler. The second method is new to Flash5, is more elegant, and is quickly becoming a preferred method--but I think the idea behind the first one is still very important. I'll start with that.

    The first thing is that all of your actions are going to be taking place outside of your target moviecip (which I'll follow your example and call 'round'). In this circumstance, I usually create just a plain red box offstage call 'faderControl'. This box won't show up in the movie, but it's actions will still effect our target object. The 'faderControl' object has three frames:

    [frame 1 -- label: 'stopped']
    stop();
    fadeAmount = 0;
    function fade()
    {
    _root.round._alpha = fadeAmount;
    fadeAmount ++;
    if (fadeAmount => 100) {
    gotoAndStop('stopped');
    }
    }

    [frame 2 -- label: 'fadeIn']
    fade();

    [frame 3 -- no label]
    fade();
    gotoAndPlay('fadeIn');

    Not too hard, right? When your movie starts, this movieclip will be stopped at the first frame. You've set the initial alpha value with the original 'fadeAmount' statement, and you've created a function that sets the alpha of your target to the fadeAmount and then increments that value. It then checks to see if you've reached your final value, and if so will stop the movie again. The fading starts when you give a faderControls.gotoAndPlay("fadeIn"); call. This will play the second and third frames over and over again as your target object will fade in.


    The second way also requires a third-party 'faderControls' movie clip. This movie clip won't have any script inside it, just a normal one-frame no-frills movie clip. No stop() command, either. You should set a variable in your movie clip using the 'onClipEvent(load)' function to initialize the fade amount in the same way that took place in the first frame in the first example.
    The 'onClipEvent(load)' function occurs once, when the movie is initialized on the stage (or in this case, off the stage), so this is also a good way to initialize variables. To do this, and to add the actual loop commands, select your 'faderControls' object and edit the script--which will attach the script to your MC in the same way that you attach on(release) etc. scripts to a button. Use this script:

    onClipEvent(load)
    {
    fadeAmount = 0;
    }

    onClipEvent(enterFrame)
    {
    while(fadeAmount <= 100) {
    _root.round._alpha = fadeAmount;
    fadeAmount++;
    }
    }

    The 'enterFrame' command will occur once everytime the clip enters a new frame. Now, since the clip had only one frame, what it's doing is just re-entering that same frame over and over again (which is why it was important not to use a stop() command). This script sets the alpha of the 'round' object and then increments the value, just like the example above. The 'while' loop makes sure it doesn't do this after it's hit 100.

    I hope this helps you out! If you have any more questions, let me know.

    cheers!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center