PDA

View Full Version : AS fade in/out while mc moves across stage ...



kubik
December 16th, 2004, 02:40 PM
(Sorry for the double post - I had initially posted this in the MX 2004 forum, which is obviously the wrong place since my question relates to AS. Admins, feel free to delete my first post. Sorry for this.)

Hey everyone!

First off - apologies for yet another "fade question". It's the first time I've used Flash in the last 9 months and I seem to have forgotten every single thing I've ever heard about Flash.* :(

Here's my test scenario: I have a movie clip ("test") that slides across the stage from left to right (or vice versa, doesn't matter) with a duration of 60 frames. I'd like it to begin sliding with an alpha of "0" and fade in to "100" within the first 15 frames, then I'd like it to fade out starting at frame 45 so that by the time it gets to the final frame (60) it's completely transparent again.

For the life of it, I simply can't wrap my head around the necessary AS. Any helping hand(s) you guys can lend would be very much appreciated!!

Thanks in advance!!

:?)


* - I swear I searched the forum for anything that would help me with this but I didn't have any luck digging up something relating to my problem.

newhopekenny
December 16th, 2004, 03:49 PM
If you're tweening your MC across sixty frames, you're either using the timeline to know this or you're using the hidden tween functions with frames instead of seconds.

If you're using the timeline to tween your animation:
All you need to do is start your MC's alpha at 100, and set it to zero on the 60th frame. Boom. Done.

If you're using AS to tween your MC across the stage:
Just take the total duration of the tween in frames (in this case, 60) and then do something like stepVar = startAlpha / totalFrames. And then assign an onEnterFrame to the MC that adds stepVar each time it runs (which will be the frame rate of the movie.

newhopekenny
December 16th, 2004, 04:08 PM
// assign fade start / end values for use later
var firstFadeStart = 0;
var firstFadeEnd = 15;
var secondFadeStart = 45;
var secondFadeEnd = 60;
// make the MC transparent for the fade-in
testMC._alpha = 0;
// onEnterFrame is called at each frame
// that means that if on every frame that if I divide the
// total alpha by the number of frames to fade (endFade - startFade)
// so that I have a value I can add at each frame during the fade
// to evenly fade in/out the MC
testMC.onEnterFrame = function() {
if (_root._currentframe>=firstFadeStart && _root._currentframe<=firstFadeEnd) {
// fade in
this._alpha += 100/(firstFadeEnd-firstFadeStart);
} else if (_root._currentframe>=secondFadeStart && _root._currentframe<=secondFadeEnd) {
// fade out
this._alpha -= 100/(secondFadeEnd-secondFadeStart);
} else if (_root._currentframe == secondFadeEnd) {
// we're done! let's delete the onEnterFrame
// no use wasting good CPU mojo
trace("done -- deleting testMC's onEnterFrame");
delete this.onEnterFrame;
} else {
trace("just chillin', homies -- nothing to fade");
}
};


I know it looks like a lot of code, but it's mostly just the comments I tossed in for clarity that bulk the ***** up.

You can use this to fade in / out at any frame(s), just change the values and you're golden.

This isn't the cleanest way of going about this, but since I'm guessing you're using a timeline to motion tween instead of pure AS, this'll work just fine. ;)

I tried to explain what was going on as best I could, let me know if you have any more questions.

And if you use this anywhere -- you owe me one million dollars. :)

kubik
December 16th, 2004, 05:23 PM
newhopekenny,

thanks a big, fat bunch for your reply! I'm probably about to make a complete idiot out of myself - but I can't get the script to work ... :sen: ... the only thing I get is an error window that traces "just chillin', homies -- nothing to fade" 30 times. I've played around with your script for a while but I'm fresh out of ideas (not a new feeling for me today, hmpft.) on how to get it to work.

If it's no too much to ask - would you mind taking a quick peek at the "test" file I've attached?

Cheers,

kubik


P.S. And yes - goes without saying that as soon as I received the million dollars for this project I will immediately forward the cash to your account ...

:flower:

scotty
December 17th, 2004, 04:35 AM
for free;)

stop();
testMC._alpha = 0;
var curframe = 0;
testMC.onEnterFrame = function() {
curframe++;
this._x -= 10;
if (curframe<=15) {
this._alpha += 100/15;
} else if (curframe>45 && curframe<=60) {
this._alpha -= 100/15;
} else if (curframe>60) {
trace("done -- deleting testMC's onEnterFrame");
delete this.onEnterFrame;
}
};

scotty(-:

kubik
December 17th, 2004, 08:20 AM
for free ...
Thank you very much scotty!! Much appreciated!

And ... now is as good a time as any to get back into AS ... I guess it's just like any other language - if you don't speak/use it for a while, you forget everything.

Now I just need to get back into the swing of things and figure out the usual suspects ... setInterval, speed, easing, etc. In case you have a link to a tutorial on how to combine those (e.g. "wait n milliseconds until tween starts, ease and fade in, speed/duration of the tween, ease and fade out") that would be wonderful - but don't sweat it. Looks like I'll be spending a whole lotta time here over the weekend doing many, many searches to get my AS knowledge back to what it used to be.

:)

Thanks again - that was mighty nice of you!

kubik

scotty
December 17th, 2004, 09:57 AM
you're welcome;)

Have a search on this site for "sequence", "easing", "setInterval" and so on with that beautiful :love: search button :love: on the top (3rd from the right) and you'll find lots of examples/ideas.

scotty(-: