how to draw a circle using ActionScript - actionscript-3

how to draw a circle using action script (as a component) i tried some xample did not work....i need to add this circle in a panel

Create a class derived from UIComponent
Override the updateDisplayList() method inside your component and draw the circle
Add an instance of your component in the panel;
Component class:
class MyCircle extends UIComponent
{
public function MyCircle()
{
super();
}
override protected function updateDisplayList(width:Number, height:Number):void
{
super.updateDisplaylist(width,height);
this.graphics.clear();
this.graphics.beginFill(0xff0000);
this.graphics.drawCircle(width/2, height/2, Math.min(width/2,height/2));
}
}
Panel component:
<mx:Panel width = "400" height
= "400">
<local:MyCircle
width = "100%"
height = "100%"/>
</mx:Panel>

// Draw a simple circle, gray, with a radius of 24 px
var circleColor:uint = 0xCCCCCC;
var radius:uint = 24;
var circle:Shape = new Shape();
circle.graphics.beginFill(circleColor);
circle.graphics.drawCircle(radius, radius, radius);
circle.graphics.endFill();
addChild(circle);
You can substitute beginLine and endLine instead of beginFill and endFill if you just want the circle's outer edge.

Related

Show border color in AdvancedDataGrid selected row in Flex

How can I show the borderColor of selected row in AdvancedDataGrid in Flex 4.6. You can see the following image for your reference.
Selected row is in the AdvancedDataGrid realized through dedicated Sprite, which is used as a canvas for drawing. To create border in the selection marker you need to create a custom class based on the AdvancedDataGrid and override protected method drawSelectionIndicator.
The following example should produce a selection marker with 1px wide red border:
public class GridWithBorderedSelectionMarker extends AdvancedDataGrid {
override protected function drawSelectionIndicator(
indicator:Sprite, x:Number, y:Number, width:Number,
height:Number, color:uint,
itemRenderer:IListItemRenderer):void {
if (isRowSelectionMode()) {
width = unscaledWidth - viewMetrics.left - viewMetrics.right;
}
var borderColor:uint = 0xff0000;
var g:Graphics = Sprite(indicator).graphics;
g.clear();
g.beginFill(borderColor);
g.drawRect(0, 0, width, height);
g.beginFill(color);
g.drawRect(1, 1, width - 2, height - 2);
g.endFill();
indicator.x = x;
indicator.y = y;
}
}

How do I extract the width and height of an embedded image in AS3?

How do I extract the width and height of an embedded image in AS3 instead of explicitly stating the dimensions?
Here's what I'm trying to do:
[Embed(source="../../lib/spaceship.png")]
private var ShipImage:Class;
private var ship_image:BitmapData;
public function Ship(x:int, y:int, width:Number, height:Number)
{
super(x, y, 36, 64);
ship_image = new ShipImage().bitmapData;
speed = new Point(0, 0);
}
Since super should be called before everything else within the constructor how do I learn about the dimensions beforehand? I'm using FlashDevelop as my IDE.
You can read those properties through BitmapData#rect:
public function Ship(x:int, y:int, width:Number, height:Number)
{
// Would be better if you haven't to pass width and height
super(x, y, 0, 0);
// Get the bitmap data
ship_image = new ShipImage().bitmapData;
// Set width and height
width = ship_image.rect.width;
height = ship_image.rect.height;
// ...
}
Other solution with statics:
[Embed(source="../../lib/spaceship.png")]
private static const ShipImage:Class;
private static var spriteWidth:int;
private static var spriteHeight:int;
private static function calculateSpriteSize():void
{
// Get the sprite "rectangle"
var rect:Rectangle = new ShipImage().bitmapData.rect;
// Set width and height
spriteWidth = ship_image.rect.width;
spriteHeight = ship_image.rect.height;
}
// Call the method into the class body
// (yes you can do that!)
calculateSpriteSize();
public function Ship(x:int, y:int, width:Number, height:Number)
{
// Retrieve the sprite size
super(x, y, spriteWidth, spriteHeight);
// ...
}
you can extract/resize Image With Scaling. you can re-size image with same aspect ratio or different aspect ratio. if you do not want to re-size then scaleX && scaleY has value 1.0 , if you want to re-size with half of the original size then both factor has value of 0.5 If you want to rotate image Then use Translation.
var matrix:Matrix = new Matrix();
matrix.scale(scalex, scaley);
matrix.translate(translatex, translatey);
var resizableImage:BitmapData = new BitmapData(size, size, true);
resizableImage.draw(data, matrix, null, null, null, true);
this resizableimage returns bitmapdata.
May This Works for You!

AS3 draws bigger Sprite than i have set (padding/margin ?)

i created my own Button which simply extends from Sprite.
Its able to show 3 different text fading in and out using a timer.
In my draw function i first draw a rectangle with the Sprites graphics object representing the background of the button.
I added a function to set the button width. This property is used to draw the rectangle. I know that the sprites's size is updated after the rectangle drawing. But for some reason the sprite's width is always more than what i have set via my "setButtonWidth" function.
Now i have a simple sprite acting as a button having a graphics.drawRectangle part drawing a simple rect. with lets say 500px width. But when i trace the width of that button it is always about 10% more. Where are these 10% coming from?
I read about calling validateNow(). But this is only for Labels, or Checkboxes. For some reason i cannot access the library for Label. This must work somehow with TextFields. But how?
// this function is part of MyButtonClass.as file
function drawButton()
{
this.graphics.clear();
this.graphics.beginFill( currentBackColor);
this.graphics.drawRoundRect(0, 0, int(this.buttonWidth)+20, 30, 10, 10);
this.graphics.endFill();
}
// this code is in main action code frame
// the buttonWidth is set this way
stage.addEventListener( Event.RESIZE, updateBtn);
function updateBtn( event:Event)
{
// access the container in which the buttons are in like ...
for( var i = 0; i < buttonContainer.numChildren; i++) {
var btn = buttonContainer.getChildAt( i) as MyButtonClass;
// MyButtonClass is the class which extends from Sprite drawing my button
// and using buttonWidth to draw the backgrounds width.
btn.setButtonWidth( (stage.stageWidth / 2) - 20);
// i set half of stageWidth because i have two columns with a list of buttons
// after setButtonWidth is called in MyButtonClass, the draw function is called
// which does the graphics stuff above.
}
}
this.buttonWidth is set to 500. There is nothing special done on that sprite. No children to be added, only this drawing stuff. But this sets the sprite's width to 550 or something.
Define Button like this
package {
import flash.display.Sprite;
public class Button extends Sprite {
private var _buttonWith: int = 0;
private var _buttonHeight: int = 0;
public function Button() {
// constructor code
}
public function set buttonWidth(w: int): void {
_buttonWith = w;
updateButton();
}
public function get buttonWidth(): int {
return _buttonWith;
}
public function set buttonHeight(h: int): void {
_buttonHeight = h;
updateButton();
}
public function get buttonHeight(): int {
return _buttonHeight;
}
protected function updateButton() {
graphics.clear();
graphics.beginFill(0xff00ff);
graphics.drawRoundRect(0, 0, buttonWidth, buttonHeight, 10, 10);
graphics.endFill();
}
}
}
And create instance like this
var button:Button = new Button();
addChild(button);
button.buttonWidth = 100;
button.buttonHeight = 30;

how can I change the color of one shape by clicking on clicking another object of sprite in actionscript

I have drawn intersecting lines. The user can click on a region inside the angle formed by the two lines.When the user clicks inside the area, the small region formed by the arc between the two lines showing the angle should change. How can I do that.the region between the intersecting lines is sprite object to dispatch event listener, but the arc is shape object.
public class changeColor extends Sprite {
private var mySpr:Sprite;
public function changeColor() {
super();
mySpr = new Sprite();
mySpr.graphics.beginFill(0xFF0000, 1);
mySpr.graphics.drawRect(0, 0, 100, 100);
mySpr.graphics.endFill();
mySpr.addEventListener(MouseEvent.CLICK, action);
addChild(mySpr);
}
public function changeSprColor(inputColor:uint):void {
var myCt:ColorTransform = new ColorTransform();
myCt.color = inputColor;
mySpr.transform.colorTransform = myCt;
}
private function action(e:MouseEvent):void {
changeSprColor(0x00FF00);
}
}

actionscript3: reflect-class applied on rotationY

I'm using a class which applies a visual reflection-effect to defined movieclips.
I use a reflection-class from here:
link to source.
It works like a charm except when I apply a rotation to the movieclip.
In my case the reflection is still visible but only a part of it.
What am I doing wrong? How could I pass/include the rotation to the Reflection-Class ?
Thanks in advance!
This is how you apply the Reflection Class to your movieclip:
var ref_mc:MovieClip = new MoviClip();
addChild(ref_mc);
var r1:Reflect = new Reflect({mc:ref_mc, alpha:50, ratio:50,distance:0, updateTime:0,reflectionDropoff:1});
Now I apply a rotation to my movieclip:
ref_mc.rotationY = 30;
And Here the Reflect-Class:
package com.pixelfumes.reflect{
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Matrix;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class Reflect extends MovieClip{
//Created By Ben Pritchard of Pixelfumes 2007
//Thanks to Mim, Jasper, Jason Merrill and all the others who
//have contributed to the improvement of this class
//static var for the version of this class
private static var VERSION:String = "4.0";
//reference to the movie clip we are reflecting
private var mc:MovieClip;
//the BitmapData object that will hold a visual copy of the mc
private var mcBMP:BitmapData;
//the BitmapData object that will hold the reflected image
private var reflectionBMP:Bitmap;
//the clip that will act as out gradient mask
private var gradientMask_mc:MovieClip;
//how often the reflection should update (if it is video or animated)
private var updateInt:Number;
//the size the reflection is allowed to reflect within
private var bounds:Object;
//the distance the reflection is vertically from the mc
private var distance:Number = 0;
function Reflect(args:Object){
/*the args object passes in the following variables
/we set the values of our internal vars to math the args*/
//the clip being reflected
mc = args.mc;
//the alpha level of the reflection clip
var alpha:Number = args.alpha/100;
//the ratio opaque color used in the gradient mask
var ratio:Number = args.ratio;
//update time interval
var updateTime:Number = args.updateTime;
//the distance at which the reflection visually drops off at
var reflectionDropoff:Number = args.reflectionDropoff;
//the distance the reflection starts from the bottom of the mc
var distance:Number = args.distance;
//store width and height of the clip
var mcHeight = mc.height;
var mcWidth = mc.width;
//store the bounds of the reflection
bounds = new Object();
bounds.width = mcWidth;
bounds.height = mcHeight;
//create the BitmapData that will hold a snapshot of the movie clip
mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF);
mcBMP.draw(mc);
//create the BitmapData the will hold the reflection
reflectionBMP = new Bitmap(mcBMP);
//flip the reflection upside down
reflectionBMP.scaleY = -1;
//move the reflection to the bottom of the movie clip
reflectionBMP.y = (bounds.height*2) + distance;
//add the reflection to the movie clip's Display Stack
var reflectionBMPRef:DisplayObject = mc.addChild(reflectionBMP);
reflectionBMPRef.name = "reflectionBMP";
//add a blank movie clip to hold our gradient mask
var gradientMaskRef:DisplayObject = mc.addChild(new MovieClip());
gradientMaskRef.name = "gradientMask_mc";
//get a reference to the movie clip - cast the DisplayObject that is returned as a MovieClip
gradientMask_mc = mc.getChildByName("gradientMask_mc") as MovieClip;
//set the values for the gradient fill
var fillType:String = GradientType.LINEAR;
var colors:Array = [0xFFFFFF, 0xFFFFFF];
var alphas:Array = [alpha, 0];
var ratios:Array = [0, ratio];
var spreadMethod:String = SpreadMethod.PAD;
//create the Matrix and create the gradient box
var matr:Matrix = new Matrix();
//set the height of the Matrix used for the gradient mask
var matrixHeight:Number;
if (reflectionDropoff<=0) {
matrixHeight = bounds.height;
} else {
matrixHeight = bounds.height/reflectionDropoff;
}
matr.createGradientBox(bounds.width, matrixHeight, (90/180)*Math.PI, 0, 0);
//create the gradient fill
gradientMask_mc.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);
gradientMask_mc.graphics.drawRect(0,0,bounds.width,bounds.height);
//position the mask over the reflection clip
gradientMask_mc.y = mc.getChildByName("reflectionBMP").y - mc.getChildByName("reflectionBMP").height;
//cache clip as a bitmap so that the gradient mask will function
gradientMask_mc.cacheAsBitmap = true;
mc.getChildByName("reflectionBMP").cacheAsBitmap = true;
//set the mask for the reflection as the gradient mask
mc.getChildByName("reflectionBMP").mask = gradientMask_mc;
//if we are updating the reflection for a video or animation do so here
if(updateTime > -1){
updateInt = setInterval(update, updateTime, mc);
}
}
public function setBounds(w:Number,h:Number):void{
//allows the user to set the area that the reflection is allowed
//this is useful for clips that move within themselves
bounds.width = w;
bounds.height = h;
gradientMask_mc.width = bounds.width;
redrawBMP(mc);
}
public function redrawBMP(mc:MovieClip):void {
// redraws the bitmap reflection - Mim Gamiet [2006]
mcBMP.dispose();
mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF);
mcBMP.draw(mc);
}
private function update(mc):void {
//updates the reflection to visually match the movie clip
mcBMP = new BitmapData(bounds.width, bounds.height, true, 0xFFFFFF);
mcBMP.draw(mc);
reflectionBMP.bitmapData = mcBMP;
}
public function destroy():void{
//provides a method to remove the reflection
mc.removeChild(mc.getChildByName("reflectionBMP"));
reflectionBMP = null;
mcBMP.dispose();
clearInterval(updateInt);
mc.removeChild(mc.getChildByName("gradientMask_mc"));
}
}
}
If you have set the height and the width of your bitmapData to the same width and height of the movieclip your reflecting, you will get 'cut off'
When you rotate your movieclip, it does effectively become wider... and taller - of course at this point you've already made the bitmapData canvas area at a set width -
What you need to do it collect the real width and height of the rotating movieclip and when you redraw the bitmapData, use the new values.
I cheated when I made the same thing and just made my bitmapData 1.5 (bounds.width * 1.5) times the size of the movieclip. But thats a hack and I' a bad person for doing it.
I got it, I have to reset the bounds of the reflect Class:
myReflectClass.setBounds(newWidth,newHeight);