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

Posted on 2007-11-26
Medium Priority
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?



_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 );

Open in new window

Question by:gabrielPennyback
  • 4
LVL 34

Accepted Solution

Aneesh Chopra earned 2000 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};

Open in new window

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.*;
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};

Open in new window

LVL 23

Expert Comment

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:)
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

LVL 34

Expert Comment

by:Aneesh Chopra
ID: 20357153
Thnx Zeffer,

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.

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

Author Comment

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?


And Zeffer, your comment makes me realize why I have not been able to learn ANYTHING EVER from a Macromedia/Adobe tutorial!
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];


Featured Post

Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
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…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.

568 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