By Zevan | February 16, 2009
Actionscript:
-
trace("min", Math.min(100, 99, 32, 75, 44, 90));
-
trace("max", Math.max(100, 99, 32, 75, 44, 90));
-
/*outputs:
-
32
-
100
-
*/
It's easy not to notice that Math.min() and Math.max() can take any number of arguments. I've seen people nest min/max calls instead of using the above option.... like this:
Actionscript:
-
trace(Math.min(1, Math.min(2, 3)));
Posted in misc | Also tagged flash |
By Zevan | February 15, 2009
Actionscript:
-
stage.frameRate=30;
-
var nav:Sprite = Sprite(addChild(new Sprite()));
-
nav.x=nav.y=150;
-
-
var cover:Sprite;
-
var coverDest:Number=0;
-
var spacing:int=4;
-
var btnNum:int=6;
-
-
buildNav();
-
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
cover.x += (coverDest - cover.x) /4;
-
}
-
-
function buildNav():void {
-
for (var i:int = 0; i<btnNum; i++) {
-
var b:Sprite=makeBox(50,50);
-
b.x = i * (b.width + spacing);
-
b.buttonMode=true;
-
b.addEventListener(MouseEvent.CLICK, onClick);
-
}
-
cover=makeBox(54, 60);
-
cover.blendMode=BlendMode.INVERT;
-
}
-
-
function onClick(evt:MouseEvent):void {
-
coverDest=evt.currentTarget.x;
-
}
-
-
function makeBox(w:Number, h:Number) {
-
var b:Sprite = new Sprite();
-
with (b.graphics) {
-
beginFill(0x000000),drawRect(-w/2,-h/2,w,h);
-
}
-
nav.addChild(b);
-
return b;
-
}
This is something I do in my intermediate flash classes... the only difference is that we do the graphics in the flash IDE during class time instead of drawing them with ActionScript.
Posted in motion | Also tagged flash |
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.
Posted in DisplayObject, Graphics | Also tagged flash |
By Zevan | February 13, 2009
Actionscript:
-
var path:String = "section/home.swf";
-
-
var deepLinkValue:String = path.split("/")[1].split(".swf")[0];
-
-
trace(deepLinkValue);
-
/*outputs:
-
home
-
*/
As a primarily self taught programmer there was a time when I didn't know what a regular expression was. There was also a time when ActionScript didn't have them (you had to use a library). Sometimes when I'm in a rush I still do weird things like the above instead of regular expressions... pretty nasty I know.
Here are two different regular expressions that do the same thing:
Actionscript:
-
var path = "section/home.swf"
-
-
trace(path.match(/\w+(?=\.)/))
-
/*outputs:
-
home
-
*/
-
-
trace(path.replace(/.+\/|\..+/g, ""))
-
/* also outputs:
-
home
-
*/
Actually, I'd be curious to see other regular expressions that only return one value (not an array) that achieve this same thing... feel free to post of a comment if you can think of one...