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

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

Related

Clearing content from a movieclip in a blank keyframe in AS3

var loader:URLLoader;
var allWords:Array;
var thisWord:String;
var textContainer:MovieClip;
var textFields:Array;
var textStyle:TextFormat;
var underline:MovieClip;
var numCorrect:uint;
var totalLetters:uint;
var misses:uint;
var missesToLose:uint;
function initializeGame():void
{
loader = new URLLoader();
allWords = new Array();
textContainer = new MovieClip();
textFields = new Array();
textStyle = new TextFormat();
textStyle.font = "Courier New";
textStyle.size = 48;
textStyle.bold = true;
Hi All,
I have created this game and only added the beginning portion of the code. The entire code would be tedious to go through. I want to click a button and go to a clear keyframe, however when I test the movie the underline movieclip still shows. How can I clear the frame completely, do I have to removeChild for the movieclip? How do I do that? Is there a way to completely unload the SWF for this game after it is loaded? If so what code can I use?
Thanks

AS3 - Change MovieClip-Color Using Filters

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];

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.

Actionscripts 3 Clone MovieClip

How do we clone a copy of an "Instance name"? Thanks guys
//The test_close is an instance name which I drew on the canvas.
var cloneMe:MovieClip = new MovieClip();
cloneMe.graphics.copyFrom(test_clone.graphics); //here is my clone codes
addChild(cloneMe);
trace(cloneMe.getBounds(cloneMe));
If you simply need a copy of the object that has been drawn, use the BitmapData draw method.
var bmd:BitmapData = new BitmapData(cloneMe.width , cloneMe.height );
bmd.draw( cloneMe);
var bm:Bitmap = new Bitmap(bmd);
// not relevant, simply shifting the new Bitmap so that it is visible
bm.x = 10;
bm.y = 10;
addChild( bm );

AS3 clone MovieClip

The below is my code for trying to clone the MovieClip and it doesn't work.
We should see two cirles if the codes is working correctly.
/*The original MovieClip*/
var circle:MovieClip = new MovieClip();
circle.graphics.beginFill(0xAA0022);
circle.graphics.drawCircle(40, 40, 40);
circle.x=10
addChild(circle);
/*CLONE the MovieClip - IT DOES'T WORK FROM HERE DOWN*/
var cloneCirle:MovieClip = new MovieClip();
cloneCirle=circle
cloneCirle.x=60
addChild(cloneCirle);
When you do cloneCircle=circle, it's not copying or cloning anything. It's just saying that the variable cloneCircle is another name for your original circle MovieClip. What you need to do is use the Graphics.copyFrom() method.
Try it:
var cloneCircle:MovieClip = new MovieClip();
cloneCircle.graphics.copyFrom(circle.graphics);
cloneCircle.x = 60;
addChild(cloneCircle);
This is for creating a duplicate of a stage object that exists in the FLA library at compile time
The object must have a 'Export for Actionscript ticked in it's properties panel and a valid class name in the 'Class' box
If the symbol only has a single frame just add another so it registers as MovieClip() rather than Sprite()
private function cloneObject(source:DisplayObject):void
{
var objectClass:Class = Object(source).constructor;
var instance:MovieClip = new objectClass() as MovieClip;
instance.transform = source.transform;
instance.filters = source.filters;
instance.cacheAsBitmap = source.cacheAsBitmap;
instance.opaqueBackground = source.opaqueBackground;
source.parent.addChild(instance);
instance.x += 20; // just to show the duplicate exists!
}
http://snipplr.com/view/44734/
Adapted from here:
function copyClip( clip:MovieClip )
{
var sourceClass:Class = Object(clip).constructor;
var duplicate:MovieClip = new sourceClass();
return duplicate;
}