By Zevan | April 24, 2010
Actionscript:
-
var circles:Array = [];
-
for (var i:int = 0; i<30; i++){
-
var c:Sprite = makeCircle();
-
c.x = stage.stageWidth / 2;
-
c.y = stage.stageHeight / 2;
-
c.scaleX = 1 + i/2;
-
c.scaleY = 0.5 + i/4;
-
addChild(c);
-
circles.push(c);
-
}
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
function onLoop(evt:Event):void {
-
circles[0].y += (mouseY - circles[0].y) / 4;
-
for (var i:int = 1; i<circles.length; i++){
-
var pre:Sprite = circles[i - 1];
-
circles[i].y += (pre.y - circles[i].y) / 4;
-
}
-
}
-
function makeCircle():Sprite{
-
var s:Sprite = new Sprite();
-
with(s.graphics){
-
lineStyle(0,0x000000);
-
drawCircle(0,0,10);
-
}
-
return s;
-
}
This morning I woke up with a vision of this simple mouse toy in my head. I decided I might as well code it up... I may do more simple things like this in the next few days, it's relaxing.
Posted in Graphics, misc, motion | Also tagged as3, flash |
Lots of people have mentioned that they have problems with QuickBox2D Polygons. The simple solution is not to use the verts 2d array (which is more like how Box2D does polys). So when in doubt about polygons, simply use the points array which will nearly always work as long as the contour you define does not cross over itself. Here is a simple example on wonderfl:
Also... polygons are covered extensively in part two of the tutorial over at active tuts... more on that later.
Here is the timeline code:
Actionscript:
-
import com.actionsnippet.qbox.*;
-
/*
-
0
-
/ \
-
0_0 0
-
| |
-
0-0
-
*/
-
var sim:QuickBox2D = new QuickBox2D(this);
-
sim.createStageWalls();
-
// define the contour of your poly
-
// no limits as long as it doesn't cross over
-
// itself
-
sim.addPoly({x:10, y:5, points:[0.5,0,
-
1, 1,
-
1, 2,
-
0.5, 2,
-
0.5, 1,
-
0,1,
-
0.5,0],
-
wireframe:false});
-
sim.addCircle({x:11, y:10});
-
sim.start();
-
sim.mouseDrag();
Posted in Uncategorized | Also tagged as3, flash, QuickBox2D |
Actionscript:
-
/*
-
This snippet is by Mels le Noble
-
www.melslenoble.nl
-
It will hide the browser scrollbars.
-
*/
-
-
// see if we are testing locally
-
if (stage.loaderInfo.url.split("/")[2])
-
{
-
ExternalInterface.call("function(){document.body.style.overflow='hidden';document.html.style.overflow = 'hidden';}");
-
}
This snippet by Mels le Noble will hide the browser scrollbars. The bulk of the snippet is the javascript inside the ExternalInterface.call() method. I like the trick that Mels uses to check if the swf is local.... snippet-worthy in itself.
Posted in UI | Also tagged as3, contest, flash, javascript |
By Zevan | September 8, 2009
Actionscript:
-
[SWF(width=600, height=600, frameRate=30)]
-
var sw:Number = stage.stageWidth;
-
var sh:Number = stage.stageHeight;
-
-
var s:Shape = Shape(addChild(new Shape()));
-
-
var scale:Number = 1;
-
var scaleDest:Number = 1;
-
var down:Boolean = false;
-
var dx:Number = 0, dy:Number = 0, time:Number = 0;
-
-
buttonMode = true;
-
-
addInstructions();
-
vectorTexture();
-
-
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);
-
stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function addInstructions():void{
-
var instruct:Sprite = Sprite(addChild(new Sprite()));
-
with (instruct.graphics) beginFill(0x666666), drawRect(0,0,270, 30);
-
instruct.x = instruct.y = 20;
-
var txt:TextField = TextField(instruct.addChild(new TextField()));
-
txt.defaultTextFormat = new TextFormat("Verdana", 11);
-
txt.x = txt.y = 5;
-
txt.selectable = false;
-
with (txt) textColor = 0xFFFFFF, autoSize = "left", text = "Click and hold to zoom, move mouse to pan";
-
}
-
-
function vectorTexture():void{
-
var cols:Vector.<uint> = Vector.<uint>([0xFFFFFF, 0x000000]);
-
var rnd:Vector.<Number> = new Vector.<Number>(6, true);
-
-
for(var i:int = 0 ; i<50; i++){
-
with(s.graphics){
-
lineStyle(Math.random() * 50 + 2, cols[int(Math.random()*cols.length)]);
-
drawCircle(Math.random() * sw, Math.random() * sh, 10 + Math.random() * Math.random() * 400);
-
}
-
}
-
s.graphics.lineStyle(20, 0xCCCCCC);
-
s.graphics.drawRect(0, 0,sw, sh);
-
}
-
-
function onDown(evt:MouseEvent):void{ down = true; }
-
function onUp(evt:MouseEvent):void{ down = false; }
-
function onLoop(evt:Event):void {
-
if (down){
-
scaleDest *= 1.05;
-
time = 0;
-
}else{
-
time++;
-
// zoom out after 30 iterations
-
if (time == 30){
-
scaleDest = 1;
-
}
-
}
-
scale += (scaleDest - scale) / 4;
-
if (scale> 10) scale = scaleDest = 10;
-
-
dx += (mouseX - dx) / 4;
-
dy += (mouseY - dy) / 4;
-
if (dx <0) dx = 0;
-
if (dy <0) dy = 0;
-
if (dx> sw) dx = sw;
-
if (dy> sh) dy = sh;
-
-
// matrix zoom/pan
-
var m:Matrix = s.transform.matrix;
-
m.identity();
-
m.translate(-dx,-dy);
-
m.scale(scale, scale);
-
m.translate(dx,dy);
-
s.transform.matrix = m;
-
}
I haven't been by the computer much these last two weeks - been traveling. Going back to nyc tomorrow so I'll go back to posting once a day.
This snippet uses a transformation matrix to zoom in and pan a Sprite instance. For demo purposes I filled the sprite with a few circles - but you'd likely be using this with a vector image of a map, a floor plan or some other graphic that warrants zooming and panning.
Back around flash 7 (I think) before the Matrix class was introduced we used to have to use MovieClip nesting to achieve this effect.
Have a look at the swf...

Posted in Graphics, matrix, motion | Also tagged as3, flash |