By Zevan | November 18, 2008
Actionscript:
-
var box:Shape = Shape(addChild(new Shape()));
-
with (box.graphics) beginFill(0x006666), drawRect(0,0,50,50);
-
box.x = box.y = 100;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
-
var m:Matrix = box.transform.matrix;
-
// skew on the X
-
m.c = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth;
-
-
// skew on the Y
-
// m.b = (mouseX - stage.stageWidth / 2 ) / stage.stageWidth
-
-
box.transform.matrix = m
-
}
This skews a box Shape using the c and b properties of the transformation matrix. Note that these values don't match those in the IDE's transform window. This is good further reading if your interested in this topic.
By Zevan | November 17, 2008
Actionscript:
-
// display object, red (0-255), green, blue, amount (0-1)
-
function tint(dsp:DisplayObject, r:Number, g:Number, b:Number, amount:Number=1):void {
-
if (amount != 1) {
-
r *= amount;
-
g *= amount;
-
b *= amount;
-
}
-
amount = 1-amount;
-
var ct:ColorTransform = transform.colorTransform;
-
ct.redOffset = r;
-
ct.redMultiplier = amount;
-
ct.greenOffset = g;
-
ct.greenMultiplier = amount;
-
ct.blueOffset = b;
-
ct.blueMultiplier = amount;
-
dsp.transform.colorTransform = ct;
-
}
-
-
// test out the tint function on a circle with a stroke:
-
var circle:Shape = Shape(addChild(new Shape()));
-
with (circle.graphics) {
-
lineStyle(5, 0x339999);
-
beginFill(0x003333);
-
drawCircle(0,0,50);
-
x = 100;
-
y = 100;
-
}
-
-
// tint the circle 50% green
-
tint(circle, 0, 255, 0, .5);
The ColorTransform object is a little confusing with its "redMultiplier, redOffset, greenMultiplier etc..." properties. Once you understand them it's not a big deal, but I still find them a bit cumbersome. So when I just want to tint a clip similar to the way you might tint a clip in the IDE... I use this tint function. Rather than taking a hexidecimal number it takes values for red, green and blue (0-255) - and an amount argument (0-1).
By Zevan | November 16, 2008
Actionscript:
-
var velocityInfo:TextField = new TextField();
-
velocityInfo.x = 20;
-
velocityInfo.y = 20;
-
addChild(velocityInfo);
-
-
var prevX:Number = mouseX;
-
var prevY:Number = mouseY;
-
var velX:Number = 0;
-
var velY:Number = 0;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
-
velX = mouseX - prevX;
-
velY = mouseY - prevY;
-
-
velocityInfo.text = velX + ", " + velY
-
-
prevX = mouseX;
-
prevY = mouseY;
-
}
Wrote this in response to a question from one of my students. The next step is to use velX and velY to push objects around the screen.
By Zevan | November 15, 2008
Actionscript:
-
stage.frameRate = 30;
-
var pencil:Shape = new Shape();
-
addChild(pencil);
-
-
var index:int = 0;
-
var points:Array = new Array();
-
var circle:Shape = new Shape();
-
circle.graphics.beginFill(0x000000);
-
circle.graphics.drawCircle(0,0, 5);
-
addChild(circle);
-
-
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
-
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
-
-
circle.addEventListener(Event.ENTER_FRAME, onMoveCircle);
-
-
function onDown(evt:MouseEvent):void {
-
pencil.graphics.lineStyle(0,0x000000);
-
pencil.graphics.moveTo(mouseX, mouseY);
-
stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
-
}
-
-
function onUp(evt:MouseEvent):void {
-
stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw);
-
}
-
-
function onDraw(evt:Event):void {
-
pencil.graphics.lineTo(mouseX, mouseY);
-
points.push(new Point(mouseX, mouseY));
-
}
-
-
function onMoveCircle(evt:Event):void {
-
if (points.length>0) {
-
circle.x += (points[index].x - circle.x) / 4;
-
circle.y += (points[index].y - circle.y) / 4;
-
index++;
-
if (index==points.length) {
-
index=0;
-
}
-
}
-
}
Wrote this in response to a student question. It causes a circle shape to animate along lines drawn by the user.