Solved

Flash action script to produce a shape with a gradient fill: I can't get it to work.

Posted on 2007-11-26
6
274 Views
Last Modified: 2011-09-20
According to a tutorial at this link ...  http://www.adobe.com/support/flash/action_scripts/actionscript_dictionary/actionscript_dictionary510.html  
... the code below will produce a box 200x200 with a diagonal gradient fill from blue to red. I put the code in a frame action, but when I published it I got nothing.  What else do I need in the script or on the timeline to produce the gradient filled shape?

Thanks,
John



 

_root.createEmptyMovieClip( "grad", 1 );

              with ( _root.grad )
 

              {
 

              colors = [ 0xFF0000, 0x0000FF ];

              alphas = [ 100, 100 ];

              ratios = [ 0, 0xFF ];

              matrix = { matrixType:"box", x:100, y:100, w:200, h:200, r:(45/

180)*Math.PI }; 

              beginGradientFill( "linear", colors, alphas, ratios, matrix );

              moveto(100,100);

              lineto(100,300);

              lineto(300,300);

              lineto(300,100);

              lineto(100,100);

              endFill();

              }

Open in new window

0
Comment
Question by:gabrielPennyback
  • 4
6 Comments
 
LVL 34

Accepted Solution

by:
Aneesh Chopra earned 500 total points
ID: 20356138
here is the fixed code:

import flash.geom.*

//

var mc:MovieClip = this.createEmptyMovieClip("grad", 1);
 

var colors = [0xFF0000, 0x0000FF];

var alphas = [100, 100];

var ratios = [0, 0xFF];

var matrix = {matrixType:"box", x:100, y:100, w:200, h:200, r:(45/180)*Math.PI};

mc.beginGradientFill("linear",colors,alphas,ratios,matrix);

mc.moveTo(100,100);

mc.lineTo(100,300);

mc.lineTo(300,300);

mc.lineTo(300,100);

mc.lineTo(100,100);

mc.endFill();

Open in new window

0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 20356158
Although you can do it using "with()" also,
only problem in your code were:
 - you haven't imported the "flash.geom.*" package
 - "lineto" should be "lineTo" (T should be in caps)
 - "moveto" should be "moveTo" (T should be in caps)

here is the your code with above mentioned fixes and it also works fine...


import flash.geom.*;

//

_root.createEmptyMovieClip("grad",1);

with (_root.grad)

{

	colors = [0xFF0000, 0x0000FF];

	alphas = [100, 100];

	ratios = [0, 0xFF];

	matrix = {matrixType:"box", x:100, y:100, w:200, h:200, r:(45/180)*Math.PI};

	beginGradientFill("linear",colors,alphas,ratios,matrix);

	moveTo(100,100);

	lineTo(100,300);

	lineTo(300,300);

	lineTo(300,100);

	lineTo(100,100);

	endFill();

}

Open in new window

0
 
LVL 23

Expert Comment

by:Zeffer
ID: 20356546
Brilliant as usual Aneesh..so what the hell are Adobe doing publishing this rubbish ?
and they expect people to actually pay for their flawed product?
or are you on a retainer..just kidding..you should be ..or they should be giving it away as you are:)
Z
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 34

Expert Comment

by:Aneesh Chopra
ID: 20357153
Thnx Zeffer,

actually,
The code provided in the Macromedia article will work fine as it is, but only if FLA is published for Flash Player 6 and actionscript 1.0 only.
With AS 2.0 and Flash Player 8.0, code has become case sensative and implementation of "MovieClip.beginGradientFill" has been changed.

secondly,
if someone needs this code for now then he/she must be working on AS2.0 atleast.
that so, I just provided AS2.0 solution without explaining all this stuff.

I hope things will be more clearer now

-------------------
Aneesh Chopra
-------------------
0
 
LVL 1

Author Comment

by:gabrielPennyback
ID: 20360264
Awesome! Thanks, Aneesh.
I'm posting a new question baout how to incorporate naother action into this script. In the meantime, though I've already awarde points, would it be too much to ask how to do this with 3 or 4 colors?

Thanks,
John

And Zeffer, your comment makes me realize why I have not been able to learn ANYTHING EVER from a Macromedia/Adobe tutorial!
0
 
LVL 34

Expert Comment

by:Aneesh Chopra
ID: 20363185
you can add upto 15 colors

just need to update following three arrays accordingly:

colors = [0xFF0000, 0x0000FF];
alphas = [100, 100];
ratios = [0, 255];


colors:Array - An array of RGB hexadecimal color values you can use in the gradient; for example; red is 0xFF0000, blue is 0x0000FF. You can specify up to 15 colors. For each color, ensure to specify a corresponding value in the alphas and ratios parameters.

alphas:Array - An array of alpha values for the corresponding colors in the colors array; valid values are 0 to 100. If the value is less than 0, Flash uses 0. If the value is greater than 100, Flash uses 100.

ratios:Array - An array of color distribution ratios; valid values are 0 to 255. This value defines the percentage of the width where the color is sampled at 100%. Specify a value for each value in the colors parameter.


here is sample code for these three arrays for 3 color gradient:

//--------------
var colors = [0xFF0000, 0x0000FF, 0x009900];
var alphas = [100, 100, 100];
var ratios = [0, 125, 255];
//----------------


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
blocked plugin flash 5 111
Adobe Flash Player won't run in IE or Firefox but will in Chrome 20 327
File drag and drop 6 131
Cannot install Adove Flash player on Windows 2012R2 6 96
I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
This article describes a solution to a problem of subloading one movie into another when they have different SWF versions. Sometime back, I was working on an ActionScript project while I came across an interesting fact which I would like to share…
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.
This Micro Tutorial will teach to how to utilize bit rate in Adobe Flash Media Live Encoder.

947 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

22 Experts available now in Live!

Get 1:1 Help Now