Latest snippets

Gumowski/Mira Pseudo-Soundwave

[SWF(width = 600, height = 600)]
var a:Number = 0.02;
var b:Number = .9998;
 
var xn1:Number = 5;
var yn1:Number = 0;
var xn:Number, yn:Number;
 
var scale:Number = 10;
var iterations:Number = 20000;
var step:Number = stage.stageWidth / iterations;

function f(x:Number):Number{
    var x2:Number = x * x;
    return a * x + (2 * (1 - a) * x2) / (1 + x2);
}

var canvas:BitmapData = Bitmap(addChild(new Bitmap(new BitmapData(600,600,false,0xEFEFEF)))).bitmapData;

var circle = new Sprite();
with(circle.graphics) beginFill(0, 0.3), drawCircle(2,2,1);

var dot:BitmapData = new BitmapData(4,4,true, 0x00000000);
dot.draw(circle);

var pnt:Point = new Point();

var txt:TextField = TextField(addChild(new TextField()));
txt.text = "move mouse";
                                                      
addEventListener(Event.ENTER_FRAME, onLoop);
function onLoop(evt:Event):void {
   
    canvas.fillRect(canvas.rect, 0xEFEFEF);

    a = mouseY / 1000;
    xn1 = mouseX / 30;
    yn1 = 0;
    for (var i:int = 0; i<iterations; i++){
          xn = xn1;
          yn = yn1;
         
          xn1 = b * yn + f(xn);
          yn1 =  -xn + f(xn1);
		  pnt.x = i * step;
		  pnt.y = 300 + yn1 * scale;
		  canvas.copyPixels(dot, dot.rect, pnt, null, null, true);
		
    }
}

Try it out:

Gumowski Mira Pseudo-soundwave – wonderfl build flash online

Closest Point on a Line

// from
// http://www.gamedev.net/topic/444154-closest-point-on-a-line/

function getClosestPoint(A:*, B:*, P:*, segmentClamp:Boolean=true):Point {
	var AP:Point = new Point(P.x - A.x, P.y - A.y),
	    AB:Point = new Point(B.x - A.x, B.y - A.y);
	var ab2:Number=AB.x*AB.x+AB.y*AB.y;
	var ap_ab:Number=AP.x*AB.x+AP.y*AB.y;
	var t:Number=ap_ab/ab2;
	if (segmentClamp) {
		if (t<0.0) {
			t=0.0;
		} else if (t > 1.0) {
			t=1.0;
		}

	}
	return new Point(A.x + AB.x * t, A.y + AB.y * t);
}


var point:Sprite = Sprite(addChild(new Sprite()));
point.x = 50;
point.y = 50;
with(point.graphics) beginFill(0), drawCircle(0,0, 3);

var line:MovieClip = MovieClip(addChild(new MovieClip()));
line.a = new Point(20, 100);
line.b = new Point(300, 60);
with(line.graphics) lineStyle(0), moveTo(line.a.x, line.a.y), lineTo(line.b.x, line.b.y);

var closestPoint:Point = getClosestPoint(line.a, line.b, point);

var closest:Sprite = Sprite(addChild(new Sprite()));
closest.x = closestPoint.x;
closest.y = closestPoint.y;
with(closest.graphics) beginFill(0xFF0000), drawCircle(0,0, 3);

ActionSnippet.zip

2018 UPDATE: grab the zip here https://actionsnippet.com/actionsnippet.zip
Seems I missed a good 15-20 requests over the years – so here it is ^^

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.

Circle Fitting

var circs:Array = []
var circNum:int = 600;
addEventListener(Event.ENTER_FRAME, onAdd);
function onAdd(evt:Event):void {
	if (circs.length < circNum){
		makeGrowable();
	}
}

function makeGrowable(){
	
	var s:MovieClip = MovieClip(addChild(new MovieClip()));
	s.x = Math.random() * stage.stageWidth;
	s.y = Math.random() * stage.stageHeight;
	with(s.graphics){
		lineStyle(0,0);
		drawCircle(0,0,10);
	}
	s.scaleX = s.scaleY = 0;
	circs.push(s);
	s.addEventListener(Event.ENTER_FRAME, onScaleUp);
}

function onScaleUp(evt:Event):void {
	var c:MovieClip = MovieClip(evt.currentTarget);
	c.scaleX = c.scaleY += 0.05;
	for (var i:int = 0; i<circs.length; i++){
		var circ:MovieClip = circs[i];
		if (circ != c){
			var amt:Number = circ.width/2 + c.width/2;
			var dx:Number = circ.x - c.x;
			var dy:Number = circ.y - c.y;
			var dist:Number = Math.sqrt(dx * dx + dy * dy);
			if (amt > dist){
				c.removeEventListener(Event.ENTER_FRAME, onScaleUp);
				if (c.scaleX < 0.1){
					if (contains(c)){
					removeChild(c);
					}
				}
			}
		}
		
	}
}

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:

CircleFitting – wonderfl build flash online

Fill in the Blank


var story:String = "Fill in the _____.";


var txt:TextField = new TextField();
txt.defaultTextFormat = new TextFormat("Georgia", 20);
txt.width = stage.stageWidth;
txt.multiline = true;
txt.wordWrap = true;
txt.text = story;
addChild(txt);

var alph:Array = "abcdefghijklmnopqrstuvwxyz".split("");
var keys:Object = {};
for (var i:int = 0; i<alph.length; i++){
	keys[65 + i] = alph[i];
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
function onKeyPressed(evt:KeyboardEvent):void{
	 
	if (evt.keyCode == Keyboard.ENTER){
		story = "Fill in the _____.";
		txt.text = story;
	}
	
	for (var i:int = 0; i<story.length; i++){
		if (story.charAt(i) == "_"){
			var head:String = story.substr(0, i);
			var tail:String = story.substr(i + 1);
			var letter:String = keys[evt.keyCode];
			if (!letter) return;
			story = head + letter + tail;
			
			txt.text = story;
			
			break;
		}
	}
}

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

Epicycloid Again

var xp:Number = 0;
var yp:Number = 0;
var t:Number = 0;
var a:Number = 100;
var b:Number = 10;
x = stage.stageWidth / 2;
y = stage.stageHeight / 2;

graphics.lineStyle(0,0x000000);
addEventListener(Event.ENTER_FRAME, onRun);
function onRun(evt:Event):void {
	var p:Number = ((a + b)/b)*t
	xp = (a + b) * Math.cos(t) - b * Math.cos(p);
	yp = (a + b) * Math.sin(t) - b * Math.sin(p);
	if (t == 0){
	  graphics.moveTo(xp, yp);
	}else{
	  graphics.lineTo(xp, yp);
	}
	t += 0.05;
}

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

Fish Curve

var xp:Number = 0;
var yp:Number = 0;
var t:Number = 0;
var a:Number = 200;
x = stage.stageWidth / 2;
y = stage.stageHeight / 2;

graphics.lineStyle(0,0x000000);
addEventListener(Event.ENTER_FRAME, onRun);
function onRun(evt:Event):void {
	xp = a * Math.cos(t) - (a * Math.pow(Math.sin(t),2))/Math.sqrt(2);
	yp = a * Math.cos(t) * Math.sin(t);
	if (t == 0){
	  graphics.moveTo(xp, yp);
	}else{
	  graphics.lineTo(xp, yp);
	}
	t += 0.05;
}

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