By Zevan | November 26, 2008
Actionscript:
-
stage.frameRate = 30;
-
var centerX:Number = 200, centerY:Number = 200, zpos:Number, xpos:Number, ypos:Number, depth:Number;
-
var rotX:Number = 0, rotY:Number = 0, px:Number, py:Number, pz:Number;
-
var cosx:Number, cosy:Number, sinx:Number, siny:Number;
-
-
var canvas:BitmapData = new BitmapData(400,400,true,0xFF000000);
-
addChild(new Bitmap(canvas));
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
canvas.fillRect(canvas.rect, 0xFF000000);
-
rotX += (mouseX / 50 - rotX)/12;
-
rotY += (mouseY / 50 - rotY)/12;
-
-
cosx = Math.cos(rotX);
-
cosy = Math.cos(rotY);
-
sinx = Math.sin(rotX);
-
siny = Math.sin(rotY);
-
for (var a:Number =0; a <6.28; a+=.1){
-
for (var b:Number =0; b <6.28; b+=.05){
-
px = 100 * Math.cos(a) * Math.cos(b) * Math.cos(b);
-
py = 100 * Math.sin(a) * Math.cos(b)
-
pz = 100 * Math.sin(b);
-
zpos= pz*cosx - px*sinx ;
-
xpos= pz*sinx +px*cosx ;
-
ypos= py*cosy - zpos*siny ;
-
zpos= py*siny+ zpos*cosy ;
-
depth = 1/((zpos/340)+1);
-
canvas.setPixel((xpos * depth) + centerX, (ypos * depth) + centerY, 0xFFFFFF);
-
}
-
}
-
}
Renders a rotating 3D shape.
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 24, 2008
Actionscript:
-
this.myVar = "I am a dynamic variable";
-
trace(this.myVar);
-
-
// trace(myVar) // will cause an error
This code will add dynamic untyped variables to the timeline. Although this example is pretty useless, it scratches the surface of an interesting topic.... by default all flash timeline code gets compiled into a giant dynamic document class that uses the undocumented addFrameScript() function. This means that all import statements on the timeline, even ones not on frame one become part of this large document class.
By Zevan | November 23, 2008
Actionscript:
-
var counter:int = 0;
-
-
graphics.lineStyle(0,0x000000);
-
-
recursive();
-
-
function recursive():void{
-
-
if (counter <4000){
-
graphics.moveTo(10,10);
-
graphics.lineTo(counter, 200);
-
counter+=4;
-
// don't call recursive(), use setTimeout instead
-
setTimeout(recursive,0);
-
}else{
-
trace(counter / 4 + " lines were drawn");
-
}
Be careful with this one, if you don't know what your doing you may be able to crash flash.
Anyway... if you use a recursive function in flash you may find yourself getting a stack overflow error. You can avoid this by using setTimeout():
Actionscript:
-
// don't call recursive(), use setTimeout() instead
-
setTimeout(recursive,0);
Of course... the stack overflow error is there for a reason, if used incorrectly you could bring the flash player to a standstill.
You can also achieve animated recursion by actually adding a delay to your setTimout() call:
Actionscript:
-
// don't call recursive(), use setTimeout() instead
-
setTimeout(recursive,10);