How to make a Mask in AS3 - actionscript-3

I have a duvifa that is:
I wonder if it is possible to make a mask in a movieClip the way it is below:
If this image would be the beginning of the game.
When the user clicks on the screen, a type of cream would be created, but not like he would extend all over the stage, but only in my way.
As below:
And also like to know if I can get a percentage of what was filled in the form, to see if the user has filled at least 40%.
Thank U.

Here is a quick example using flashPro, assuming you have your shape graphic as an import .png library object with an export for actionscript class of MC.
import flash.display.Shape;
import flash.events.Event;
import flash.display.BlendMode;
var mc:MC = new MC(); //the background image
addChild(mc);
var drawing:Shape = new Shape(); //drawing foreground
addChild(drawing);
var mcMask:MC = new MC(); //mask version of background image
addChild(mcMask);
var outerMask:Shape = new Shape(); //this object masks the areas outside the bounds of mcMask object
outerMask.graphics.beginFill(0);
outerMask.graphics.drawRect(0,0,mcMask.width,mcMask.height);
outerMask.graphics.endFill();
addChild(outerMask);
drawing.mask = outerMask; //the mask to the shape whose dimensions equal the image mask
mcMask.blendMode = BlendMode.ALPHA; //this tells the mask graphic to make things underneath it use the same alpha data as this image. This works will with PNG masks.
this.blendMode = BlendMode.LAYER; //the parent needs to have a blend mode of LAYER for it to work properly
stage.addEventListener(MouseEvent.MOUSE_MOVE,draw);
function draw(e:Event):void {
trace("draw");
drawing.graphics.beginFill(0x0000FF);
drawing.graphics.drawCircle(mouseX,mouseY,10);
drawing.graphics.endFill();
}

Related

Retrieving percentage of colors of a MovieClip

today my question is as follows:
I have a game, and one of the phases is that you have to pass a "protective cream" on the doll's body. I already have this effect (image below), I'm using an alpha filter combined with a mask that is drawn.
I wonder how can I do to be checking after 20 seconds of the game, if the user has filled 100% of the masks ...
My code is this (forgive me, I am beginner and Brazilian ... any questions ask me):
stop();
import flash.display.Shape;
import flash.events.Event;
import flash.display.BlendMode;
import flash.display.BitmapData;
import flash.utils.Timer;
var tempoFase2:Timer = new Timer(10000, 1);
var corpo_creme:MovieClip = new corpo_mask();
addChild(corpo_creme);
corpo_creme.x = corpo_branco.x;
corpo_creme.y = corpo_branco.y;
setChildIndex(corpo_branco, 1);
setChildIndex(cabeca, 3);
setChildIndex(corpo_creme, 2);
var drawing:Shape = new Shape();
addChild(drawing);
corpo_creme.mask = drawing;
corpo_branco.blendMode = BlendMode.LAYER;
stage.addEventListener(MouseEvent.MOUSE_MOVE,draw);
function draw(e:Event):void {
drawing.graphics.beginFill(0xFFFFFF);
drawing.graphics.drawCircle(mouseX,mouseY,30);
drawing.graphics.endFill();
}
Thank U.
Try this solution:
https://stackoverflow.com/a/15354416/1627055
Basically, you run BitmapData.threshold() against a mask of type BitmapData. You can also draw the relevant portion of your drawing over a temporary BitmapData object and do the same trick, make sure though that your algorithm will count the areas that are not covered by your drawing as filled - you can achieve this by creating the bitmap data pre-filled with white.

access the current color of movieclip in colPicker as3

i am using a Red movie Clip and i have a col Picker. when this red movie Clip load on the stage, i want to change the color(red) of col Picker. I am using following Code:
colorBox.selectedColor = red_clip.transform.colorTransform;
How the Access the color.Please Guide me.
Are you loading a MovieClip that is like square and monochrome?
I can't imagine the use case for this.
Anyway, to get the color of a given pixel of a movieclip that is on stage, you will need to render it in a bitmap first.
// Imagining you want the color that is at (x_offset, y_offset) in your loaded movieclip.
// create a bitmapdata and draw the loaded clip in it
var bdata : BitmapData = new BitmapData(red_clip.width, red_clip.height, true, 0);
bdata.draw(red_clip);
// read pixel from the bitmapdata to get a color
var color : uint = bdata.getPixel(x_offset, y_offset);
var color_with_alpha : uint = bdata.getPixel32(x_offset, y_offset); // you can also get alpha if you want
// Change color in picker
colorBox.selectedColor = color;

as3 animation with multiple images

I have only 3 images that I'm working with for animating a running animal:
1: animal standing
2: animal in stride/jumping
3: animal on its back dead
Currently, I'm embedding them into the main class as sprites and changing their .alpha properties whenever I want one and not the other 2. Is there a better way to do what I'm doing? One of the annoying things is updating the properties for each sprite individually. It would be ideal to have one object and being able to change the image on the object so I would only have to change one object's properties.
For this it would be best to handle your animation in its own Class!
Something like this, let's say your animal is a horse so that's what I'll call the class
package {
import flash.display.Sprite;
public class Horse extends Sprite{
private var holder:Sprite = new Sprite();
public function Horse() {
var img1:Image1 = new Image1();// ur images from library
var img2:Image2 = new Image2();
var img3:Image2 = new Image3();
holder.addChild(img1);
holder.addChild(img2);
holder.addChild(img3);
addChild(holder);
setImage(0);// set 1st image visible
}
public function setImage(nr:uint):void
{
for(var i:int = 0; i < holder.length;i++;)
holder[i].visible = false;
holder[nr].visible = true;
}
}
}
then you would use it like this for example!
var horse:Horse = new Horse();
addChild(horse);
horse.x = 25;
horse.y = 25; // move the whole object(all 3 images)
horse.setImage(2);// or to whatever frame you need
Use the visible attribute instead of the alpha value. If u just set alpha to 0 it will still be rendered and cost cpu. if you set visible to false it will not be rendered!
EDIT: As Amy pointed out, blitting is a possibilty here to, although the fastest aproach would be BitmapData Frame Assignment!
Here you keep all your Frames as BitmapData in a Vector, which you load from a SpriteSheet and just assign the new BitmapData to your Bitmap instead of using copyPixels. It's faster then blitting and you still have all your builtin methods availible!!

Convert drawn lines into bitmapdata - Bitmap.draw() + Sprite.graphics - wont work

EDIT: The convertation / copy process it self works, I just cant figure out how to tell the bitmapdata, which part of the stage to copy - I tried to solve that problem by movie the canvas to x=0 y=0 didnt show anychanges.
The only thing that showed a change was that I did move the canvas BEFORE drawing to zero, but this is totally buggy because the part of the drawing which has negativ coordinates wont be copied since the coordinate change only affect the bitmap if you do it before you start to paint
OLDER ENTRY:
I want to convert the Sprite.graphics into bitmapData, because I have a drawTool which allowes the user to paint lines, which are located inside the Sprite.grahpics I think.
I need to convert these lines to bitmapdata, because this allows me to deform them later on, but I cant use this
Bitmapdata.draw(Sprite.graphics);
And using only the Sprite instead of Sprite.graphics doesnt show any result =\
help is needed!
Use a matrix if you want to draw only a certain portion and from an origin other than (0,0). There's plenty in the Adobe docs on this, or a good example here:
http://www.quasimondo.com/archives/000670.php
Only use graphics when drawing. The actual Sprite object contains what you want, so following your convention, simply do:
BitmapData.draw(Sprite);
Although for a literal example:
var mySprite:Sprite = new Sprite();
// add lines etc using mySprite.graphics
var myBitmapData:BitmapData = new BitmapData(mySprite.width, mySprite.height);
myBitmapData.draw(mySprite);
I think, you've not completely understand usage BitmapData.draw().
BitmapData.draw() is a All DisplayObject(Sprite, MovieClip, Shape, Text, Video...) drawing possible. because they are have a IBitmapDrawable.(more information refer a adobe document Is the best teacher.)
If you want implement Paint Board. refer a below code. very simple Paint Board. but some help you.
try copy & paste.
import flash.display.Sprite;
import flash.events.Event;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.events.MouseEvent;
var isDraw:Boolean = false;
var brush:Sprite =new Sprite();
brush.graphics.beginFill(0x0000ff);
brush.graphics.drawCircle(0,0,5);
brush.graphics.endFill();
var canvasData:BitmapData = new BitmapData(600,400, false);
var canvas:Bitmap = new Bitmap(canvasData);
addChild(canvas);
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDrawStart);
stage.addEventListener(MouseEvent.MOUSE_UP, onDrawStop);
stage.addEventListener(Event.ENTER_FRAME, render);
function onDrawStart(e:MouseEvent):void
{
isDraw = true;
}
function onDrawStop(e:MouseEvent):void
{
isDraw = false;
}
function render(e:Event):void
{
if(!isDraw) return;
brush.x = mouseX;
brush.y = mouseY;
canvasData.draw(brush,brush.transform.matrix);
}

convert masked movieclip into bitmap and save it on server

i have a
dsgnArea----> a movieclip
dsgnArea is masked by dsgnAreaMask, which inturn is a movieclip
dsgnArea.mask=dsgnAreaMask;
the width,height and position of dsgnAreaMask and dsgnArea are same.
i dynamically added multiple movieclips and labels to dsgnArea;
like..
dsgnArea.addChild(movieClip1);
dsgnArea.addChild(movieClip2);
dsgnArea.addChild(label1);
dsgnArea.addChild(label2); and so on...
these movieclips (movieClip1,movieClip2,......) and labels(label1,label2,....) positions can be altered in runtime..
but as i masked the dsgnArea with dsgnAreaMask, only a portion of the added movieClips and labels are visible...
So, my problem is to grab that visible portion in dsgnArea into a bitmap,like a screenshot of that particular dsgnArea, and save it in my server.
please help me out in this problem.
Say s is the DisplayObject object you want to capture and m is the mask applied on it.
var maskRect:Rectangle = m.getRect(s);
var matrix:Matrix = new Matrix(1, 0, 0, 1, -maskRect.x, -maskRect.y);
var w:Number = Math.min(s.width, maskRect.right) - maskRect.x;
var h:Number = Math.min(s.height, maskRect.bottom) - maskRect.y;
var bd:BitmapData = new BitmapData(w, h);
bd.draw(s, matrix);
Does that work?
The BitmapData draw method is what you are looking for. You can use it's clipRect param to define what you would like to draw (the masked parts).
Quasimondo did a handy little method to do this (take a snapshot of the whole displayObject), it's available here: http://www.quasimondo.com/archives/000670.php
I don't know if it works with masked content though.
if it doesn't, the trick would be to translate the whole content by the size of the mask
var bounds:Rectangle = dsgnAreaMask.getBounds( dsgnAreaMask );
instead of using the content of the clip
var bounds:Rectangle = clip.getBounds( clip );
as far as saving file to server is concerned, the question was asked (answered?) here AS3 Save Media File to server