Actionscript:
-
[SWF(width=500, height=500, backgroundColor=0x000000, frameRate=30)]
-
-
for (var i:int = 0; i<10; i++){
-
// draggable ellipse
-
var dot:Sprite = drag(createSprite("Ellipse", -10, -10, 20, 20));
-
dot.x = Math.random() * stage.stageWidth ;
-
dot.y = Math.random() * stage.stageHeight ;
-
}
-
-
for (i = 0; i<10; i++){
-
-
var box:Sprite = drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));
-
box.x = Math.random() * stage.stageWidth ;
-
box.y = Math.random() * stage.stageHeight ;
-
}
-
-
-
// createSprite can create ellipses or rectangles
-
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);
-
// trick from a previous post
-
s.graphics["draw" + shape](xp, yp, w, h);
-
addChild(s);
-
return s;
-
}
-
-
// drag and spin add listeners to an untyped target and return that target for easy function nesting
-
function drag(target:*):*{
-
target.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:MouseEvent){ evt.currentTarget.startDrag(); });
-
return target;
-
}
-
-
function spin(target:*, speed:Number):*{
-
target.addEventListener(Event.ENTER_FRAME, function(evt:Event){ evt.currentTarget.rotation+=speed; });
-
return target;
-
}
-
-
stage.addEventListener(MouseEvent.MOUSE_UP, function(){ stopDrag() });
The above will create some draggable circles and some rotating draggable rects... but that's not really the point....
When prototyping and just playing around I write functions that take an Object as an argument, alter that Object in some way and pass that some Object out as a return value.... this makes it so I can write things like this:
Actionscript:
-
drag(spin(createSprite("Rect", -20, -20, 40, 40, 0xFF0000), Math.random()*5 + 1));
Readability can be a problem so... consider that before using this for anything...