Okay. I’m trying to make a popup alert modal window thing with an external class. I have a simple swf set up with just some random sample text, a rectangle, and a button (just so I can tell if it’s working or not). What I want to happen is for the “alert” class to initialize upon startup of the swf and then when I click the button (instance: btn), the alert will show up. If I get rid of all AS in the swf and set alert as the document class, it shows up just fine, so I think it should be working, but for some reason I get this instead when I try the button function:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at alert$cinit()
at global$init()
at alertTester_fla::MainTimeline/frame1()
Here’s the cote in my alertTester swf:
import alert;
alert.msgAlert = "This is me testing stuff out.";
btn.addEventListener(MouseEvent.CLICK, displayAlert);
function displayAlert(e:MouseEvent):void
{
alert.showAlert();
}
And here’s the code in alert.as:
package {
import flash.display.MovieClip;
import flash.display.*;
import flash.events.*;
import flashx.textLayout.formats.Float;
import flash.text.TextField;
import flash.geom.Matrix;
import flash.text.*;
public class alert extends MovieClip {
private static var stage:Stage = null;
private static var bkgd:Sprite;
private static var msgBox:Sprite;
private static var msg:TextField;
public static var msgAlert:String = "Test.";
//for alert box itself
public static var rectW:int = 200;
public static var rectH:int = 150;
private static var rectX:Number = (stage.stageWidth/2) - (rectW/2);
private static var rectY:Number = (stage.stageHeight/2) - (rectH/2);
public function alert() {
addEventListener(Event.ADDED_TO_STAGE, added);
}
private function added(event:Event):void {
init(stage);
}
private function init(stageRef:Stage):void
{
stage = stageRef;
trace("Initialized!");
}
public static function showAlert():void {
if (stage == null) {
trace("Alert class has not been initialized!");
return;
}
//initialize
bkgd = new Sprite();
msgBox = new MovieClip();
msg = new TextField();
//assign content
bkgd = createBkgd();
msgBox = createBox();
msgText(msgAlert);
//add children
msgBox.addChild(msg);
bkgd.addChild(msgBox);
stage.addChild(bkgd);
}
private static function createBkgd() : Sprite
{
//setup/initialize
var overlay:Sprite = new Sprite();
//make rectangle/cover stage
overlay.graphics.beginFill(0x929292);
overlay.graphics.drawRect(0,0,stage.stageWidth,stage.stageHeight);
overlay.graphics.endFill();
overlay.alpha = .85;
//return
return overlay;
}
private static function createBox() : Sprite
{
//setup/initialize necessary variables
var box:Sprite = new Sprite();
var colors:Array = new Array(0xFFFFFF, 0xE1E1E1);
var alphas:Array = new Array(1, 1);
var ratios:Array = new Array(0,125);
var mat:Matrix = new Matrix();
//make rectangle/alert box
mat.createGradientBox(rectW,rectH,(Math.PI/2),rectX,rectY);
box.graphics.beginGradientFill(GradientType.LINEAR, colors, alphas, ratios, mat);
box.graphics.drawRoundRect(rectX,rectY,rectW,rectH,25,25);
box.graphics.endFill();
//return
return box;
}
private static function msgText(alertMsg:String):void
{
//create formatter and font
var myFormat:TextFormat = new TextFormat();
var myFont = new Font();
//format formatter
myFormat.size = 14;
myFormat.align = TextFormatAlign.CENTER;
myFormat.font = "Arial";
//declare and assign message variable
var msgWords:String = alertMsg;
//set text field properties
msg.autoSize = TextFieldAutoSize.CENTER;
msg.defaultTextFormat = myFormat;
msg.background = false;
msg.border = false;
msg.selectable = false;
msg.type = TextFieldType.DYNAMIC;
msg.textColor = 0x000000;
msg.antiAliasType = AntiAliasType.ADVANCED;
msg.embedFonts = true;
msg.text = msgWords;
//set position
msg.x = rectX + rectW/2 - msg.width/2;
msg.y = rectY + rectH/5;
}
}
}
I originally had the “init” function as a public static function so I could do “alert.init(stage);” from the swf, but it gave me this same error.
Thanks in advance for any help you can offer. =\