Simple Actionscript Class setup
The simplest start to an Actionscript class
package { import flash.display.Sprite; public class ClassName extends Sprite { public function ClassName () { init(); } private function init():void { trace("init"); } } }

The simplest start to an Actionscript class
package { import flash.display.Sprite; public class ClassName extends Sprite { public function ClassName () { init(); } private function init():void { trace("init"); } } }
Sometimes you will need a class to be on the stage before you can use it. For example, when using the stage properties like stage.stageWidth and stage.stageHeight.
I have found the following to work the best:
package { public class DisplayItem extends Sprite { public function SomeClass () { addEventListener(Event.ADDED_TO_STAGE, init); } private function init(event:Event):void { //It's always best to remove any event listeners once you dont need them any more. event.target.removeEventListener(Event.ADDED_TO_STAGE, init); trace("stage height = "+stage.stageHeight); trace("stage width = "+stage.stageWidth); } } }
This is the simplest way to center anything.
anyThing = new AnyThing(); anyThing.x = (stage.stageWidth * 0.5) - (anyThing.width * 0.5); anyThing.y = (stage.stageHeight * 0.5) - (anyThing.height * 0.5); addChild(anyThing);
You’ll often want to draw a rectangle using the graphics.drawRect() method, like:
var box:Sprite = new Sprite(); box.graphics.beginFill(0x7170b6, 0.85); //drawRect(x, y, width,height) box.graphics.drawRect(100,50,300, 200); box.graphics.endFill(); addChild(box);
where the x and y values are 100 & 50. I have found that using the x and y values inside the drawRect method to not function as I wish.
It is simply solved by setting the values outside of the method, like:
var box:Sprite = new Sprite(); box.graphics.beginFill(0x7170b6, 0.85); box.graphics.drawRect(0,0,300, 200); box.graphics.endFill(); box.x = 100; box.y = 50; addChild(box);
I have always found that when programming large sites, controlling fonts becomes problematic. Not having a central font repository made maintaining and changing fonts a struggle.
So I created my own font area, a single point of contact for all my fonts – being a single point of contact it makes sense to make it a Singleton class. It uses getter function which hold the TextFormat information.
If you use, say, the same format over 5 different classes throughout your site,you now only have to change it in one place, rather than 5.
// // FontHandler - font repository // package com.fourteenlox.utils { import flash.text.*; public class FontHandler { private static var _instance:FontHandler; public function FontHandler(singletonEnforcer:SingletonEnforcer) {} public static function getInstance():FontHandler { if(FontHandler._instance == null) { FontHandler._instance = new FontHandler(new SingletonEnforcer()) } return FontHandler._instance; } public function get bodyformat():TextFormat { var _bodyformat:TextFormat = new TextFormat; _bodyformat.size = 14; _bodyformat.font = new HelMed ().fontName; _bodyformat.color = 0x777777; _bodyformat.align = 'left'; return _bodyformat; } public function get titleformat():TextFormat { var _titleformat:TextFormat = new TextFormat; _titleformat.size = 24; _titleformat.font = new HelMed ().fontName; _titleformat.color = 0xFF0000; _titleformat.align = 'left'; return _titleformat; } public function get linkformat():TextFormat { var _linkformat:TextFormat = new TextFormat; _linkformat.size = 14; _linkformat.font = new HelMed ().fontName; _linkformat.color = 0x000000; _linkformat.align = 'left'; _linkformat.underline = true; return _linkformat; } } } class SingletonEnforcer{}
Usage:
import com.fourteenlox.utils.FontHandler; _textTitle = new TextField(); _textTitle.embedFonts = true; _textTitle.defaultTextFormat = FontHandler.getInstance().titleformat; _textTitle.antiAliasType = AntiAliasType.ADVANCED; _textTitle.autoSize = TextFieldAutoSize.LEFT; _textTitle.multiline = true; _textTitle.wordWrap = true; _textTitle.selectable = false; _textTitle.width = 700; _textTitle.text = _xml.text.@title; addChild(_textTitle);
Grrrrr, can’t work out how to line up the code, it seems to break when I paste it here….
It’s very easy to work that out. You just need to use the Modulus function (%).
var isEven:Boolean = ((num % 2) == 0);
credit: bit-101
Firstly you need to add an Event Listener to your Cube.
cube.addEventListener(InteractiveScene3DEvent.OBJECT_PRESS, onClickHandler);
Inside the onClickHandler is the function for determining which face is being clicked on. I have only included the front and back in the switch case, but you can also have “top”, “bottom”, “right” and “left”.
private function onClickHandler(event:InteractiveScene3DEvent):void { var face:String = faceHandler(event); switch (face) { case "front" : // Do something when the front is clicked break; case "back" : // Do something when the back is clicked break; } }
Here is the faceHandler function. It uses the InteractiveUtils.getMapCoordAtPoint Papervision function to determine which face is being clicked on.
private function faceHandler(event:InteractiveScene3DEvent):String { var faceNormals = { back:new Number3D(0,0,1), front:new Number3D(0,0,-1), right:new Number3D(1,0,0), left:new Number3D(-1,0,0),bottom:new Number3D(0,-1,0), top:new Number3D(0,1,0)}; var point:Object = InteractiveUtils.getMapCoordAtPoint(event.face3d, event.sprite.mouseX, event.sprite.mouseY); for(var key:String in faceNormals) { if ( Number3D.dot(faceNormals[key], event.face3d.faceNormal) > 0.99 ) return key; } return "none"; }
I can’t take credit for the initial code (I can’t find where I got it, to credit them) – however i have edited it and expanded the functionality.
If you’ve ever wanted to create a block of filled colour with a gradient for a background, similiar to the standard Flex background, then here you go:
var mat:Matrix = new Matrix(); var bg:Sprite = new Sprite(); mat.createGradientBox( stage.stageWidth, stage.stageHeight, Math.PI * .5 ); bg.graphics.beginGradientFill( GradientType.LINEAR, [ 0xFFFFFF, 0x000000 ], [ 1, 1 ], [ 0, 255 ], mat ); bg.graphics.drawRect( 0, 0, stage.stageWidth, stage.stageHeight ); bg.graphics.endFill(); addChild( bg );