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).
Posted in color | Also tagged color, flash, tint |
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.
Posted in motion | Also tagged flash, mouse, velocity |
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.
Posted in arrays, motion | Also tagged flash |
By Zevan | November 30, 1999
Actionscript:
-
// create constants for all letter and number keys:
-
var alphabet:Array = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
-
var nums:Array = ["ZERO","ONE","TWO","THREE","FOUR","FIVE","SIX","SEVEN","EIGHT","NINE"];
-
-
var key:Object = new Object();
-
for (var i:int = 0; i<alphabet.length; i++)
-
key[alphabet[i]] = 65 + i;
-
-
for (i = 0; i<nums.length; i++){
-
var code:int = 48 + i;
-
key[nums[i]]= code;
-
key[i] = code;
-
}
-
-
-
// test them out
-
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
-
function onKeyPressed(evt:KeyboardEvent):void {
-
if (evt.keyCode == key.A){
-
trace("the a was pressed");
-
}
-
if (evt.keyCode == key.B){
-
trace("the b was pressed");
-
}
-
if (evt.keyCode == key.NINE){
-
trace("the 9 key was pressed");
-
}
-
if (evt.keyCode == key["0"]){
-
trace("the 0 key was pressed");
-
}
-
}
This is an easy way to store values for alphanumeric keys in variables that make sense... instead of having to do things like this:
Actionscript:
-
// hit the zero key
-
if (evt.keyCode == 48){
-
trace("the zero key was hit");
-
}
Posted in Object, keys | Also tagged flash, keys |