Solved

Changing the alpha of an instance in AS3

Posted on 2011-02-13
10
363 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
Comment Utility
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
Comment Utility
Unfortunately not.  The alpha didn't change for each instance.
0
 
LVL 16

Expert Comment

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

Sorry 'bout that.
0
 

Author Comment

by:MrJiggyHill
Comment Utility
No, I got compiler errors doing ._alpha
0
 
LVL 16

Expert Comment

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

var _deltaAlpha = 100/ _frameCount ;

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 17

Assisted Solution

by:deepanjandas
deepanjandas earned 200 total points
Comment Utility
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
Comment Utility
still no change in the alpha
0
 

Author Comment

by:MrJiggyHill
Comment Utility
Ah - that worked deepanjandas!  The alpha is now changing.  

Thank you guys very much!
0
 
LVL 16

Expert Comment

by:sjklein42
Comment Utility
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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The last time I worked with Flash and Socket connections was in AS1. A recent project required flash connecting to a Socket, and sending receiving information - we figured it would be easy enough - we all know about the socket policy documents and c…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The goal of the tutorial is to teach the user how to select the video input device. Make sure you have an input device that in connected and work and recognized by Adobe Flash Media Live Encoder and select it in the “video input” menu.

771 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

10 Experts available now in Live!

Get 1:1 Help Now