Friday, October 21, 2011
ActionScript Hero
Wednesday, October 12, 2011
Code Story: Three Blind Mice
For today's geeky story time I present the tale of THREE BLIND MICE (as told in AS3). But first, here's the original tale (tail!) as a recap:
And now dramatically interpreted in code:
Here's what it looks like in Flash (if that's easier for you to read):

And here it is with some explanations:

Three blind mice, three blind mice
see how they run, see how they run
they all ran after the farmer's wife
who cut off their tails with a carving knife.
Did you ever see such a thing in your life
as three blind mice?
And now dramatically interpreted in code:
import com.characters.FieldMouse;
import com.characters.FarmersWife;
var mice:Array = new Array();
var farmersWife_mc:FarmersWife = new FarmersWife();
addChild(farmersWife_mc);
FarmersWife.run = function():void
{
//trace("running");
}
FieldMouse.run = function()
{
trace("running");
}
for(var i:int=0; i < 3; i++)
{
var theMouse:FieldMouse = new FieldMouse();
var tail:MovieClip = new MovieClip();
theMouse.addChild(tail);
theMouse.vision = false;
mice.push(theMouse);
}
function cut(event:MouseEvent)
{
while(var j:int=0 < mice.length)
{
carvingKnife(mice[j]);
j++;
}
farmersWife_mc.removeEventListener(MouseEvent.MOUSE_OVER);
}
function carvingKnife(mouse:MovieClip)
{
mouse.removeChild(tail_mc);
}
farmersWife_mc.addEventListener(MouseEvent.MOUSE_OVER, cut);
farmersWife_mc.run();
while(var k:int=0 < mice.length)
{
Mice[j].run();
k++;
}
import com.characters.FarmersWife;
var mice:Array = new Array();
var farmersWife_mc:FarmersWife = new FarmersWife();
addChild(farmersWife_mc);
FarmersWife.run = function():void
{
//trace("running");
}
FieldMouse.run = function()
{
trace("running");
}
for(var i:int=0; i < 3; i++)
{
var theMouse:FieldMouse = new FieldMouse();
var tail:MovieClip = new MovieClip();
theMouse.addChild(tail);
theMouse.vision = false;
mice.push(theMouse);
}
function cut(event:MouseEvent)
{
while(var j:int=0 < mice.length)
{
carvingKnife(mice[j]);
j++;
}
farmersWife_mc.removeEventListener(MouseEvent.MOUSE_OVER);
}
function carvingKnife(mouse:MovieClip)
{
mouse.removeChild(tail_mc);
}
farmersWife_mc.addEventListener(MouseEvent.MOUSE_OVER, cut);
farmersWife_mc.run();
while(var k:int=0 < mice.length)
{
Mice[j].run();
k++;
}
Here's what it looks like in Flash (if that's easier for you to read):

And here it is with some explanations:

Labels: actionscript3, code story, flash, three blind mice
Sunday, August 7, 2011
Snow White and the Seven Dwarves (in AS3)
var maiden:Princess = new Princess();
var queen:Villain = new Villain();
var cabinDwellers:Array = new Array();
maiden.skin = 0xFFFFFF;
maiden.hair = 0x000000;
maiden.kryptonite = apple;
maiden.energy = 100;
maiden.living = true;
maiden.mode = "wishing and hoping";
var hagMode:Boolean = false;
queen.happy = true;
queen.appearance = hagMode;
var fairest:Boolean;
var initial:Boolean = true;
function checkFairest():Boolean
{
if(maiden.living == true)
{
fairest = false;
} else {
fairest = true;
}
return fairest;
}
function checkMirror():Void
{
var mirrorAnswer:Boolean = checkFairest();
if(fairest == true)
{
queen.happy = true;
if(hagMode == true)
{
hagMode = false;
}
} else {
queen.happy = false;
if(initial)
{
var hunter:Weapon = new Weapon();
killMaiden(hunter);
initial = false;
} else {
hagMode = true;
queen.appearance = hagMode;
var apple:Weapon = new Weapon();
killMaiden(apple);
}
}
}
function killMaiden(method:Weapon):Void
{
if(method != maiden.kryptonite)
{
maiden.direction("into the woods");
for(i:int = 0; i < 7; i++)
{
var dwarf:Dwarf = new Dwarf();
cabinDwellers.push(dwarf);
}
sendToWork(cabinDwellers);
checkMirror();
} else {
if(cabinDwellers.length <= 0)
{
maiden.energy = 0;
maiden.living = true;
maiden.mode = "waiting for the one she loves";
}
}
}
function sendToWork(workers:Array):Void
{
for(i:int = 0; i < workers.length; i++)
{
worker:Dwarf = workers.pop();
worker.whistling = true;
}
}
checkMirror();
var queen:Villain = new Villain();
var cabinDwellers:Array = new Array();
maiden.skin = 0xFFFFFF;
maiden.hair = 0x000000;
maiden.kryptonite = apple;
maiden.energy = 100;
maiden.living = true;
maiden.mode = "wishing and hoping";
var hagMode:Boolean = false;
queen.happy = true;
queen.appearance = hagMode;
var fairest:Boolean;
var initial:Boolean = true;
function checkFairest():Boolean
{
if(maiden.living == true)
{
fairest = false;
} else {
fairest = true;
}
return fairest;
}
function checkMirror():Void
{
var mirrorAnswer:Boolean = checkFairest();
if(fairest == true)
{
queen.happy = true;
if(hagMode == true)
{
hagMode = false;
}
} else {
queen.happy = false;
if(initial)
{
var hunter:Weapon = new Weapon();
killMaiden(hunter);
initial = false;
} else {
hagMode = true;
queen.appearance = hagMode;
var apple:Weapon = new Weapon();
killMaiden(apple);
}
}
}
function killMaiden(method:Weapon):Void
{
if(method != maiden.kryptonite)
{
maiden.direction("into the woods");
for(i:int = 0; i < 7; i++)
{
var dwarf:Dwarf = new Dwarf();
cabinDwellers.push(dwarf);
}
sendToWork(cabinDwellers);
checkMirror();
} else {
if(cabinDwellers.length <= 0)
{
maiden.energy = 0;
maiden.living = true;
maiden.mode = "waiting for the one she loves";
}
}
}
function sendToWork(workers:Array):Void
{
for(i:int = 0; i < workers.length; i++)
{
worker:Dwarf = workers.pop();
worker.whistling = true;
}
}
checkMirror();
Labels: actionscript3, addChildsPlay, code, code story
Tuesday, July 5, 2011
Code haikus
Inspired by ThinkGeek's request for geeky haikus - I wrote up this guy:
Noob error alert!
You forgot the instance name.
Null object reference!!
Noob error alert!
You forgot the instance name.
Null object reference!!
Labels: actionscript3
Wednesday, December 3, 2008
Knowing your roots
The best way to grasp something is to explain it to somebody else. SO - I just learned this today and thus this blog is my "explanation to somebody else" (that might be useful to somebody in a google search one day.)
With AS3 came the elimination of "root". I had just learned AS2 roots and then boom... you've got to start all over because there ain't know root no mores!
Basically - in AS2 you can put all of your code on frame 1 of the main timeline - including your functions and global variables. Then if you make nested movieclips (as I do.. nested with nested within nested) you could call up to the function or movieclip in the main timeline by prefacing it with "root." Like so: "root.book_mc.gotoAndPlay("page1");"
Pretty simple once you got the hang of it. It was rather like a man in a sewer calling up to his buddy on the street level to say "Hey, throw me down a bottle of water." Or "Hey, can you call my wife and tell her I'll be late to dinner?"
Well now in ActionScript 3 there is no "root." (Well, actually there IS a root but it does something different that I just don't understand.) Instead you use "MovieClip(this.parent)". Or "MovieClip(this.parent.parent)" if you're nested two deep. (Or MovieClip(this.parent.parent.parent.parent.parent)...etc... although I don't know how far down you can drill before everything just explodes.)
For example - let's say that I have a MovieClip called "book_mc" and inside of that I have a MovieClip called "pages_mc" and inside of THAT I have another MovieClip called "words_mc". Now let's say that inside of "words_mc" I have a MovieClip called "button_mc". And when I click on this button I want the book to go and play it's animation of the book closing. (This blog would be helpful with graphic explanations... but alas... no graphics for you!)
In my code I would first clarify where exactly I was by using a trace statement (within an MouseEvent.CLICK function) like so: "trace (this.parent.name)" This is saying, "Hello button upon which I have just clicked... what is your Mom's name?" (Where "trace (this.name)" would yield a result of "button_btn", by putting the "parent" in there I would instead yield "words_mc".)
OK so now I know that I'm inside of words_mc. And I already know that words_mc is inside of pages_mc which is inside of book_mc. So in order to tell the book to close I would have to write something like "MovieClip(this.parent.parent.parent).gotoAndPlay("closeBook")". In order to call a FUNCTION on the main timeline I would write something like "MovieClip(this.parent.parent.parent).functionNameHere();"
These periods, btw, are actually "dots" and what I'm doing here is called "dot syntax."
Got it now?? Good. Instead of the man in the sewer this is more like some annoying guy at a mixer who is REALLY INTO genealogy, "Hi, who's your Mom? And who is her Mom? And what was her Dad's name? And his Dad? Yeah, I was wondering, could you please ask your Great Great Great Grandfather to send a message to Marty McFly? Awesome!"
(But I don't mean to confuse you... we're not actually time travelling. It's more like the man in the sewer saying, "Hey, I'm on this ladder, and this ladder is attached to a truck, and the truck is on the road and the road is in the universe and we are ALL in the Matrix!")
Damn! I better understand this now.
(Also check this FlashKit.com forum post for another, cleaner, way to do it in a more OOP way.)
With AS3 came the elimination of "root". I had just learned AS2 roots and then boom... you've got to start all over because there ain't know root no mores!
Basically - in AS2 you can put all of your code on frame 1 of the main timeline - including your functions and global variables. Then if you make nested movieclips (as I do.. nested with nested within nested) you could call up to the function or movieclip in the main timeline by prefacing it with "root." Like so: "root.book_mc.gotoAndPlay("page1");"
Pretty simple once you got the hang of it. It was rather like a man in a sewer calling up to his buddy on the street level to say "Hey, throw me down a bottle of water." Or "Hey, can you call my wife and tell her I'll be late to dinner?"
Well now in ActionScript 3 there is no "root." (Well, actually there IS a root but it does something different that I just don't understand.) Instead you use "MovieClip(this.parent)". Or "MovieClip(this.parent.parent)" if you're nested two deep. (Or MovieClip(this.parent.parent.parent.parent.parent)...etc... although I don't know how far down you can drill before everything just explodes.)
For example - let's say that I have a MovieClip called "book_mc" and inside of that I have a MovieClip called "pages_mc" and inside of THAT I have another MovieClip called "words_mc". Now let's say that inside of "words_mc" I have a MovieClip called "button_mc". And when I click on this button I want the book to go and play it's animation of the book closing. (This blog would be helpful with graphic explanations... but alas... no graphics for you!)
In my code I would first clarify where exactly I was by using a trace statement (within an MouseEvent.CLICK function) like so: "trace (this.parent.name)" This is saying, "Hello button upon which I have just clicked... what is your Mom's name?" (Where "trace (this.name)" would yield a result of "button_btn", by putting the "parent" in there I would instead yield "words_mc".)
OK so now I know that I'm inside of words_mc. And I already know that words_mc is inside of pages_mc which is inside of book_mc. So in order to tell the book to close I would have to write something like "MovieClip(this.parent.parent.parent).gotoAndPlay("closeBook")". In order to call a FUNCTION on the main timeline I would write something like "MovieClip(this.parent.parent.parent).functionNameHere();"
These periods, btw, are actually "dots" and what I'm doing here is called "dot syntax."
Got it now?? Good. Instead of the man in the sewer this is more like some annoying guy at a mixer who is REALLY INTO genealogy, "Hi, who's your Mom? And who is her Mom? And what was her Dad's name? And his Dad? Yeah, I was wondering, could you please ask your Great Great Great Grandfather to send a message to Marty McFly? Awesome!"
(But I don't mean to confuse you... we're not actually time travelling. It's more like the man in the sewer saying, "Hey, I'm on this ladder, and this ladder is attached to a truck, and the truck is on the road and the road is in the universe and we are ALL in the Matrix!")
Damn! I better understand this now.
(Also check this FlashKit.com forum post for another, cleaner, way to do it in a more OOP way.)
Labels: actionscript3, flash, tutorials



