Tag Archives: flash

Functions Returning Functions

Actionscript:
  1. var connect:Function = function(xp:Number, yp:Number, col:uint=0):Function{
  2.     graphics.lineStyle(0,col);
  3.     graphics.moveTo(xp, yp);
  4.     var line:Function = function(xp:Number, yp:Number):Function{
  5.         graphics.lineTo(xp, yp);
  6.         return line;
  7.     }
  8.     return line;
  9. }
  10.  
  11. // draw a triangle
  12. connect(200,100)(300,300)(100,300)(200, 100);
  13.  
  14. // draw a box
  15. connect(100,100, 0xFF0000)(150,100)(150,150)(100, 150)(100,100);

This is one of those techniques that I never really get tired of. It's pretty useless, but fun to play around with every now and then. This draws the somewhat boring looking picture below:

[EDIT]
A few people pointed out that this could be simplified with arguments.callee... So here is an example... it does the same thing as the original code...

Actionscript:
  1. var connect:Function = function(xp:Number, yp:Number, col:uint=0):Function{
  2.     graphics.lineStyle(0,col);
  3.     graphics.moveTo(xp, yp);
  4.     return function(xp:Number, yp:Number):Function{
  5.         graphics.lineTo(xp, yp);
  6.         return arguments.callee;
  7.     }
  8. }

Posted in functions, misc | Also tagged , | 6 Comments

QuickBox2D Custom Debug Draw

Actionscript:
  1. import com.actionsnippet.qbox.*;
  2. import Box2D.Dynamics.*
  3.  
  4. stage.frameRate = 60;
  5.  
  6. var sim:QuickBox2D = new QuickBox2D(this, {debug:true});
  7.  
  8. // get at the b2DebugDraw instance
  9. var debug:b2DebugDraw = sim.w.m_debugDraw;
  10. debug.m_drawScale = 30.0;
  11. debug.m_fillAlpha = 0.5;
  12. debug.m_alpha = 0.5;
  13. debug.m_lineThickness = 1.0;
  14. debug.m_drawFlags = 0xFF;
  15.  
  16. sim.createStageWalls();  
  17.  
  18. for (var i:int = 0; i<10; i++){
  19.   sim.addBox({x:3 + i, y:3 + i, width:2, height:0.5});
  20. }
  21. sim.addCircle({x:12, y:5, radius:2});
  22.  
  23. sim.start();
  24. sim.mouseDrag();

Note: This snippet requires the QuickBox2D library

This snippet shows an easy way to get at the settings for Box2D's debug renderer.


Have a look at the swf...

Posted in Uncategorized | Also tagged , , , | 2 Comments

Obfuscated Slider

Actionscript:
  1. var slider:MovieClip = makeSlider();
  2. slider.addEventListener(Event.CHANGE, function(evt:Event):void{
  3.     trace(evt.currentTarget.percent);                                  
  4. });
  5.  
  6. function makeSlider():MovieClip{
  7.     var slider:MovieClip = MovieClip(addChild(new MovieClip()));
  8.     var circle:Sprite = Sprite(slider.addChild(new Sprite()));
  9.     with (circle.graphics) beginFill(0x000000), drawCircle(0,0,10);
  10.     var line:Shape = Shape(slider.addChild(new Shape()));
  11.     with (line.graphics) lineStyle(0,0x000000), lineTo(0, 100);
  12.     slider.x = slider.y = 100;
  13.     circle.addEventListener(MouseEvent.MOUSE_DOWN, function(evt:Event):void{ evt.currentTarget.startDrag(false, new Rectangle(0,0,0,100)), slider.addEventListener(Event.ENTER_FRAME, onChange) });
  14.     var stopIt:Function = function(){ stopDrag(), slider.removeEventListener(Event.ENTER_FRAME, onChange) };
  15.     stage.addEventListener(Event.MOUSE_LEAVE, stopIt);
  16.     stage.addEventListener(MouseEvent.MOUSE_UP, stopIt);
  17.     return slider;
  18. }
  19. function onChange(evt:Event):void { evt.currentTarget.percent = evt.currentTarget.getChildAt(0).y / 100, evt.currentTarget.dispatchEvent(new Event(Event.CHANGE)) }

This is a pretty nasty implementation of a basic slider. Just felt like writing some obfuscated code today... It could probably be made even more confusing with some additional tweaks...

Have a look at the swf over at wonderfl...

Posted in UI | Also tagged , | Comments closed

ActionSnippet Contest Winner

The other judge for the contest was Rich Shupe. We just reviewed the entries together and have decided on a winner....

The winner is Petri Leskinen (piXelero)

Have a look at the winning snippet if you haven't seen it already.

Rich and I thought that Kyle Phillips deserved honorable mention for his google analytics snippet.

Thanks to everyone who sent in snippets. Even though there were only a handful of submissions I had a good deal of fun reviewing and judging. I still have two more entries that I have yet to publish... so stay tuned...

Posted in misc | Also tagged , , | Leave a comment