?
Solved

Changing the alpha of an instance in AS3

Posted on 2011-02-13
10
Medium Priority
?
370 Views
Last Modified: 2012-05-11
In Actionscript, I have created a For Loop to create six instances of a movie clip.  How can I change the alpha of each instance to be less than the instance before it?
var _boarder:mcBoarder;
var _boarderX:Number = 133;
var _boarderY:Number = 267;
var _boarderR:Number = 0;

for(var i:Number = 0; i < 6; i++)
{
	_boarder = new mcBoarder();
	addChild(_boarder);
	_boarder.x = _boarderX;
	_boarder.y = _boarderY;
	_boarder.rotation = _boarderR;
	_boarderX += 60;
	_boarderY -= 60;
	_boarderR -= 45;
}

Open in new window

0
Comment
Question by:MrJiggyHill
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 1200 total points
ID: 34885058
Is it as simple as this?


var _boarder:mcBoarder;
var _prevBoarder:mcBoarder;
var _boarderX:Number = 133;
var _boarderY:Number = 267;
var _boarderR:Number = 0;

var _frameCount = 6;
var _firstAlpha = 100;
var _deltaAlpha = 10/ _frameCount ;

for(var i:Number = 0; i <  _frameCount ; i++)
{
      _boarder = new mcBoarder();
      addChild(_boarder);
      _boarder.x = _boarderX;
      _boarder.y = _boarderY;
      _boarder.rotation = _boarderR;
      if ( i == 0 ) _boarder.alpha = _firstAlpha;
      else _boarder.alpha = _prevBoarder.alpha - _deltaAlpha;
      _boarderX += 60;
      _boarderY -= 60;
      _boarderR -= 45;
      _prevBoarder = _boarder;
}
0
 

Author Comment

by:MrJiggyHill
ID: 34885072
Unfortunately not.  The alpha didn't change for each instance.
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34885102
I think the "alpha" property may actually have an underscore, as in _boarder._alpha

Sorry 'bout that.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:MrJiggyHill
ID: 34885132
No, I got compiler errors doing ._alpha
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34885161
Ok.  Go back to the original.  I had a typo (10 instead of 100).  Should read like this:

var _deltaAlpha = 100/ _frameCount ;

0
 
LVL 17

Assisted Solution

by:deepanjandas
deepanjandas earned 800 total points
ID: 34885199
First of all, as this is AS3, it cannot be _alpha, has to be .alpha.

Also the alpha values in AS3 ranges from 0-1.
So it will be in decimal.

var _firstAlpha = 1;
var _deltaAlpha = 1/ _frameCount ;


Warm Regards
Deepanjan Das
0
 

Author Comment

by:MrJiggyHill
ID: 34885204
still no change in the alpha
0
 

Author Comment

by:MrJiggyHill
ID: 34885217
Ah - that worked deepanjandas!  The alpha is now changing.  

Thank you guys very much!
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 34885221
deepanjandas is right.  Corrected (and simplified) code.

var _boarder:mcBoarder;
var _boarderX:Number = 133;
var _boarderY:Number = 267;
var _boarderR:Number = 0;

var _frameCount = 6;
for(var i:Number = 0; i <  _frameCount ; i++)
{
      _boarder = new mcBoarder();
      addChild(_boarder);
      _boarder.x = _boarderX;
      _boarder.y = _boarderY;
      _boarder.rotation = _boarderR;
      _boarder.alpha = 1.0 * ( _frameCount - i ) / _frameCount;
      _boarderX += 60;
      _boarderY -= 60;
      _boarderR -= 45;
} 

Open in new window

0
 
LVL 17

Expert Comment

by:deepanjandas
ID: 34885227
Try this for alpha:
var frameCount:int = 6;
var baseAlpha:Number = 1/_frameCount;

for(var i:Number = _frameCount; i > 0; i--)
{
      _boarder = new mcBoarder();
      addChild(_boarder);

      _boarder.x = _boarderX;
      _boarder.y = _boarderY;
      _boarder.rotation = _boarderR;

      _boarder.alpha = baseAlpha * i;
      else _boarder.alpha = _prevBoarder.alpha - _deltaAlpha;

      _boarderX += 60;
      _boarderY -= 60;
      _boarderR -= 45;
      _prevBoarder = _boarder;
}

Open in new window


Warm Regards
Deepanjan Das
0

Featured Post

7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa‚Ķ
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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.
Suggested Courses

765 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