Created
April 16, 2010 01:08
-
-
Save lamberta/367885 to your computer and use it in GitHub Desktop.
LetterFountain.as demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** A "fountain" particle system that emits a random text character. | |
* Written by William Lamberta, http://www.lamberta.org/blog/letter-fountain | |
* | |
* Uses an extended TextField class to store an independent velocity value. | |
* Make sure it includes the public variables: | |
* public var vx:Number = 0; | |
* public var vy:Number = 0; | |
*/ | |
package { | |
import flash.display.Sprite; | |
import flash.display.StageAlign; | |
import flash.display.StageScaleMode; | |
import flash.events.Event; | |
import flash.text.TextFormat; | |
import classes.TextWithVelocity; //my extended TextField | |
//size for blog entry | |
[SWF(width="450", height="300", frameRate="31", backgroundColor="0xffffff")] | |
public class letterFountain extends Sprite { | |
private var count:int = 75; //objects on screen at once | |
private var gravity:Number = 0.65; //velocity applied downward | |
private var textArray:Array = new Array(); | |
public function letterFountain() { | |
init(); | |
} | |
private function init():void { | |
stage.scaleMode = StageScaleMode.NO_SCALE; | |
stage.align=StageAlign.TOP_LEFT; | |
for(var i:int = 0; i < count; i++) { | |
//set text style | |
var format:TextFormat = new TextFormat(); | |
format.font = "Verdana"; | |
format.color = 0x000000; | |
format.size = Math.round(Math.random() * (40-12)) + 12; | |
//create TextField | |
var text:TextWithVelocity = new TextWithVelocity(); | |
text.defaultTextFormat = format; //apply our formatting | |
//random ASCII character code, 65-122 (A-Z, a-z). | |
var randText:int = Math.round(Math.random() * (122-65)) + 65; | |
text.text = String.fromCharCode(randText); | |
//original placement at bottom left corner | |
text.x = 0; | |
text.y = stage.stageHeight; | |
text.vx = Math.random() * (8-4) + 4;//initial random x from 4-8 | |
text.vy = Math.random() * -10 - 10; //random upwards velocity | |
//add to array and stage | |
textArray.push(text); | |
addChild(text); | |
} | |
addEventListener(Event.ENTER_FRAME, onEnterFrame); | |
} | |
private function onEnterFrame(event:Event):void { | |
for(var i:Number = 0; i < textArray.length; i++) { | |
//get each TextField from array as proper type | |
var text:TextWithVelocity = TextWithVelocity(textArray[i]); | |
text.vy += gravity; //add gravity velocity to y | |
text.x += text.vx; //add velocity to x position | |
text.y += text.vy; //add velocity to y position | |
//determine if character is off the stage | |
if(text.x - text.height > stage.stageWidth || | |
text.x + text.height < 0 || | |
text.y - text.height > stage.stageHeight || | |
text.y + text.height < 0) { | |
//change letter | |
var randText:int = Math.round(Math.random() * (122-65)) + 65; | |
text.text = String.fromCharCode(randText); | |
//reset position | |
text.x = 0; | |
text.y = stage.stageHeight; | |
text.vx = 8; | |
text.vy = Math.random() * -10 - 10; | |
} | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* TextWithVelocity, Billy Lamberta. | |
* Extended textfield, with added variables to hold a velocity value. | |
* */ | |
//need to put the package directory structure here | |
package { | |
import flash.text.TextField; | |
public class TextWithVelocity extends TextField { | |
//my internal variables, these actually hold the values | |
private var _vx:Number = 0; | |
private var _vy:Number = 0; | |
public function TextWithVelocity() { | |
super(); | |
} | |
/** | |
* public getter's and setter's to access the values. | |
* considered "best practice." | |
**/ | |
public function get vx():Number { | |
return _vx; | |
} | |
public function set vx(i:Number):void { | |
_vx = i; | |
} | |
public function get vy():Number { | |
return _vy; | |
} | |
public function set vy(i:Number):void { | |
_vy = i; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment