By Zevan | December 8, 2008
Actionscript:
-
stage.frameRate = 30;
-
var xp:Number = 0;
-
var yp:Number = 0;
-
var counter:Number = 0;
-
graphics.lineStyle(0,0x999999);
-
graphics.moveTo(200,200);
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
for (var i:int = 0; i <20; i++) {
-
counter += .05;
-
xp=200 + counter * Math.cos(counter);
-
yp=200 + counter * Math.sin(counter);
-
graphics.lineTo(xp, yp);
-
}
-
if (counter> 400) {
-
removeEventListener(Event.ENTER_FRAME, onLoop);
-
}
-
}
Draws a spiral over time.
Also posted in Graphics | Tagged actionscript, flash, sprial |
By Zevan | December 6, 2008
Actionscript:
-
var xp:Number=Math.random() * stage.stageWidth;
-
var yp:Number=Math.random() * stage.stageHeight;
-
graphics.lineStyle(0,0x000000);
-
graphics.moveTo(xp, yp);
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
xp+=Math.random()*10-5;
-
yp+=Math.random()*10-5;
-
graphics.lineTo(xp, yp);
-
}
Nothing special here, but its good to know that this technique has a name... and that it's NOT Brownian Motion... more here.
By Zevan | November 25, 2008
Actionscript:
-
stage.frameRate = 30;
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void{
-
if (int(Math.random()*5)==1){
-
for (var i:int= 0; i<10; i++) createParticle();
-
}
-
}
-
-
function createParticle():void{
-
var s:MovieClip = new MovieClip();
-
s.graphics.beginFill(0);
-
s.graphics.drawCircle(0,0,Math.random()*10 + 2);
-
s.velX = Math.random()*10-5
-
s.velY = Math.random()*10-5
-
s.posX = s.x = 200;
-
s.posY = s.y = 200;
-
addChild(s);
-
s.addEventListener(Event.ENTER_FRAME, onRunParticle);
-
}
-
-
function onRunParticle(evt:Event):void {
-
var s:MovieClip = MovieClip(evt.currentTarget);
-
s.posX += s.velX;
-
s.posY += s.velY;
-
s.scaleX = s.scaleY -= .04;
-
if (s.scaleX <0){
-
removeChild(s);
-
s.removeEventListener(Event.ENTER_FRAME, onRunParticle);
-
}
-
s.x = s.posX;
-
s.y = s.posY;
-
}
Nothing special here. But my students are always asking me about this - in class I have them alter this code to make fire, water and abstract particle systems.
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().