PDA

View Full Version : 2 Different ways to fade in a MC with AS (1 of them is not working...)



Manare
June 8th, 2005, 09:41 PM
Hi!
This should be very simple: I want to fade-in a Movieclip (called MC1) using Actionscript.
I normally use this (and it works just fine):


this.MC1._alpha = 0;
this.MC1.onEnterFrame = function()
{
if (MC1._alpha <100) {
trace(MC1._alpha);
MC1._alpha +=5;
}
};


However, this time I need to do it without using a function, so I've tried the following:

this.MC1._alpha = 0;
while (MC1._alpha <100) {
trace(MC1._alpha);
MC1._alpha +=5;
}

Well, even though I can see the alpha values decreasing step by step (throughout the "trace" instruction), the fact is that the MC does not fade-in at all, it's just starts at a 100% alpha value.
How comes this illogical behaviour? The instruction "trace" shows what it should do, but Flash does not fade it in.
How can I do then what I want?

...this AScript is killing me...!

Thanks for reading,
Manare

drosa10
June 8th, 2005, 11:20 PM
try this:



mc._alpha=0
for(i=0;i<20;i++){
mc._alpha+=5
}

scotty
June 9th, 2005, 02:55 AM
The loop you're using is executed in one frame, so you need an onEnterFrame to see the fadeIn :)

scotty(-:

Manare
June 9th, 2005, 09:26 AM
Hi again!
Thanks drosa10 and scotty for your interest and help...however this piece of AS code did not work properly.

Well, the reason why I need to do it without the use of a function is because if used, then the behaviour differs from what expected. That is: if I don't use a function, there's an "IF" statement which returns the expected value, however, when using a function, the "IF" statement returns an "Undefined" value. I don't understand why...that's why I don't want to use a function in this case.
Perhaps, what I should wander or ask to this forum, is why is this so...but unless it's something "basic" related to functions that I didn't notice when studied them, I thought at first it would be easier to avoid functions to get this simple alpha effect.
I'll put the AS codes here:

This is the non working version which uses a function.
The problem is the 2nd IF statement. When I use a function, the returned value is "undefined" so the "IF" statement is therefore "False" and the alpha fade-out sequence is not executed.

for (i=1; i<6; i++)
{
if ((eval("_root.submenu.Tapa" + i)._alpha >=100) and (_root["subapart"+i]==0))
{
this.onEnterFrame = function()
{
//Trace should show the real value which is 100. Instead I get "undefined"
trace("Value in 2nd IF statement is "+eval("_root.submenu.Tapa" + i)._alpha);
IF (eval("_root.submenu.Tapa" + i)._alpha >0)
{
eval("_root.submenu.Tapa" + i)._alpha -=5;
}
};
}
}

If I don't use a function, THAT 2nd "IF" statement returns a value of 100 (which is correct), therefore, the alpha fade-out sequence is executed....but only once, I mean, it only does 1 step and decreases the alpha value to 95 (100-5). That's why I tried to substitute that "IF" for a "WHILE" or "FOR" statements instead without success (it seems to be that the loop completes its iterations before the image is displayed because any code that's not within an event handler gets executed before the movie starts). Here's the code:

for (i=1; i<6; i++)
{
if ((eval("_root.submenu.Tapa" + i)._alpha >=100) and (_root["subapart"+i]==0))
{
//Trace now shows the correct value which is 100. So, the here below "IF" is executed.
trace("Value in 2nd IF statement is "+eval("_root.submenu.Tapa" + i)._alpha);
IF (eval("_root.submenu.Tapa" + i)._alpha >0)
{
eval("_root.submenu.Tapa" + i)._alpha -=5;
}
}
}

So here's my question:
- Why the returned value is 100 in the second example (which is correct) and yet is "undefined" in the first example when I use a function? Is there a path problem related to using a function perhaps? I can't think of anything else but for sure, there's something I'm missing.

Thanks for your interest,
Manare