By Zevan | November 22, 2008
Actionscript:
-
MovieClip.prototype.tint = function(col:uint):void {
-
var ct:ColorTransform = transform.colorTransform;
-
ct.color = col;
-
this.transform.colorTransform = ct;
-
}
-
-
var circle:MovieClip = MovieClip(addChild(new MovieClip()));
-
with (circle.graphics) {
-
beginFill(0x123455);
-
drawCircle(0,0,50);
-
x = 100;
-
y = 100;
-
}
-
-
circle.tint(0xFF0000);
When AS3 first came out I didn't realize that prototype was still around.... This adds a function called tint() to all MovieClips. You should extend MovieClip instead of using this method.... but it's interesting to see that it's still around. There's an explanation of prototype and the AS3 namespace here.
By Zevan | November 21, 2008
Actionscript:
-
stage.frameRate = 30;
-
-
var imageNum:int = 10000;
-
var point:Point = new Point(0,0);
-
var s:Sprite = new Sprite();
-
s.graphics.beginFill(0xCCCCCC);
-
s.graphics.lineStyle(0,0x000000);
-
s.graphics.drawCircle(3,3,3);
-
s.alpha = .1;
-
-
var nested:Sprite = new Sprite();
-
nested.addChild(s);
-
var image:BitmapData = new BitmapData(s.width, s.height, true, 0x00000000);
-
image.draw(nested);
-
-
var canvas:BitmapData = new BitmapData(400,400, true, 0xFFFFFFFF);
-
addChild(new Bitmap(canvas));
-
-
var xPos:Array = new Array();
-
var yPos:Array = new Array();
-
for (var i:int = 0; i<imageNum; i++) {
-
xPos.push(Math.random()*400);
-
yPos.push(Math.random()*400);
-
}
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
canvas.fillRect(new Rectangle(0,0,400,400), 0xFFFFFFFF);
-
var div:Number;
-
for (var i:int = 0; i<imageNum; i++) {
-
div = (i / 100)+2;
-
xPos[i] += (mouseX - xPos[i])/div;
-
yPos[i] += (mouseY - yPos[i])/div;
-
point.x = xPos[i];
-
point.y = yPos[i];
-
canvas.copyPixels(image, image.rect, point, null, null, true);
-
}
-
}
This is from my other blog but I figured it was worth posting here. It draws 10'000 transparent circles that each follow the mouse at different speeds. This is achieved using copyPixels().
By Zevan | November 20, 2008
Actionscript:
-
var image:Loader = new Loader();
-
image.load(new URLRequest("http://www.shapevent.com/sketchbook/wp-content/uploads/avalanche.jpg"));
-
image.x = stage.stageWidth / 2;
-
image.y = stage.stageHeight / 2;
-
addChild(image);
-
-
image.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
-
function onComplete(evt:Event):void {
-
-
// CENTER THE IMAGE
-
image.content.x = -image.content.width / 2;
-
image.content.y = -image.content.height / 2;
-
-
// BITMAP SMOOTHING
-
Bitmap(image.content).smoothing = true;
-
image.contentLoaderInfo.removeEventListener(Event.COMPLETE, onComplete);
-
}
-
-
-
// scale the image with the mouse to demonstrate smoothing and scaling from the center
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
image.scaleX = image.scaleY = mouseX / stage.stageWidth;
-
}
This is really two snippets combined. How to scale the content of a Loader from the center:
Actionscript:
-
// CENTER THE IMAGE
-
image.content.x = -image.content.width / 2;
-
image.content.y = -image.content.height / 2;
and how to smooth a jpg, png etc... that has been loaded dynamically into your swf:
Actionscript:
-
// BITMAP SMOOTHING
-
Bitmap(image.content).smoothing = true;
By Zevan | November 19, 2008
Actionscript:
-
// display object target, value - "center" or "upperLeft"
-
function setRegistration(dsp:DisplayObjectContainer, v:String):void {
-
var i:int;
-
var child:DisplayObject;
-
var b:Rectangle=getBounds(dsp);
-
for (i = 0; i <dsp.numChildren; i++) {
-
child=dsp.getChildAt(i);
-
child.x+=b.x*-1;
-
child.y+=b.y*-1;
-
}
-
if (v=="center") {
-
dsp.x+=dsp.width/2;
-
dsp.y+=dsp.height/2;
-
for (i = 0; i <dsp.numChildren; i++) {
-
child=dsp.getChildAt(i);
-
child.x-=dsp.width/2;
-
child.y-=dsp.height/2;
-
}
-
} else if (v == "upperLeft") {
-
if (dsp.parent) {
-
b=getBounds(dsp.parent);
-
dsp.x=b.left;
-
dsp.y=b.top;
-
}
-
}
-
}
-
-
// example:
-
// (make a MovieClip and fill it with, text, shapes etc...)
-
-
setRegistration(clip, "center");
-
// registration(clip, "upperLeft");
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
clip.rotation+=1;
-
}
Teaching beginner flash... I've noticed people always get confused about the registration point. They think they should be able to move it with the IDE or ActionScript. I always say "You can't change the registration point, you can only change the position of graphical elements in relationship to it." Anyway, as a sort of joke one day after class I wrote the above function... it changes the registration of any DisplayObjectContainer to either "center" or "upperLeft".
To test this code out, fill a MovieClip with a bunch of different things (text, graphics, scribbles)... give it a strange registration point and then run the above code on it. You'll be able to tell that the registration has changed by the way the clip rotates. Although I haven't been able to break this function, I think it's possible that it doesn't work under all circumstances.
Using reparenting is a more elegant solution to this issue. Maybe I'll post that in the next couple days.