How reduce aplha value of movie clip using code

  • asifbhura
  • Newbie
  • Newbie
  • asifbhura
  • Posts: 5

Post 3+ Months Ago

Hi everyone

I am trying to reduce alpha value of movie clip. but it doesnt work some code i tried which given below

Code: [ Select ]
onClipEvent(load)
{
    alphavalue=100;
    for(i=0;i<=100;i++)
    {
      _root.almc._alpha = alphavalue;
   }
     alphavalue--;
    
}
  1. onClipEvent(load)
  2. {
  3.     alphavalue=100;
  4.     for(i=0;i<=100;i++)
  5.     {
  6.       _root.almc._alpha = alphavalue;
  7.    }
  8.      alphavalue--;
  9.     
  10. }

Please help me
waiting for reply.

thanx in advance
best regards,
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Sulfin
  • Graduate
  • Graduate
  • User avatar
  • Posts: 129
  • Loc: canada

Post 3+ Months Ago

the alphavalue variable is being set to 100 before the _alpha is set to the alphavalue.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

Your code is making me cross-eyed :wink:

Try this, (main timeline)
Code: [ Select ]
fadeFactor = 15;
fadeTo = 0;
almc.onEnterFrame = function(){
   if(this._alpha > fadeTo){
     this._alpha -= fadeFactor;
   }else{
     delete this.onEnterFrame;
   }
}
  1. fadeFactor = 15;
  2. fadeTo = 0;
  3. almc.onEnterFrame = function(){
  4.    if(this._alpha > fadeTo){
  5.      this._alpha -= fadeFactor;
  6.    }else{
  7.      delete this.onEnterFrame;
  8.    }
  9. }


fadeTo is the alpha level to fade to & fadeFactor is the amount to subtract each iteration of the loop :D
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Since i'm a function freak, and I tend to do A LOT with alpha transitions in actionscript, I wrote this prototype a while back to help me out with fading clips. I hope you can find it useful too.
Code: [ Select ]
MovieClip.prototype.fadeTo = function(value, speed) {
    this.onEnterFrame = function() {
        this.aV = Math.floor(value-this._alpha);
        this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
    };
};
//example useage
clipInstanceName.fadeTo(40, 10);
  1. MovieClip.prototype.fadeTo = function(value, speed) {
  2.     this.onEnterFrame = function() {
  3.         this.aV = Math.floor(value-this._alpha);
  4.         this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  5.     };
  6. };
  7. //example useage
  8. clipInstanceName.fadeTo(40, 10);


clipInstanceName is obviously the instance name of the movieclip symbol you are targeting to fade. The 40 is the final _alpha value you want, and the 10 is the speed in which you want it to get there. Since this prototype uses the easing equation so that it is a smooth alpha transition the higher the speed number, the slower the transition will go.


Oh, and btw, your original code didn't work because a for loop is run all at once in the background and outputs the final result. In Flash a 'loop' that runs constant is the onEnterFrame handler.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

lostinbeta wrote:
your original code didn't work because a for loop is run all at once in the background and outputs the final result.


That & var alphavalue was never touched untill the loop exited (I just noticed that) :P

lostinbeta, any chance of a brief runthrough on your prototypes logic ? The code in the first post had me crosseyed with an incrementing i against a decrementing _alpha, yours has me scratching a hole in my head all together :scratchhead:

oh yeah, Bam !! :lol:
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Code: [ Select ]
MovieClip.prototype.fadeTo = function(value, speed) { };
A prototype is like a normal function, however it has one very significant difference.... it is object oriented. In this case the object is a MovieClip symbol. That means to properly use this function it must be attached to an instance of a movieclip symbol, hence why you call the function as "clipInstanceName.functionName();". Since prototypes are global you don't target them like you do normal functions (_root.functionName(), etc).

Code: [ Select ]
this.onEnterFrame = function() {};
The onEnterFrame handler is an object specific dynamic event handler. It is the dynamic form of "onClipEvent(enterFrame){}".

Code: [ Select ]
   this.aV = Math.floor(value-this._alpha);
   this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
  1.    this.aV = Math.floor(value-this._alpha);
  2.    this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
Now this is the more complicated stuff. I wrote two tutorials on easing in Flash, they both use the same formula, but show the basic method, which the ideas in the equation can be applied to more than just _x and _y positions (as we see in this alpha fade). Here are the tutorials...

Tutorial 1
Tutorial 2

As I stated, the tutorials apply easing to a clips position, but the ideas of the equation are not limited to that, so in this case I brought it to changing the alpha value of a clip.

There are a few sligh differences in how I wrote it. The basic easing equation is set up as
Code: [ Select ]
_property += (endValue-_property)/speed;
However, in the case of alpha fading, I wanted to delete the onEnterFrame handler (only possible in a dynamic event handler, not an onClipEvent handler) after the fade was completed. With the standard easing equation, the numbers it produces requires a cheap workaround to achieve this. So I seperated the "(endValue-_property)" part from the equation and chose to hold it in it's own variable.

The variable in this case is "this.aV" (which to me stands for alpha value), the "this" locator in front of the variable assures that the variable is created within the object calling the function. This is the joy of variable scopes. If you don't have "this" there the variable will be the same value for any clip running this function. The use of "this" assures you can use this on more than one clip at a time because each clip will be reading the value of the variable contained within itself.

I chose to Math.floor() the "(endValue-_property)" part so I wouldn't have to deal with 8 place decimals numbers. A nice round number is what i'm looking for, but I want it to round down so it can hit 0 better.

Code: [ Select ]
this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);
This line right here is probably the most confusing to you. But I hope to clear it up.

The main part that is probably what is confusing about this line is the crap about "?" and ":". A very handy shortcut in many programming languages is what is called the tertiary operator ( http://www.kirupa.com/developer/actions ... rtiary.htm ). It is a shorthand form of an if/else statement
Code: [ Select ]
statement ? do A : do B;
That basically says if statement is true, do A, else do B.

So now that you hopefully have a better understanding of the tertiary operator, let's get back to the code.

Code: [ Select ]
this.aV ? this._alpha += this.aV/speed : (this._alpha=value, delete this.onEnterFrame);


What this says that if the variable this.aV exists/has a value other than 0, the alpha should adjust itself via the basic easing equation. Else if this.aV doesn't exist or is equal to 0, the alpha value of this clip will be set to the value you wanted, and the onEnterFrame dynamic event handler will be deleted thus this code block will stop being run.

In regular if/else form that would go something like this
Code: [ Select ]
if (this.aV) {
 this._alpha += this.aV/speed;
} else {
 this._alpha = value;
 delete this.onEnterFrame;
}
  1. if (this.aV) {
  2.  this._alpha += this.aV/speed;
  3. } else {
  4.  this._alpha = value;
  5.  delete this.onEnterFrame;
  6. }


I help this better clarifies for you joebert :)
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13503
  • Loc: Florida

Post 3+ Months Ago

**Starts taking electrical outlet covers off, checking lightbulbs, "Where's the camera ?!?"
lostinbeta wrote:
This line right here is probably the most confusing to you. But I hope to clear it up.

yes & yes :D Thanks once again lostinbeta :)

So,
Code: [ Select ]
ifIGetThisRight ? (thisWillRun(true), thenThis(true)) : (thisWillRun(false), thenThis(false))
  • lostinbeta
  • Guru
  • Guru
  • User avatar
  • Posts: 1402
  • Loc: Philadelphia, PA

Post 3+ Months Ago

Yeppers.

If it is just one statement you are not required to have the parenthesis, but as you did when there are more than 2 statements the parenthesis are required and the statements must be seperated by a ",". So you nailed it on the head.

The tertiary operator obviously isn't optimal for an if/else statement that will contain many statements, but it's a nice simple way to compress your code when you don't have much going on in the if/else.

Flash re-converts it to the standard if/else format during export so technically there isn't any optimizing going on, just making things harder/easier to read/write ;)

Post Information

  • Total Posts in this topic: 8 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.