Browse the notebook

Month: June 2010

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
Read snippet: Circle Fitting

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

Read snippet: Fill in the Blank