Solved

Changing the alpha of an instance in AS3

Posted on 2011-02-13
10
364 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
  • 4
  • 4
  • 2
10 Comments
 
LVL 16

Accepted Solution

by:
sjklein42 earned 300 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 17

Assisted Solution

by:deepanjandas
deepanjandas earned 200 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best way to find duplicate message in database 7 29
Can not play videos on sciencechannel.com in Chrome 13 114
Problem to echo 6 81
Problem to picture file 3 68
I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
The goal of the tutorial is to teach the user how to select which audio input to use. Once you have an audio input plugged into the laptop or computer, you will go into the audio input settings and choose which audio input you want to use.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now