Talking about Actionscript 3, Flash, Game development and Tutorials.
Before you start reading this tutorial, there is something you must do.
Turn off the lights, close your eyes, raise your hands, and say:
« Hail, Lord of Flash Game. Shall I read this tutorial and forget it before I try to flood the web with another silly Coloring Book Game »
Really, seriously… I am showing you how do it but please, please… don’t do it.
Finding the right image
If you can’t draw your image, you’ll have to download it from the web. Try to Google for coloring book and you’ll know what I mean.
Just remember the “copyright” word… I don’t think you’re allowed to use Ben 10 or Hello Kitty images… but if you are about to create a Flash coloring book, you are an evil person and stealing artworks won’t be a problem.
The image I’ll use is taken from www.coloringbook.info
Main concept
Every colorable part is a distinct movieclip, and its color is dynamically changed with Actionscript
Converting the image
First, you need to import it into Flash
File->Import->Import to Stage
Then, select the image on the stage, and convert it from bitmap to vector
Modify->Bitmap->Trace Bitmap
with these settings:
Color threshold: 100
Minimum area: 1
Curve fit: Very Smooth
Corner threshold: Few Corners
Now you have your bitmap converted into a Flash shape. Select all the black outline clicking on every black line you see while pressing Shift, then convert it to a symbol
Modify->Convert to Symbol
name it outline
and don’t check Export for Actionscript
Now do the same thing with the head…
Then with the remaining body parts and eventually the background. Once the shape has been divided into objects, select all objects on the stage and convert them as a unique symbol you will call teddy_mc
.
This time remember to check Export for Actionscript
Now you can delete everything from the stage and remove the image from the library.
Now your library should look like this one:
Now edit teddy_mc
movieclip, selecting every movieclip inside it (head, outline, …) and giving it a proper instance name… head
for the head, feet
for the feet and so on
Obviously tracing a bitmap won’t give the same quality results as if you were able to draw your coloring book by yourself
Finally you are ready to code some interesting lines
Actionscript
The aim of this script is putting on stage the teddy bear, a three color palette, setting a default color, allowing people to change color clicking on the palette, and painting the teddy bear.
package {
import flash.display.Sprite;
import flash.geom.ColorTransform;
import flash.events.MouseEvent;
public class coloring extends Sprite {
public var teddy:teddy_mc = new teddy_mc();
public var palette:palette_mc;
public var pal_color:ColorTransform;
public var colors:Array=new Array(0xFF0000,0x00FF00,0x0000FF);
public var current_color:int=0;
public function coloring():void {
addChild(teddy);
teddy.addEventListener(MouseEvent.CLICK,on_teddy_click);
for (var i:int=0; i<3; i++) {
palette = new palette_mc();
pal_color=palette.transform.colorTransform;
pal_color.color=colors[i];
palette.transform.colorTransform=pal_color;
palette.x=40+i*60;
palette.y=300;
palette.ind=i;
addChild(palette);
palette.addEventListener(MouseEvent.CLICK,on_palette_click);
}
}
public function on_palette_click(e:MouseEvent):void {
var palette_clicked:palette_mc=e.currentTarget as palette_mc;
current_color=palette_clicked.ind;
}
public function on_teddy_click(e:MouseEvent):void {
for (var i:int = 0; i < teddy.numChildren; i++) {
if (teddy.getChildAt(i).hitTestPoint(mouseX,mouseY,true)) {
pal_color=teddy.getChildAt(i).transform.colorTransform;
pal_color.color=colors[current_color];
teddy.getChildAt(i).transform.colorTransform=pal_color;
}
}
}
}
}
Line 3: ColorTransform
class lets you adjust the color values in a display object
Line 6: Declaring the teddy bear object
Line 7: Declaring the palette: it's a simple white circle that will be instanced three times in three different colors
Line 8: Variable used to handle color adjustment
Line 9: Array with possible colors... in this case red, green and blue
Line 10: Setting the default color
Line 12: Placing the teddy bear on stage
Line 13: Adding a listener to know when I'm clicking on it
Line 14: Beginning of the loop that will create the three-color palette
Line 15: Creating a new palette object
Lines 16-18: Giving the palette object the i
-th color in the colors
array
Lines 19-20: Setting the palette position on the stage
Line 21: Giving the palette an index, to easily recognize it
Line 22: Adding the palette on stage
Line 23: Adding a listener to know when I'm clicking on a palette
Lines 26-29: Changing the current color according to the index (see line 21) of the palette I clicked
Line 31: Looping through all teddy's children (the colorable parts)
Line 32: Checking which movieclip I clicked
Lines 33-35 Changing its color in the same way shown at lines 16-18
And this is the result:
Play with colors... as you can see the antialiasing sucks... but you can make it perfect if you draw the image by yourself.
Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.