By Zevan | February 1, 2009
Actionscript:
-
package {
-
import flash.display.*;
-
-
public class Main extends MovieClip{
-
-
private static var _display:MovieClip;
-
private static var _stage:Stage;
-
-
// use getters instead of public static vars so they
-
// cannot be reset outside of Main
-
public static function get display():MovieClip{
-
return _display;
-
}
-
-
public static function get stage():Stage{
-
return _stage;
-
}
-
-
public function Main(){
-
Main._display = this;
-
Main._stage = stage;
-
-
// test out the static references
-
var t:Test = new Test();
-
}
-
}
-
}
-
-
class Test{
-
public function Test(){
-
// test out the static references
-
with(Main.display.graphics){
-
lineStyle(1, 0xFF0000);
-
for (var i:int = 0; i<100; i++){
-
lineTo(Math.random() * Main.stage.stageWidth,
-
Math.random() * Main.stage.stageHeight);
-
}
-
}
-
}
-
}
This snippet creates two private static variables that reference the stage and the main timeline/document class. It then uses getters to regulate the use of these static vars so that they cannot be reset from outside the Main class.
Sometimes the amount of extra coding you need to do to maintain a valid stage reference can be cumbersome... similarly, passing references of the document class all around your app can be annoying. If you don't mind using two global vars in your app... this trick can come in handy.
What's nice about using getters here is that if someone tries to do this:
Actionscript:
-
Main.display = new MovieClip();
they'll get an error... in flex, you even see that little red ex pop up next to this line of code if you write it
... that wouldn't happen with a public static var....
By Zevan | January 31, 2009
Actionscript:
-
package {
-
import flash.display.Sprite;
-
-
[SWF(width=500, height=500, backgroundColor=0xEFEFEF, frameRate=30)]
-
public class Main extends Sprite{
-
private var _robotManager:RobotManager;
-
public function Main(){
-
_robotManager = new RobotManager(this, 12);
-
}
-
}
-
}
-
-
class RobotManager{
-
-
private var _robots:Array;
-
private var _top:Sprite;
-
private var _robotNum:int;
-
-
public function RobotManager(top:Sprite, robotNum:int){
-
_top = top;
-
_robotNum = robotNum;
-
_robots = new Array();
-
setupRobots();
-
top.addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function setupRobots():void{
-
for (var i:int = 0; i<_robotNum; i++){
-
_robots[i] = new Robot();
-
_robots[i].x = 100+(i % 3) * 150;
-
_robots[i].y = 100+int(i / 3) * 100;
-
_robots[i].addEventListener("death", onRobotDie);
-
_top.addChild(_robots[i]);
-
}
-
}
-
private function onRobotDie(evt:Event):void{
-
for (var i:int = 0; i<_robotNum; i++){
-
if (_robots[i] == evt.currentTarget){
-
_robots.splice(i, 1);
-
}
-
}
-
_robotNum--;
-
}
-
-
private function onRun(evt:Event):void{
-
collisions();
-
}
-
-
private function collisions():void{
-
var currBot:Robot;
-
for (var i:int = 0; i<_robotNum; i++){
-
currBot = _robots[i];
-
for (var j:int = 0; j<_robotNum; j++){
-
if (currBot != _robots[j]){
-
var dx:Number = currBot.x - _robots[j].x;
-
var dy:Number = currBot.y - _robots[j].y;
-
if (Math.sqrt((dx * dx) + (dy * dy)) <40){
-
currBot.die();
-
}
-
}
-
}
-
}
-
}
-
}
-
-
import flash.display.*
-
import flash.events.*;
-
-
class Robot extends Sprite{
-
private var _inc:Number
-
private var _td:Number;
-
private var _theta:Number;
-
private var _rad:Number;
-
-
public function Robot(){
-
_theta = Math.random() * (Math.PI * 2);
-
_td = _theta;
-
_inc = Math.random() * .2 + .05;
-
_rad = _inc * 10;
-
with(graphics){
-
beginFill(0x666666);
-
drawRect(-10,-10,20,20);
-
endFill();
-
beginFill(0x333333);
-
drawRect(-5, -5, 20, 10);
-
lineStyle(0,0xAAAAAA);
-
endFill();
-
drawCircle(0,0,20);
-
}
-
addEventListener(Event.ENTER_FRAME, onRun);
-
}
-
-
private function onRun(evt:Event):void{
-
_td += _inc;
-
_theta += _inc * Math.cos(_td);
-
if (x <0 || y <0 || x> 500 || y> 500){
-
_theta += Math.PI;
-
}
-
x += _rad * Math.cos(_theta);
-
y += _rad * Math.sin(_theta);
-
rotation = _theta / Math.PI * 180;
-
}
-
-
public function die():void{
-
removeEventListener(Event.ENTER_FRAME, onRun);
-
addEventListener(Event.ENTER_FRAME, onDie);
-
}
-
-
private function onDie(evt:Event):void{
-
scaleX = scaleY += .1;
-
alpha -= .1;
-
if (scaleX> 2){
-
removeEventListener(Event.ENTER_FRAME, onDie);
-
if (parent){
-
parent.removeChild(this);
-
dispatchEvent(new Event("death"));
-
}
-
}
-
}
-
}
This code should be placed in a document class... not the timeline (hopefully you already know that)
The above creates 12 wandering robots that die when they collide with one another.
This is not a snippet really... but it's only slightly over 100 lines and it contains a few tricks I find myself using from time to time.
You can view the swf here.
Posted in Events, OOP, arrays, motion | Also tagged collision, flash |
By Zevan | January 30, 2009
Actionscript:
-
// xor
-
trace(0 ^ 0);
-
trace(0 ^ 1);
-
trace(1 ^ 0);
-
trace(1 ^ 1);
-
-
trace("canonical representation of xor");
-
trace(xor(0, 0));
-
trace(xor(0, 1));
-
trace(xor(1, 0));
-
trace(xor(1, 1));
-
-
function xor(a:int, b:int):int{
-
//1-a is the same as int(!a)
-
return 1-a & b | a & 1-b;
-
}
-
-
/*
-
outputs:
-
0
-
1
-
1
-
0
-
canonical representation of xor
-
0
-
1
-
1
-
0
-
*/
I learned about this from reading The Elements of Computing Systems: Building a Modern Computer from First Principles By Noam Nisan and Shimon Schocken
Check out chapter 1 from the above link for an easy to understand description of the canonical representation of a boolean function.
Just a side note... this happens to be the 100th post on actionsnippet.com
Posted in Operators, one-liners | Also tagged flash |
By Zevan | January 29, 2009
Actionscript:
-
var a:int, b:int;
-
-
a = 0;
-
b = 0;
-
-
trace(int(!(a & b)));
-
-
a = 0;
-
b = 1;
-
-
trace(int(!(a & b)));
-
-
a = 1;
-
b = 0;
-
-
trace(int(!(a & b)));
-
-
a = 1;
-
b = 1;
-
-
trace(int(!(a & b)));
-
-
/*
-
outputs:
-
1
-
1
-
1
-
0
-
*/
-
-
/*
-
NAND
-
00 1
-
01 1
-
10 1
-
11 0
-
*/
I started reading "The Elements of Computing Systems: Building a Modern Computer from First Principles" By Noam Nisan and Shimon Schocken. So far it's a very fun read. They talk about the power of NAND in the first chapter....
Posted in Operators, misc | Also tagged flash |