Crockford’s Create

So here is a javascript inspired snippet. It's also in javascript style with no typing really. To port to javascript would take less than 4 minutes:

Actionscript:
  1. // 1D
  2. function _(a, b){
  3.     var c = {};
  4.     for (var i in b){
  5.         trace(i);
  6.       a[i] = b[i];
  7.     }
  8.     return a;
  9. }
  10.  
  11. // douglas crockfords create
  12. function create(o){
  13.     var F = function(){};
  14.     F.prototype = o;
  15.     return  new F();
  16. }
  17.  
  18. // 1D
  19. function obj(o){
  20.     for (var i in o){
  21.         trace(o[i]);
  22.     }
  23. }
  24.  
  25. var Mover = {
  26.     ox : 0, oy : 0,
  27.     x : 100, y : 100, radius: 10, t : 0, speed : 0.2,
  28.     run : function(){
  29.         //namespace Mover;
  30.         this.x = this.ox + this.radius * Math.cos(this.t);
  31.         this.y = this.oy + this.radius * Math.sin(this.t);
  32.         this.t += this.speed;
  33.     }
  34. };
  35. var SubMover = _(Mover,
  36.                  {ox : 200, oy: 200,
  37.                   draw : function(){
  38.                      this.run();
  39.                      graphics.beginFill(0);
  40.                      graphics.drawCircle(this.x, this.y, 10);
  41.                   }
  42.                  });
  43.  
  44. var m = create(SubMover);
  45.  
  46. setInterval(function(){
  47.   m.radius += 1;           
  48.   m.draw();
  49. }, 30)
  50.  
  51. for (var i in m){
  52.     trace(i);
  53. }

This makes use of Douglas Crockford's Object.create method. Just one of the many ways to do Object Oriented programming with flash or js. I use a combo of many methods depending on the project:

Actionscript:
  1. function Thing(){
  2.   var x = 0;
  3.   return function(){
  4.     x += 1;
  5.     // do stuff
  6.   }
  7. }
  8.  
  9. is a nice one that comes to mind - just a simple closure can be used like an object.
  10.  
  11. Almost forgot, in AS small projects ONLY. Here is more info about Object.create() :
  12.  
  13. <a href="http://javascript.crockford.com/prototypal.html">http://javascript.crockford.com/prototypal.html</a>

Posted in OOP, functions, javascript | Tagged , , , | Leave a comment

Stereoscopic Papervision

Cross your eyes to see 3D third image:

move your mouse up and down on the interactive demo... click ->

Source is a wreck out of pure laziness (also kind of obvious how this works) so if you want it anyway just comment and I'll post it. I'm not embarrassed ;)

Posted in 3D | Tagged | Leave a comment

ActionSnippet.zip

Who would like a zip of the folder I used when making this site fla files and all? If you want one, post a comment and I'll send it to you.

Actually have a little new content for the site coming, at least one new post.

If you don't get yours within a day of posting... just let me know.

Posted in Uncategorized | 23 Comments

New Site zReference

Actionsnippet has been pretty inactive for the last few months. I took a short break from blogging, but I'm starting up again on a new site... go check it out: zReference

Posted in misc | Tagged | 2 Comments

Quick IE Test

Found this today, not related to actionscript but rather nice. It allows you to take a screen shot of your website in IE... if your on a mac without windows this is a quick way to test in a pinch:

http://ipinfo.info/netrenderer/index.php

Posted in Uncategorized | 6 Comments

Circle Fitting

Actionscript:
  1. var circs:Array = []
  2. var circNum:int = 600;
  3. addEventListener(Event.ENTER_FRAME, onAdd);
  4. function onAdd(evt:Event):void {
  5.     if (circs.length <circNum){
  6.         makeGrowable();
  7.     }
  8. }
  9.  
  10. function makeGrowable(){
  11.    
  12.     var s:MovieClip = MovieClip(addChild(new MovieClip()));
  13.     s.x = Math.random() * stage.stageWidth;
  14.     s.y = Math.random() * stage.stageHeight;
  15.     with(s.graphics){
  16.         lineStyle(0,0);
  17.         drawCircle(0,0,10);
  18.     }
  19.     s.scaleX = s.scaleY = 0;
  20.     circs.push(s);
  21.     s.addEventListener(Event.ENTER_FRAME, onScaleUp);
  22. }
  23.  
  24. function onScaleUp(evt:Event):void {
  25.     var c:MovieClip = MovieClip(evt.currentTarget);
  26.     c.scaleX = c.scaleY += 0.05;
  27.     for (var i:int = 0; i<circs.length; i++){
  28.         var circ:MovieClip = circs[i];
  29.         if (circ != c){
  30.             var amt:Number = circ.width/2 + c.width/2;
  31.             var dx:Number = circ.x - c.x;
  32.             var dy:Number = circ.y - c.y;
  33.             var dist:Number = Math.sqrt(dx * dx + dy * dy);
  34.             if (amt> dist){
  35.                 c.removeEventListener(Event.ENTER_FRAME, onScaleUp);
  36.                 if (c.scaleX <0.1){
  37.                     if (contains(c)){
  38.                     removeChild(c);
  39.                     }
  40.                 }
  41.             }
  42.         }
  43.        
  44.     }
  45. }

Circle fitting is one of those things I've never bothered to do... today I figured I'd give it a try and this is what I came up with. I posted it on wonderfl:

Posted in Graphics, misc, motion | Tagged , , , | 6 Comments

Fill in the Blank

Actionscript:
  1. var story:String = "Fill in the _____.";
  2.  
  3.  
  4. var txt:TextField = new TextField();
  5. txt.defaultTextFormat = new TextFormat("Georgia", 20);
  6. txt.width = stage.stageWidth;
  7. txt.multiline = true;
  8. txt.wordWrap = true;
  9. txt.text = story;
  10. addChild(txt);
  11.  
  12. var alph:Array = "abcdefghijklmnopqrstuvwxyz".split("");
  13. var keys:Object = {};
  14. for (var i:int = 0; i<alph.length; i++){
  15.     keys[65 + i] = alph[i];
  16. }
  17. stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
  18. function onKeyPressed(evt:KeyboardEvent):void{
  19.      
  20.     if (evt.keyCode == Keyboard.ENTER){
  21.         story = "Fill in the _____.";
  22.         txt.text = story;
  23.     }
  24.    
  25.     for (var i:int = 0; i<story.length; i++){
  26.         if (story.charAt(i) == "_"){
  27.             var head:String = story.substr(0, i);
  28.             var tail:String = story.substr(i + 1);
  29.             var letter:String = keys[evt.keyCode];
  30.             if (!letter) return;
  31.             story = head + letter + tail;
  32.            
  33.             txt.text = story;
  34.            
  35.             break;
  36.         }
  37.     }
  38. }

I needed to do a fill in the blank for a personal project that I'm working on and this is what I came up with. Have a look at the swf here:

(you need to click first so you can type with the keyboard):
Fill in the blank

Posted in TextField, UI | Tagged , , | 3 Comments

Epicycloid Again

Actionscript:
  1. var xp:Number = 0;
  2. var yp:Number = 0;
  3. var t:Number = 0;
  4. var a:Number = 100;
  5. var b:Number = 10;
  6. x = stage.stageWidth / 2;
  7. y = stage.stageHeight / 2;
  8.  
  9. graphics.lineStyle(0,0x000000);
  10. addEventListener(Event.ENTER_FRAME, onRun);
  11. function onRun(evt:Event):void {
  12.     var p:Number = ((a + b)/b)*t
  13.     xp = (a + b) * Math.cos(t) - b * Math.cos(p);
  14.     yp = (a + b) * Math.sin(t) - b * Math.sin(p);
  15.     if (t == 0){
  16.       graphics.moveTo(xp, yp);
  17.     }else{
  18.       graphics.lineTo(xp, yp);
  19.     }
  20.     t += 0.05;
  21. }

I've messed with Epicycloids in the past - browsing mathworld I decided to create this snippet. It will draw a curve like this:

Posted in Uncategorized | Tagged , , | 1 Comment

Fish Curve

Actionscript:
  1. var xp:Number = 0;
  2. var yp:Number = 0;
  3. var t:Number = 0;
  4. var a:Number = 200;
  5. x = stage.stageWidth / 2;
  6. y = stage.stageHeight / 2;
  7.  
  8. graphics.lineStyle(0,0x000000);
  9. addEventListener(Event.ENTER_FRAME, onRun);
  10. function onRun(evt:Event):void {
  11.     xp = a * Math.cos(t) - (a * Math.pow(Math.sin(t),2))/Math.sqrt(2);
  12.     yp = a * Math.cos(t) * Math.sin(t);
  13.     if (t == 0){
  14.       graphics.moveTo(xp, yp);
  15.     }else{
  16.       graphics.lineTo(xp, yp);
  17.     }
  18.     t += 0.05;
  19. }

While surfing mathworld I stumbled upon the equation for something called the Fish Curve. This snippet will draw something like this:

Posted in Math, misc | Tagged , , | Leave a comment

Astroid Pedal Curve Variation

Actionscript:
  1. var xp:Number = 0;
  2. var yp:Number = 0;
  3. var t:Number = 0;
  4. var r:Number = 200;
  5. x = stage.stageWidth / 2;
  6. y = stage.stageHeight / 2;
  7.  
  8. graphics.lineStyle(0,0x000000);
  9. addEventListener(Event.ENTER_FRAME, onRun);
  10. function onRun(evt:Event):void {
  11.     r = 200 * Math.cos(t / 10);
  12.     xp = r * Math.pow(Math.cos(t), 3);
  13.     yp = r * Math.pow(Math.sin(t), 3);
  14.     if (t == 0){
  15.       graphics.moveTo(xp, yp);
  16.     }else{
  17.       graphics.lineTo(xp, yp);
  18.     }
  19.     t += 0.1;
  20. }

While browsing mathworld I decided to do a variation on this curve . The above snippet will draw something like this:

Posted in Math, misc | Tagged , , | Leave a comment