My first recipe: Magnetic Poetry
so, my first little recipe from the ActionScript 3.0 cookbook, happens to be one that i really like... My fiance' and i have a set on our 'fridge and its always good fun to make some cool poetry or whatever... anyway, this makes a 600x600 dark dark blue magnetic poetry widget. ive not done much with mxml per se, im pretty much sticking to learning actionscript 3.0 first.
so, without any further babbling... here it is, and the code is below.
** NOTE**
i can take credit for only the hacks of this code, the code is basically straight from the book, although i have embellished and changed the code at some points, its basically verbatim. however, i can read this, i can write all of this, i can even understand it all!! so far, being a coldfusion developer hasnt hindered me one bit, in fact, a lot of the stuff ive figured out over the years in cf, directly apply to learning actionscript 3.0. ill talk about that later as i get a good handle on exactly how.
Here is the code for my piece "Floetry"
//this is my take on the magnetic poetry lesson the incredible
//ActionScript 3.0 cook book.
//thanks for showing me the light fellas.
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
[SWF(backgroundColor="#00000f", frameRate="35", width="600", height="600")]
public class Floetry extends Sprite
{
public function Floetry()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
//Create a string and split the string into an array of words
//make sure you put spaces at the end, like near the word kennedy.
//if you dont do that, you will end up with words that are joined, bad bad.
var example:String =
"on up down across street buddha karma good bad ugly hottie chick dude tony kennedy president vice-president " +
"lily carlos jezebel poop factory piss carpet bob marley damian jamaica deck i i i on on on at " +
"inside outside three one two four 69 do fire buttslide dog floor jump";
var words:Array = example.split( " " );
var word:Sprite;
var wordText:TextField;
var textFormat:TextFormat = new TextFormat();
textFormat.size = 20;
textFormat.font = 'Calibri';
for ( var i:int = 0; i < words.length; i++ ) {
//here we create a new sprite for each word.
word = new Sprite();
addChild(word);
//next we create a text field within the sprite by creating a new
//Textfield instance and adding it as a child
wordText = new TextField();
word.addChild(wordText);
//now we need to make a textfield that has the value of the word in it
//give it a border and a background and make it not-selectable so that we can drag it around
wordText.autoSize = TextFieldAutoSize.LEFT;
wordText.border = true;
wordText.background = true;
wordText.selectable = false;
//i wanted random font colors so im taking the value of that greyscale color and
//multiplying it with a random value... thanks andy :)
wordText.textColor = Math.random() * 0xaaaaaa;
wordText.backgroundColor = 0x000000;
wordText.defaultTextFormat = textFormat;
//here we actually set the value of the text field with the word
wordText.text = words[i];
//and now we add a event handler for the pickup and dropoff of the textFields
word.addEventListener(MouseEvent.MOUSE_DOWN, handleDrag);
word.addEventListener(MouseEvent.MOUSE_UP, handleDrop);
//and now to make it more like a true refridgerator, lets randomize the placement of the words.
var rx:Number = Math.random() * stage.stageWidth - word.width;
var ry:Number = Math.random() * stage.stageHeight - word.height;
word.x = rx;
word.y = ry;
}
function handleDrag(event:MouseEvent):void {
//the thing we drag will be the textfield, so to get to that
//we have to go up to the parent
var word:Sprite = event.target.parent;
//while dragging we want it to be z-indexed to the top
setChildIndex(word, numChildren - 1);
//now, as the mouse moves, we drag stuff
word.startDrag();
}
function handleDrop(event:MouseEvent):void {
var word:Sprite = event.target.parent;
//stop dragging when mouse lets go
word.stopDrag();
}
}
}
}
Labels: actionscript, coldfusion, Flex, poetry, tony weeg
