Not just Actionscript noodlings

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….

05 Jul

Which loader?

I just found this useful peice of infomation from LiveDocs:

The Loader class is used to load SWF files or image (JPG, PNG, or GIF) files.

Use the URLLoader class to load text or binary data.

The URLLoader class downloads data from a URL as text, binary data, or URL-encoded variables. It is useful for downloading text files, XML, or other information to be used in a dynamic, data-driven application.

I was unaware until now that using URLLoader to load XML data was the preferred option, now to go back through some projects and make the change.

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.