Controlling the Color of a Flash Movie Clip with Sliders

written by: Elis Frugalo; article published: year 2007, month 07;



In: Categories » Computers and technology » Flash » Controlling the Color of a Flash Movie Clip with Sliders

This tutorial presents a full application that creates sliders for the red, green, blue, and alpha values that control a movie clip's color:

  1. Create a new Flash document and save it.

  2. On the main timeline, rename the default layer as movieClips and create a new layer named actions.

  3. Create a movie clip symbol and draw a circle in it. The circle should be approximately 120 x 120 pixels.

  4. Return to the main timeline and create an instance of the circle movie clip on the Stage on the movieClips layer. Place the instance on the left side of the Stage. Name the instance circle_mc using the Property inspector.

  5. Open the Components panel (Window Components) and drag four instances of the ScrollBar component onto the Stage on the movieClips layer. Name these instances red_sb, green_sb, blue_sb, and alpha_sb. Line them up horizontally on the right side of the Stage.

  6. Select the keyframe of the actions layer and open the Actions panel.

  7. Add the following code to the Actions panel and test the movie (Control Test Movie). The scrollbars are automatically colorized to indicate the color components they control. Moving the thumb sliders on the scrollbars adjusts the circle's color.

// Define a function that will initialize the scrollbar instances as sliders to
   // control the color values.
   function initSliders (  ) {
 // First, set the scroll properties of each of the scrollbars. For the red,
   // green, and blue scrollbars, the values should range from 0 to 255. Use a
   // pageSize of 120 for the color sliders to create a proportional thumb bar. 
   // The alpha range is from 0 to 100, and so the pageSize should be 47 to create
   // a thumb bar that is proportional with the other sliders.
   red_sb.setScrollProperties  (120, 0, 255);
   green_sb.setScrollProperties(120, 0, 255);
   blue_sb.setScrollProperties (120, 0, 255);
   alpha_sb.setScrollProperties(47,  0, 100);
 // Colorize the sliders themselves. Make the red_sb slider red and, similarly,
   // make green_sb green and blue_sb blue. Make the alpha_sb slider white.
   red_sb.setStyleProperty  ("face", 0xFF0000);
   green_sb.setStyleProperty("face", 0x00FF00);
   blue_sb.setStyleProperty ("face", 0x0000FF);
   alpha_sb.setStyleProperty("face", 0xFFFFFF);
 // Set the initial position for the color sliders. alpha_sb remains at 100%.
   red_sb.setScrollPosition  (127);
   green_sb.setScrollPosition(127);
   blue_sb.setScrollPosition (127);
   }
function initColor (  ) {
   // Store a new Color object in a property of circle_mc.
   my_color = new Color(circle_mc);
   circle_mc.col = my_color;
 // Store references to the four scrollbars as properties of circle_mc.
   circle_mc.red   = red_sb;
   circle_mc.green = green_sb;
   circle_mc.blue  = blue_sb;
   circle_mc.alpha = alpha_sb;
   }
// Initialize the sliders and the Color object.
   initSliders(  );
   initColor(  );
// Update the color of the circle_mc movie clip based on the slider positions.
   circle_mc.onEnterFrame = function (  ) {
   // Retrieve the current position of the color and alpha sliders.
   var r = 255 - this.red.getScrollPosition(  );
   var g = 255 - this.green.getScrollPosition(  );
   var b = 255 - this.blue.getScrollPosition(  );
   var a = 100 - this.alpha.getScrollPosition(  );
 // Set up the transformation object properties to set circle_mc's color.
   transformObj = new Object(  );
   transformObj.ra = 0;
   transformObj.rb = r;
   transformObj.ga = 0;
   transformObj.gb = g;
   transformObj.ba = 0;
   transformObj.bb = b;
   transformObj.aa = a;
   transformObj.ab = 0;
   this.col.setTransform(transformObj);
   }

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. ActionScript: Filling a Shape with a Gradient
Use the beginGradientFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. In a gradient fill, there is a graded change in colors. Flash supports linear gradients, in which one color fades into the next from left to right. Flash also supports radial gradients, in which the colors radiate out from a center point. You can initiate a gradient-filled shape using beginGradientFill( ) in the same way you initiate a solid-filled shape with beginFill( ). The difference is that the call to beginGradientFill( ) require...

2. Scripting Masks in Flash
Use the Drawing API to create a shape and then use MovicClip.setMask( ) to apply the mask. Masks can be used to create unique shapes or visual effects. For example, you can use masks to create wipes and transitions or interesting animations in which only the masked portion of the artwork is visible at a given time. You can even create masks that change shape over time, and use them to mask bitmapped graphics (in movie clips). You can use any movie clip as a mask of another movie clip using the setMask( ) method. The setMask( ...

3. Drawing a Triangle using ActionScript
Create a custom MovieClip.drawTriangle( ) method using the Drawing API and invoke it on a movie clip. You can determine and plot the vertices of a triangle given the lengths of two sides and the angle between them. This is a better approach than specifying the lengths of the three sides because knowing the lengths of two sides and the angle between them always determines a triangle, whereas three arbitrary sides may not fit together to make a triangle. The custom drawTriangle( ) method accepts six parameters: ab ...

4. ActionScript: Filling a Shape with a Solid or Translucent Color
Use the beginFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. To draw a filled shape, call beginFill( ) prior to any other drawing methods, including the custom methods you have defined such as drawCircle( ) and drawPolygon( ). Invoke endFill( ) after calling other drawing methods to create the shape. You cannot apply a fill to an existing shape drawn at authoring time or runtime. You must invoke beginFill( ) before drawing the shape to be filled. This example creates a solid blue ...

5. Drawing a rectangle using ActionScript
Create a custom MovieClip.drawSimpleRectangle( ) method using the Drawing API and invoke it on a movie clip. To draw a simple rectangle, specify the stroke's attributes using the lineStyle( ) method and then draw four lines using the lineTo( ) method: // Create rectangle_mc with a depth of 1 on the main timeline. _root.createEmptyMovieClip("rectangle_mc", 1); // Specify a one-pixel, solid, black line. rectangle_mc.lineStyle(1, 0x000000, 100); // Draw four lines to form the perimeter ...

6. How to draw a rectangle with rounded corners
You want to draw a rectangle with rounded corners, an offset, or rotation.Create a custom MovieClip.drawRectangle( ) method using the Drawing API and invoke it on a movie clip. The drawSimpleRectangle( ) method is, as the name suggests, quite simple. Let's create a more complex version that also: Draws a rectangle with a specified angle of rotation Let's you specify the rectangle center's coordinates Can draw a rectangle with rounded corners The drawRectangle( ) method accepts...

7. Reusing and Organizing Code in Flash Movies
You want to reuse code that you've created for one project in another Flash movie. Or you want to write your ActionScript code in an external text editor. Place your ActionScript code in external .as files and use the #include directive to add them to your Flash movies: // Adds all the code within MyActionScriptFile.as to your Flash movie. #include "MyActionScriptFile.as" Use the #include directive to incorporate code from external text files into your Flash movie during compilation from a .fla file ...

8. Drawing a Circle using ActionScript
Create a custom MovieClip.drawCircle( ) method using the Drawing API and invoke it on a movie clip. You can create a circle in ActionScript with eight curves. Fewer curves results in a distorted circle and too many curves hinders performance. Let's create a custom method of the MovieClip class for drawing circles. This method, drawCircle( ), allows for three parameters: radius The radius of the circle x The x coordinate of the circle's center point. If undefined, the circle is centered at x =...

9. ActionScript: Repeating a Task at Timed Intervals
You want to perform an action or actions at a specific timed interval. Use the setInterval( ) function. The setInterval( ) function allows you to specify an interval (in milliseconds) at which your Flash movie will invoke a function. Use setInterval( ) to perform a particular action over time but not necessarily at the frequency of the frame rate of the movie. // Define a function. function myIntervalFunction ( ) { // Output the difference between the current timer value and its value from the ...

10. Mouse Location Flash Script
Not only can you get the location of a movie clip on the screen, you can even get the location of the mouse, also known as the cursor. What is the difference between the mouse and the cursor? The mouse is the physical device attached to your computer. You may even have a track pad or tablet instead. The cursor is the graphic that moves around the screen as you move your mouse. So, technically, cursor is the term I should be using here. However, ActionScript uses the term mouse in its keywords. I will therefore use mouse and cursor i...