I Asked a similar question yesterday however I am apparently still confused. Help would be appreciated.
In class Main I am instantiating a custom class NavLabel that creates a textField. With in NavLabel I dispatch a custom event CustomEventWithParams on a reference to class Main that passes the parameter theLabelWidth along with it. from with in class Main i can trace the
theLabelWidth for each of the instantiated NavLabel objects. I want to hold those values in an array labelWidthArray and then use them to position the NavLabel objects. how can I do this?
Thanks,
class Main
package
{
import flash.display.*;
import flash.events.*;
public class Main extends MovieClip{
private var label1:NavLable;
private var label2:NavLable;
public var labelWidthArray:Array;
public function Main()
{
addEventListener(CustomEventWithParams.LABEL_WIDTH, widthFromParam);
init();
}
function init(){
label1 = new NavLable(this,"label1Text");
label2 = new NavLable(this,"label2");
}
function widthFromParam(e:CustomEventWithParams):void{
trace(e.params.param);// this traces "55" "88"
labelWidthArray.push(e.params.param); //Cannot access a property or method of a null object reference.
}
class NavLabel
package {
import com.greensock.loading.*;
import com.greensock.events.*;
import flash.display.*;
import flash.text.*;
import CustomEventWithParams;
public class NavLabel extends Sprite{
private var theLabel:TextField;
private var _theText:String;
private var txtFmt:TextFormat;
private var loader:SWFLoader;
public var theLabelWidth:int;
public var main:Main;
public function NavLabel(main,theText:String) {
_theText = theText;
this.main = main
loader = new SWFLoader("fonts/fonts.swf", {onComplete:completeHandler});
loader.load();
}
function completeHandler(event:LoaderEvent):void {
var fontClass:Class = loader.getClass("TheCantarell");
Font.registerFont(fontClass);
txtFmt= new TextFormat();
txtFmt.font = "Cantarell";
txtFmt.bold = true;
txtFmt.size = 15;
theLabel = new TextField();
theLabel.mouseEnabled = false;
theLabel.text = _theText;
theLabel.setTextFormat(txtFmt);
theLabel.autoSize = TextFieldAutoSize.LEFT;
theLabel.border = true;
theLabelWidth = theLabel.width;
var evt:CustomEventWithParams = new CustomEventWithParams(CustomEventWithParams.LABEL_WIDTH, {param:theLabelWidth});
main.dispatchEvent(evt);
this.addChild(theLabel);
}
public function get getLabelWidth():String {
return _theText
}
}
}
CustomEvent Class
package
{
import flash.events.Event;
public class CustomEventWithParams extends Event{
public static const LABEL_WIDTH:String = "LabelWidth";
public var params:Object;
public function CustomEventWithParams(type:String, params:Object, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
this.params = params;
}
public override function clone():Event
{
return new CustomEventWithParams(type, this.params, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("CustomEventWithParams", "params", "type", "bubbles", "cancelable");
}
}
}