Not just Actionscript noodlings

25 Nov

Simple Actionscript Class setup

The simplest start to an Actionscript class

	import flash.display.Sprite;
	public class ClassName extends Sprite
		public function ClassName ()
		private function init():void

19 Oct

Making sure something is on the stage

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:

	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., init);
			trace("stage height = "+stage.stageHeight);
			trace("stage width = "+stage.stageWidth);

19 Oct

Centering anything

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);

06 Aug

drawRect() co-ordinates problem

You’ll often want to draw a rectangle using the graphics.drawRect() method, like:

var box:Sprite = new Sprite();, 0.85);
//drawRect(x, y, width,height),50,300, 200);;

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();, 0.85);,0,300, 200);;
box.x = 100;
box.y = 50;

08 Jul

Font Repository

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{}


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;

Grrrrr, can’t work out how to line up the code, it seems to break when I paste it here….

02 Jul

Is a number even or odd?

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

02 Jul

Find which face is clicked on a Papervision3D Cube.

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
				case "back" : 
					// Do something when the back is clicked

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 ([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.

23 May

Creating gradient background

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 ); GradientType.LINEAR, [ 0xFFFFFF, 0x000000 ], [ 1, 1 ], [ 0, 255 ], mat ); 0, 0, stage.stageWidth, stage.stageHeight );;
addChild( bg );