Skip to content

Instantly share code, notes, and snippets.

@mhulse
Created April 21, 2011 01:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhulse/933493 to your computer and use it in GitHub Desktop.
Save mhulse/933493 to your computer and use it in GitHub Desktop.
Test if document class or assigned to movieclip/sprite in library.
package {
// Imports:
import flash.display.*;
import flash.events.*;
/**
* AS3 test class.
*
* <p>Tests if this class is a "Document" class
* or assigned to a MovieClip exported at runtime.</p>
*
* @author Micky Hulse
*/
public class Test extends MovieClip {
// Constant:
public static var CLASS_REF:Class = Test;
public static var CLASS_NAME:String = 'Test';
// Private:
private var _this:Object = null;
/**
* Class constructor.
*/
public function Test($this:Object = null) {
if ($this != null) {
_this = $this;
}
addEventListener(Event.ADDED_TO_STAGE, init, false, 0, true);
};
/**
* ADDED_TO_STAGE handler.
*
* @param Event
*
* @return void
*/
private function init($e:Event):void {
removeEventListener(Event.ADDED_TO_STAGE, init);
if (_this == null) {
if (this.root is CLASS_REF) {
trace('Document class: ' + CLASS_NAME);
// Create new sprite:
var s:Sprite = new Sprite();
s.graphics.beginFill(0xFF0000);
s.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
s.graphics.endFill();
s.alpha = 0;
// Add sprite to stage:
addChild(s);
// Reference to "this":
_this = s;
} else {
trace('Not document class: ' + this.root);
// Reference to "this":
_this = this;
}
}
// Button/cursor:
_this.buttonMode = true;
_this.useHandCursor = true;
// Event handler:
_this.addEventListener(MouseEvent.MOUSE_UP, onClick, false, 0, true);
};
/**
* Click handler.
*
* @param Event
*
* @return void
*/
private function onClick($e:MouseEvent):void {
trace($e.target);
};
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment