By Zevan | February 14, 2009
Actionscript:
-
[SWF(width=600,height=400,backgroundColor=0x000000,frameRate=30)]
-
var mcs:Vector.<MovieClip> = new Vector.<MovieClip>();
-
var num:int = 2000;
-
for (var i:int = 0; i<num; i++){
-
var s:MovieClip = new MovieClip();
-
s.graphics.lineStyle(0,0xFFFFFF);
-
s.graphics.drawCircle(0,0, Math.random()*30 + 3);
-
s.x = Math.random()*stage.stageWidth;
-
s.y = Math.random()*stage.stageHeight;
-
// comment out to kill your cpu
-
s.cacheAsBitmap = true;
-
addChild(s);
-
s.vx = Math.random() * 2 - 1;
-
s.vy = Math.random() * 2 - 1;
-
mcs.push(s);
-
}
-
addEventListener(Event.ENTER_FRAME, onCenter);
-
function onCenter(evt:Event):void{
-
for (var i:int = 0; i<num; i++){
-
mcs[i].x += mcs[i].vx;
-
mcs[i].y += mcs[i].vy;
-
}
-
}
This was created in response to a question about the real speed gain of cacheAsBitmap. Just comment out the cacheAsBitmap line to see the difference.
Despite it's extreme simplicity, this is actually a rather fun file to play with, change some of the graphics class method calls around and see what happens.
Also posted in Graphics | Tagged actionscript, flash |
By Zevan | February 1, 2009
Actionscript:
-
package {
-
import flash.display.*;
-
-
public class Main extends MovieClip{
-
-
private static var _display:MovieClip;
-
private static var _stage:Stage;
-
-
// use getters instead of public static vars so they
-
// cannot be reset outside of Main
-
public static function get display():MovieClip{
-
return _display;
-
}
-
-
public static function get stage():Stage{
-
return _stage;
-
}
-
-
public function Main(){
-
Main._display = this;
-
Main._stage = stage;
-
-
// test out the static references
-
var t:Test = new Test();
-
}
-
}
-
}
-
-
class Test{
-
public function Test(){
-
// test out the static references
-
with(Main.display.graphics){
-
lineStyle(1, 0xFF0000);
-
for (var i:int = 0; i<100; i++){
-
lineTo(Math.random() * Main.stage.stageWidth,
-
Math.random() * Main.stage.stageHeight);
-
}
-
}
-
}
-
}
This snippet creates two private static variables that reference the stage and the main timeline/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class.
Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome... similarly, passing references of the document class all around your app can be annoying. If you don't mind using two global vars in your app... this trick can come in handy.
What's nice about using getters here is that if someone tries to do this:
Actionscript:
-
Main.display = new MovieClip();
they'll get an error... in flex, you even see that little red ex pop up next to this line of code if you write it ... that wouldn't happen with a public static var....
Also posted in OOP, variables | Tagged actionscript, flash, flex |
By Zevan | January 18, 2009
Actionscript:
-
[SWF(width=500, height=500, backgroundColor=0xFFFFFF, frameRate=30)]
-
-
var box:Sprite = createSprite("Rect", 0, 0, 100, 100, 0xFF0000);
-
-
var d0:Sprite = drag(createSprite("Ellipse", -5, -5, 10, 10));
-
d0.x = d0.y = 200;
-
-
var d1:Sprite = drag(createSprite("Ellipse", -5, -5, 10, 10));
-
d1.x = 200
-
d1.y = 300;
-
-
var d2:Sprite = drag(createSprite("Ellipse", -5, -5, 10, 10));
-
d2.y = d0.y;
-
d2.x = 300;
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
-
var m:Matrix = new Matrix();
-
m.scale((d2.x - d0.x) / 100,(d1.y - d0.y) / 100);
-
m.translate(d0.x, d0.y);
-
-
m.c = (d1.x - d0.x) / 100
-
m.b = (d2.y - d0.y ) / 100
-
-
box.transform.matrix = m;
-
}
-
-
function createSprite(shape:String, xp:Number, yp:Number, w:Number, h:Number, col:uint=0x444444):Sprite {
-
var s:Sprite = new Sprite();
-
s.graphics.beginFill(col);
-
s.graphics["draw" + shape](xp, yp, w, h);
-
addChild(s);
-
return s;
-
}
-
-
function drag(target:*):*{
-
target.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });
-
return target;
-
}
-
-
stage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });
The above creates a red rectangle that can be skewed by dragging 3 points. Why is this so cool you ask?
This is why (move your mouse)
The above was written in flash 7, before the Matrix object existed. So in order to create it I used this technique from Eric Lin.
By Zevan | December 21, 2008
Actionscript:
-
mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
-
function onRollOver(evt:MouseEvent):void {
-
addChild(MovieClip(evt.currentTarget));
-
}
This one is very simple, but it's important to note that using addChild() on something that is already on the display list simply brings it to the top. Back in AS2 we used to do:
Also posted in display list | Tagged actionscript, flash |