Creating a color preview in AS3 for a drawing application - actionscript-3

i'm quite new to AS3 but I know the basics. I'm creating a drawing application and I would like to create a rectangle that changes to the color selected everytime a new color is selected. I can do the easy stuff such as creating the rectangle etc. but for the actual color transformation I am a bit lost. I haven't found a direct answer with this and I was hoping someone could guide me in the right direction, thanks!

This is a simple program that creates a red button on the stage. When clicked, it changes to blue.
import flash.display.MovieClip;
import flash.events.MouseEvent;
var ButtonA:MovieClip = new MovieClip();
ButtonA.buttonMode = true;
ButtonA.graphics.lineStyle(1,0);
ButtonA.graphics.beginFill(0xFF0000,1)
ButtonA.graphics.drawRect(0,0,50,50);
addChild(ButtonA);
ButtonA.x = ButtonA.y = 20;
ButtonA.addEventListener(MouseEvent.CLICK, changeColorHandler, false, 0, true);
function changeColorHandler(event:MouseEvent) : void
{
var obj_color:ColorTransform = new ColorTransform();
obj_color.color = 0x0000ff;
ButtonA.transform.colorTransform = obj_color;
}
This uses a click event, but you can take the code inside the handler and create your own color setter function that you can call from wherever you want. i.e:
function changeColor(obj:MovieClip, newColor:int) : void
{
var obj_color:ColorTransform = new ColorTransform();
obj_color.color = newColor;
obj.transform.colorTransform = obj_color;
}

To change the color of rectangle when new color is selected , i think you should use color picker control from flash Professional ,please import color picker control and attach to you project
function ColorPicker_Class()
{
colorPicker=new ColorPicker();
addChild(colorPicker);
colorPicker.x=150;
colorPicker.addEventListener(ColorPickerEvent.CHANGE,onChangeColor);
rect=new Sprite();
rect.x=300; rect.y=250;
addChild(rect);
}
function onChangeColor(event:ColorPickerEvent):void
{
var colorpick:ColorPicker=ColorPicker(event.currentTarget);
rect.graphics.lineStyle(1);
rect.graphics.beginFill(colorpick.selectedColor,1);
rect.graphics.drawRect(0,0,100,100);
}

Related

AS3 Fill Color inside line by pen

I was trying to make a flash app with AS3 where I can draw line with pen tool with different colors , also fill the shapes on an image with different colors, now I have went through various tutorials and achieved it, however in the end I am faced with 2 problems that I am unable to solve even after 3 days of efforts:
How can I fill color inside the shapes formed by using the pen tool,
say if I draw a rough circle using pen tool and then I try and fill
it with green, how can I detect MovieClips which I need to fill.
When I draw lines over shapes and then try and fill the shapes, the
shapes gets filled but the lines still appear on top of the shapes
filled with color.
You can get a better idea of what I have achieved by visiting this link, click the pen symbol and paint bucket symbol to see how it works.
Below is my code for pen tool and fill color:
I draw a sprite add an image and then use the property to detect color to draw a line of color I choose, followed by code to fill color where I divide the image in various MovieClips and then make then into one and detect if mouse is clicked on which clip and fill it with selected color.
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;
bucbut.addEventListener(MouseEvent.CLICK,nClick0PC);
/////////////pentool code --------
convertToBMD();
pbut.addEventListener(MouseEvent.CLICK,nClick0P);
function nClick0P(event:MouseEvent):void{
spBoard.addEventListener(MouseEvent.ROLL_OUT,boardOut);
spBoard.addEventListener(MouseEvent.MOUSE_MOVE,boardMove);
spBoard.addEventListener(MouseEvent.MOUSE_DOWN,boardDown);
spBoard.addEventListener(MouseEvent.MOUSE_UP,boardUp);
}
var spBoard:Sprite=new Sprite();
this.addChildAt(spBoard,0);
spBoard.x=20;
spBoard.y=100;
var owl2:owl;
owl2 = new owl();
owl2.name="owl1";
spBoard.addChildAt(owl2,0);
owl2.x=315;
owl2.y=180;
var shDrawing:MovieClip = new MovieClip();
//var shDrawing:Shape=new Shape();
spBoard.addChild(shDrawing);
//spBoard.addChildAt(shDrawing,1);
function nClick0PC(event:MouseEvent):void{
owl2.addEventListener(MouseEvent.CLICK,on_owl_click);
}
var doDraw:Boolean=false;
var lineSize:Number=10;
var currentColor:Number;
spBoard.graphics.lineStyle(1,0x000000);
spBoard.graphics.beginFill(0xFFFFFF);
spBoard.graphics.drawRect(0,0,602,330);
spBoard.graphics.endFill();
spBoard.filters = [ new DropShadowFilter() ];
function boardOut(e:MouseEvent):void {
doDraw=false;
}
function boardDown(e:MouseEvent):void {
doDraw=true;
trace(activeColor);
shDrawing.graphics.lineStyle(lineSize,activeColor);
shDrawing.graphics.endFill();
shDrawing.graphics.moveTo(shDrawing.mouseX,shDrawing.mouseY);
}
function boardUp(e:MouseEvent):void {
doDraw=false;
}
function boardMove(e:MouseEvent):void {
var curX:Number=shDrawing.mouseX;
var curY:Number=shDrawing.mouseY;
if(doDraw && checkCoords(curX,curY)){
shDrawing.graphics.lineTo(curX,curY);
e.updateAfterEvent();
}
}
function checkCoords(a:Number,b:Number):Boolean {
if(a>=605-lineSize/2 || a<=lineSize/2 || b>=311-lineSize/2 || b<=lineSize/2){
return false;
}
else {
return true;
}
}
/////////////---------------------color picker
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
var pixelValue:uint;
var activeColor:uint = 0x000000;
var ct:ColorTransform = new ColorTransform();
var colorsBmd:BitmapData;
function convertToBMD():void
{
colorsBmd = new BitmapData(colors.width,colors.height);
colorsBmd.draw(colors);
}
function chooseColor(e:MouseEvent):void
{
pixelValue = colorsBmd.getPixel(colors.mouseX,colors.mouseY);
activeColor = pixelValue;//uint can be RGB!
ct.color = activeColor;
//shapeSize.transform.colorTransform = ct;
}
////////////////////========================================Fill color
function on_owl_click(e:MouseEvent):void {
for (var i:int = 0; i < owl2.numChildren; i++) {
if (owl2.getChildAt(i).hitTestPoint(mouseX,mouseY,true)) {
trace(owl2.getChildAt(i).name);
owl2.getChildAt(i).transform.colorTransform= ct;
}
}
}
I deleted a lot of your code and left this:
convertToBMD();
colors.addEventListener(MouseEvent.MOUSE_UP, chooseColor);
var activeColor: uint = 0x000000;
var colorsBmd: BitmapData;
function convertToBMD(): void
{
colorsBmd = new BitmapData(colors.width, colors.height);
colorsBmd.draw(colors);
}
function chooseColor(e: MouseEvent): void
{
var pixelValue:uint = colorsBmd.getPixel(colors.mouseX, colors.mouseY);
activeColor = pixelValue; //uint can be RGB!
}
Also I removed an owl at the stage. Download my FLA to see changes.
Next. I added two canvases.
var canvasData:BitmapData = new BitmapData(650, 437, false, 0xEFEFEF);
var canvas:Bitmap = new Bitmap(canvasData);
canvas.x = 0;
canvas.y = 102;
addChild(canvas);
var penCanvas:Shape = new Shape();
penCanvas.x = canvas.x;
penCanvas.y = canvas.y;
You can read about Bitmap and BitmapData here.
First canvas it's a raster image. Second canvas it's a Shape, so you can use moveTo and lineTo methods to draw with a pencil.
Next. In library i found an owl image and export it to code.
If not understand, I can explain more detailed.
Next. Registration event handlers.
bucbut.addEventListener(MouseEvent.CLICK, clickBucket);
pbut.addEventListener(MouseEvent.CLICK, clickPen);
function clickBucket(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_DOWN, canvasDown);
stage.addEventListener(MouseEvent.CLICK, clickOnCanvas);
}
function clickPen(event:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_DOWN, canvasDown);
stage.removeEventListener(MouseEvent.CLICK, clickOnCanvas);
}
Let's see at clickOnCanvas method:
function clickOnCanvas(event:MouseEvent):void
{
// If we click at the canvas
if (canvas.hitTestPoint(mouseX,mouseY))
{
canvasData.floodFill(canvas.mouseX, canvas.mouseY,activeColor);
}
}
About floodFill you can read here.
And the last three methods used to draw by pen.
function canvasDown(event:MouseEvent):void
{
penCanvas.graphics.lineStyle(10, activeColor);
penCanvas.graphics.moveTo(penCanvas.mouseX, penCanvas.mouseY);
// only when mouse button is down we register two handlers, one for move and another for mouse up
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
function mouseMove(event:MouseEvent):void
{
// when mouse is moving we are drawing line
penCanvas.graphics.lineTo(penCanvas.mouseX, penCanvas.mouseY);
// As I said earlier canvasData it's a raster image. So we copy penCanvas and paste to canvasData.
canvasData.draw(penCanvas);
// For a smoother drawing
event.updateAfterEvent();
}
function mouseUp(event:MouseEvent):void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMove);
stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
}
That's all!
Here you can download sources.

Path glow effect in the mouse over event in actionscript 3

I'm building a board game in action script 3 Adobe flash. In that if i mouseover on a particular pawn, it has to show the number of steps which can be moved by that pawn with respect to dice value with path glow effect.
Here in my code path will glow after i move the pawn with respect to dice number.
opawn1.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_3);
function fl_ClickToGoToAndStopAtFrame_3(event: Mouse): void {
var filterarray: Array=new Array();
opawn1.filters=[glow];
var gfilter: GlowFilter=new GlowFilter();
filterarray.push(gfilter);
current_pawn = arrayPawn[0];
checkSize(opawn1);
if (o_move == 0) {
o_move = 1;
convert_to_movieclip(s1);
}
temp = get_number_of_moves(odirectmove, checkorange, 0, current_pawn);
odirectmove = false;
for(var i=0;i<temp+1;i++)
{
s1[i].filters=filterarray;
}
Here I used mouse click event, Its not working if i change it as mouseover.
Please let me know the above code is correct or not.
How to achieve this?
As #otololua said, the type of your fl_ClickToGoToAndStopAtFrame_3 event parameter should be MouseEvent and not Mouse, then you can change MouseEvent.CLICK by MouseEvent.MOUSE_OVER like this :
opawn1.addEventListener(MouseEvent.MOUSE_OVER, opawn1_on_MouseOver);
function opawn1_on_MouseOver(event:MouseEvent): void {
var glow_filter: GlowFilter = new GlowFilter();
var filters_array: Array = [glow_filter];
your_target_object.filters = filters_array
// ...
}
And if you need that effect be visible only when the mouse is over, you can remove it using MouseEvent.MOUSE_OUT like this :
opawn1.addEventListener(MouseEvent.MOUSE_OUT, opawn1_on_MouseOut);
function opawn1_on_MouseOut(event:MouseEvent): void {
your_target_object.filters = null;
// ...
}
Hope that can help you.

Flash Actionscript 3 - Simple Btn Click save game/app

I'm working on a project that requires me to save all of the bits and pieces on the second frame of the stage. This is a glorified dress up game where the user game make design or a piece of art and save the project and come back to it later by clicking the "restore_btn"
This will have multiple 'dragable' bits and pieces on the stage, on the second frame. Could someone give me some insight in how to make it so the app can save on the desktop and when the user opens it up and clicks the 'restore' button their last design loads up on the stage? Thanks for you help. I've had bit of trawl of the net and i can't find any simple tuts for what I need.
Code added, just in case.
p.s please keep it simple as I'm designer. :-)
stop();
Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
function follow(evt:MouseEvent){
tweezer_cur.x = mouseX;
tweezer_cur.y = mouseY;
}
//Resetter btn ---------------------
reset_btn.addEventListener(MouseEvent.CLICK, startover);
function startover(event:MouseEvent):void
{
gotoAndPlay(1);
}
//------------------------------ fullscreen
function setFullScreen():void {
if (stage.displayState== "normal") {
stage.displayState="fullScreen";
stage.scaleMode = StageScaleMode.NO_SCALE;
} else {
stage.displayState="normal";
}
}
fullbtn.addEventListener(MouseEvent.CLICK, goFull);
// btn declared - - - - - - - -
function goFull(event:MouseEvent):void {
setFullScreen();
};
//---------------------------- print project
//--- all the draggables will live here
dragme.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
dragme.addEventListener(MouseEvent.MOUSE_UP, dropObject);
function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
}
//--------
//creating a container as main canvas
var artworkContainers:Sprite = new Sprite();
addChild(artworkContainers);
//example adding content
//var anyContentIWantToPrint:Sprite = new Sprite();
//anyContentIWantToPrint.graphics.beginFill(0, 1);
//anyContentIWantToPrint.graphics.drawRect(0, 0, 1024, 768);
//anyContentIWantToPrint.graphics.endFill();
//artworkContainers.addChild(anyContentIWantToPrint);
printme_btn.addEventListener(MouseEvent.CLICK, startPrintJobHandler, false, 0, true);
function startPrintJobHandler(event:MouseEvent):void
{
var printJob:PrintJob = new PrintJob();
printJob.start()
var printJobOptions:PrintJobOptions = new PrintJobOptions();
printJobOptions.printAsBitmap = true;
//When 'artworkContainer' will be your artwork canvas, where the user will drag and drop. Replace for the instance name you are using.
printJob.addPage(artworkContainers, null, printJobOptions);
printJob.send();
}
// making all of the functions save! --------------------------------
var saveData:SharedObject = SharedObject.getLocal("MyDesign");
if(!saveData.data.test)
saveData.data.test = "Test string";
trace(saveData.data.test); // Test string
This is too long to answer in a comment, so here it goes:
import flash.net.SharedObject;
import flash.geom.Point;
import flash.events.MouseEvent;
var restore_values:SharedObject=SharedObject.getLocal("dress_up_game");
if(!restore_values.data.shirt_point){
restore_values.data.shirt_point=new Point(shirt.x,shirt.y);
restore_values.flush();
}
restore_btn.addEventListener(MouseEvent.CLICK,restore);
function restore(e:MouseEvent){
if(restore_values.data.shirt_point){
shirt.x=restore_values.data.shirt_point.x;
shirt.y=restore_values.data.shirt_point.y;
}
}
shirt.addEventListener(MouseEvent.MOUSE_DOWN,start_shirt_drag);
function start_shirt_drag(e:MouseEvent){
shirt.startDrag()
}
shirt.addEventListener(MouseEvent.MOUSE_UP,stop_shirt_drag);
function stop_shirt_drag(e:MouseEvent){
shirt.stopDrag()
restore_values.data.shirt_point=new Point(shirt.x,shirt.y);
restore_values.flush();
}
It took me a few minutes to scratch this out. Once again, the button's instance name is "restore_btn". The garment or "draggable bit" goes by the instance name "shirt". The code places the shirt at the last saved position if you click the restore button. You should be able to adapt this code to your project's situation on your own.
Cheers,
Drake Swartzy
Yeah, Atriace has the right idea. Check this out:
http://www.republicofcode.com/tutorials/flash/as3sharedobject/
Cheers,
Drake Swartzy

Movieclips clashing with bitmap mask

I am trying to reveal this movie clip image which is originally a bitmap but needs to be used as a bitmap for this purpose. for some reason it's not working ...
It's not throwing any errors... I need this image to be masked as the user presses on it... and later be compared with another bitmap to carry out a function. but for some reason as I mentioned before it's not working out. can somebody please help me?? this is the code for it...
import flash.display.Graphics;
import flash.display.MovieClip;
import flash.display.BitmapData;
var mouseclick:Number=0;
var maskedbg_mc:maskedbg = new maskedbg ();
var masking:Sprite = new Sprite()
addChild (maskedbg_mc);
maskedbg_mc.x = 18;
maskedbg_mc.y = 343;
var bitmapDataCopy:BitmapData = new BitmapData(742,165,true,0x00FFFFFF);
var b:Bitmap = new Bitmap(bitmapDataCopy);
bitmapDataCopy.draw(maskedbg_mc);
b.mask = masking;
var Testing:BitmapData = new BitmapData(maskedbg_mc.width, maskedbg_mc.height, true, 0x00000000);
addChild(masking);
stage.addEventListener(MouseEvent.MOUSE_DOWN, Pressing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, Moving);
stage.addEventListener(MouseEvent.MOUSE_UP, Lifting);
function Pressing(event:MouseEvent):void {
mouseclick = 1;
}
function Moving(event:MouseEvent):void {
if (mouseclick == 1) {
masking.graphics.beginFill(0x000000);
masking.graphics.drawEllipse(mouseX, mouseY, 70, 60);
masking.graphics.endFill();
}
}
function Lifting(event:MouseEvent):void {
mouseclick = 0;
}
if ( bitmapDataCopy.compare(Testing) ==0 )
{
trace ("Awesomness")
}
Overlooking your code, I notice you are not adding "b" (the masked DisplayObject) to the display list, while you are adding "maskedbg_mc" which actually isn't being masked in your code. Do you have a reason for having these 2 display objects?
I would recommend you following actionscript coding conventions:
http://sourceforge.net/adobe/flexsdk/wiki/Coding%20Conventions/
Your code looks quite confusing when you have both variables and functions with initial letter in uppercase, they look like classes.

On Mouse Over change Sprite Graphics Color: AS3

I have created a sprite as below:
var arrowHeadRight:Sprite = new Sprite();
with(arrowHeadRight.graphics){
beginFill(0xDDDDDD, 1);
moveTo(50,0);
lineTo(0,50);
lineTo(50,100);
lineTo(50,0);
endFill();
}
On Mouse Over, I wish to change the color of the fill on this shape?
Can this be done or do I have to re-draw the graphics with an updated beginFill line?
I figured it out.
I can use the ColorTransform class:
var newCol:ColorTransform = new ColorTransform();
function nextOver(e:MouseEvent):void {
newCol.color=0x666666;
btnNext.transform.colorTransform = newCol;
}