AS3 - Change MovieClip-Color Using Filters - actionscript-3

I wonder how I can create an "Adjust Color"-filter in AS3.
I created a red box in Photoshop (8x8px), saved it as a PNG-file, then I imported it to Flash. I converted the file to a MovieClip-symbol. I applied an "Adjust Color"-filter. With that I can change the color easily, but I want to get the same effect in AS3.
Does anyone have a clue on how?
Thanks:)

To change MovieClip color use ColorTransform class and transform.colortransform property of the MovieClip:
var ct:ColorTransform = new ColorTransform();
ct.color = 0xCFFF54;
yourMovieClip.transform.colorTransform = ct;
Advanced method
Using this method you can change brightness, contrast, etc.
import fl.motion.AdjustColor;
var adjustColor:AdjustColor = new AdjustColor();
adjustColor.brightness = 50;
adjustColor.contrast = 50;
adjustColor.saturation = 50;
adjustColor.hue = 50;
var matrix:Array = adjustColor.CalculateFinalFlatArray();
var colorMatrix:ColorMatrixFilter = new ColorMatrixFilter(matrix);
yourMovieClip.filters = [colorMatrix];

Related

Creating a color preview in AS3 for a drawing application

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);
}

How to import picture in flash with transaprent background

How do I make transparent background to imported image in flash, because now i imported it and image has white box around it.
code for adding images to stage
var imageBD = (Math.floor(Math.random()*2))? new Trees() : new Rocks;
var bitmap:Bitmap = new Bitmap();
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height);
BD.draw(imageBD);
bitmap.bitmapData = BD;
bitmap.width = mRadius * 2 * mToPx;
bitmap.height = mRadius * 2 * mToPx;
bitmap.x = pxStartX;
bitmap.y = pxStartY;
this.addChild(bitmap);
obstacleImages.push(bitmap)
Since i'm mew i cannot post images so i'm giving you a link to image:http://prntscr.com/pugdl
You need to specifically tell the BitmapData (docs) object to be transparent.
In your case, replace this line:
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height);
...with this:
var BD:BitmapData = new BitmapData(imageBD.width, imageBD.height, true, 0x00000000);
Make sure the image itself has transparency. General file types for these kinds of images are PNG or GIF.
You shouldn't have to do anything for Flash to find the transparency.

How do I randomly set the color for a MovieClip in actionscript3

I want to randomly set the color for a MovieClip in ActionScript 3. How would I go about doing so?
Example with some colors in a array:
var colorArray:Array = new Array(0xFFFF33, 0xFFFFFF, 0x79DCF4, 0xFF3333, 0xFFCC33, 0x99CC33);
var randomColorID:Number = Math.floor(Math.random()*colorArray.length);
var myColor:ColorTransform = this.transform.colorTransform;
myColor.color=colorArray[randomColorID];
myMovieClip.transform.colorTransform = myColor;
Change the myMovieClip to the instance name oof your movie clip, if you want you could add a click event to change the random colors.
I know your post is old, but i just figured this out.
You don't need to create your own color array, you can steal it from the colorpicker.
import fl.controls.*;
var _lineWeight:Number = 20;
var cp:ColorPicker = new ColorPicker();
var randomNumber:Number = Math.random() * cp.colors.length;
var drawing:Shape = new Shape();
drawing.graphics.lineStyle(_lineWeight,cp.colors[randomNumber]);

Rendering text in AS3

I'm having a bit of confusion about how to render text in a pure AS3 project. There are classes like flash.text.StaticText but these are designer-only, you can't create them in code. I was half-expecting the Graphics class to have text-rendering options but alas, no.
Specifically I was going to add a label above each player's sprite with their name, health %, etc. So I expected to add a child text-element or draw text using Graphics in some way... it's read-only and should not support user-input, I just want to draw text on-screen.
You can use TextField class for this. Please check the reference. All fields and methods are self explanatory.
A possible example.
var myField:TextField = new TextField();
myField.text = "my text";
myField.x = 200;
myField.y = 200;
addChild(myField); // assuming you are in a container class
If TextField doesn't work, you can create text using this method:
var format:ElementFormat = new ElementFormat();
format.fontSize = 26;
format.color = 0x0000FF;
var textElement:TextElement = new TextElement('Hello World!', format);
var textBlock:TextBlock = new TextBlock();
textBlock.content = textElement;
var textLine:TextLine = textBlock.createTextLine(null, 500);
textLine.x = (stage.stageWidtht - textLine.width) / 2;
textLine.y = (stage.stageHeight - textLine.height) / 2;
addChild(textLine);
Look at:
Creating and displaying text in ActionScript 3.0 Developer’s Guide

Set text outlining / border in Actionscript 3.0

How can I set the properties for the text outline / border for each character in a line of text in AS3 ?
I don't think you can. What you can do is use a blur filter to mimic the appearance of an outline. Just paste this into an empty AS3 movie:
var txt:TextField = new TextField();
this.addChild(txt);
txt.appendText('Lorem ipsum');
txt.autoSize = TextFieldAutoSize.LEFT;
txt.antiAliasType = flash.text.AntiAliasType.NORMAL;
txt.selectable = false;
var txtFormat:TextFormat = new TextFormat();
txtFormat.size = 40;
txtFormat.font = 'Helvetica';
txt.setTextFormat(txtFormat);
txt.defaultTextFormat = txtFormat;
var outline:GlowFilter = new GlowFilter();
outline.blurX = outline.blurY = 1;
outline.color = 0xFF0000;
outline.quality = BitmapFilterQuality.HIGH;
outline.strength = 100;
var filterArray:Array = new Array();
filterArray.push(outline);
txt.filters = filterArray;
Try playing with the strength, blurX, blurY and quality properties, in order to obtain different appearances. I think that's about the closest you can get to a text outline.
PS: font embedding would greatly improve the quality of the effect, as well as making the antialias work properly.
i am not shore i understand but you can use same kind of
filter on the testbox and by doing so you can get a same kind of a border
in each one of your letters