The first of all ( and private advice from me ) is you do this type of programming at ActionScript 3.0 , taking into consideration the practicality of handling screen clicks, since AS3 is event-oriented , and possibly you will be more familiar.
Following the logic of the video in question, I have prepared a simple class that performs both operations, both fills with solid colors GraphicsSolidFill
as bitmap textures GraphicsBitmapFill
.
Understanding Object Filling
Any and every drawing you perform with fills, has an object called Shape
( ref. ). In this object, a property named graphics
( ref. ) controls the shapes and lines of your "drawing", which means that you can change any property of your object.
This drawing is nothing more than an image in Vector Drawing . Which means that the measurement of the same is not in Pixels, but in Mathematical Coordinates. Ref .
Let's use exactly this object to handle all the modifications of our movieclips.
Answering the question
Make a drawing on the stage and each part of your drawing will convert it to a MovieClip
. (Just like in the video.It is important to instantiate them, eg braco_mc, leg_mc, head_mc ... 'Assuming it is a doll.')
We will create our function, let's call it fillTo();
which receives a MovieClip
and an object, either Bitmap
or hexadecimal color uint
:
var colortransform:ColorTransform = new ColorTransform(); //Criamos o objeto que aplica a transformação de cores no nosso desenho;
var shapeGD:Vector.<IGraphicsData>; //Objeto vetor que contém os dados da nossa imagem
var newGD:Vector.<IGraphicsData>; //Irá contém os novos dados do nosso desenho
function fillTo(mc:MovieClip, obj:*):void {
var shape:Shape = (mc.getChildAt(0) as Shape); //Recupera o desenho dentro do movieclip
shapeGD = shape.graphics.readGraphicsData(); //Recupera os dados do nosso objeto Shape (curvas e linhas)
newGD = new Vector.<IGraphicsData>;
//Para cada objeto dentro do nossos objeto vetor, recuperamos apenas a propriedades de linhas e curvas, deixando de lado as de preenchimento
shapeGD.forEach(function(it:*, i:int, vec:Vector.<IGraphicsData>):void {
if(!(it is GraphicsBitmapFill) && !(it is GraphicsGradientFill) && !(it is GraphicsSolidFill)) {
newGD.push(it); //Adicionar somente as propriedades citadas
}
});
//Verificamos se a propriedade passada é um Bitmap ou cor hexadecimao uint
if(obj is Bitmap) {
//Recupera o bitmap (no caso a textura)
var bitmapData:BitmapData = (obj as Bitmap).bitmapData;
//Objeto responsável por adicionar o preenchimento em bitmap ao nosso desenho
var graphicsbitmap:GraphicsBitmapFill = new GraphicsBitmapFill(bitmapData);
newGD.push(graphicsbitmap); //Puxa este objeto para nosso vetor
//Adiciona as novas propriedades ao nosso desenho
shape.graphics.beginBitmapFill(bitmapData, null, true, true);
shape.graphics.drawGraphicsData(newGD);
}
else if(obj is uint) {
//O procedimento é o mesmo, mas o objeto é diferente
//aplicamos apenas a cor sólida ao nosso objeto
var graphicscolor:GraphicsSolidFill = new GraphicsSolidFill(obj);
newGD.push(graphicscolor);
shape.graphics.beginFill(obj);
shape.graphics.drawGraphicsData(newGD);
}
}
To fill in our movieclips, you just have to call the function like this:
fillTo(perna_mc, 0xFF0000); //Pintando a perna de vermelho
fillTo(perna_mc, textura_bitmap); //Pintando a perna com a textura em bitmap
I made a class called Filler , to simplify the tutorial.