Hello
I need a little help with adding and removing stage event listeners
I am relatively new to AS 3 and have been building a liquid layout site
unfortunately I do not have a strong grasp of classes and have been doing all of my scripting on the main timeline
with encapsulated script within MC’s.
basically I am calling on the same functions over and over
and those are mostly resize functions which I have added StageEventListeners
on the appropriate frames to listen for resize events on that stage
now as of now I have literally dozens of these scattered about the site.
when testing my site things are running fairly smoothly
however I keep getting error messages in my output window that go a little something like this.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ShawnConveyTestv3_fla::MainTimeline/setPos()
at ShawnConveyTestv3_fla::MainTimeline/resizeHandler1()
they vary as to which resize handler I have offended at that moment.
but my question is (in 2 parts)
-
what is the best way to add and remove listeners to the stage (cleanest and simplest)
-
assuming you are going to tell me that I will have to learn classes and packages, could anyone point me to a good online reference
for a crash course on these topics? I am sure that my life could be greatly simplified if I understood these key concepts, but for some
reason I have not been able to fully grasp them. ( this coding thing is a bit new to me, I am a visual artist by training)
any help would be greatly greatly appreciated
thanks
her is some sample code of the mess I have scripted myself in…
import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener (Event.RESIZE, resizeHandler); // ** invisible flex back **
stage.addEventListener (Event.RESIZE, resizeHandler1); // ** for Navigation / intro **
// --> Backgrounds for MC's set to invisable <-- //
body1_mc.invis_mc.alpha = 0;
body2_mc.invis_mc.alpha = 0;
body_mc.invis_mc.alpha = 0;
header_mc.invis_mc.alpha = 0;
footer_mc.invis_mc.alpha = 0;
dim_mc.alpha = 0;
// --> Initialize Flex-Sizing & Original Stage positions <-- //
resizeHandler (null);
function resizeHandler (event:Event):void {
var sw:Number = stage.stageWidth;
var sh:Number = stage.stageHeight;
header_mc.height = 30;
footer_mc.height = 100;
body1_mc.width = 15;
body2_mc.width = 15;
header_mc.x = 0;
header_mc.y = 0;
body_mc.x = 15;
body_mc.y = 30;
body1_mc.x = 0;
body1_mc.y = 30;
body2_mc.x = sw-body2_mc.width;
body2_mc.y = 30;
footer_mc.x = 0;
header_mc.width = footer_mc.width = sw;
body_mc.width = sw-body1_mc.width-body2_mc.width;
body_mc.height = sh-header_mc.height-footer_mc.height;
body1_mc.height = sh-header_mc.height-footer_mc.height;
body2_mc.height = sh-header_mc.height-footer_mc.height;
footer_mc.y = sh-footer_mc.height;
// --> controls the dim curtain for gallery viewing pleasure <-- //
dim_mc.x = stage.stageWidth / 2;
dim_mc.y = stage.stageHeight / 2;
dim_mc.width = stage.stageWidth;
dim_mc.height = stage.stageHeight;
}
function setPos() {
////////////////////////////////////////////////////////////////////////////////////////////
// -> center registration
////////////////////////////////////////////////////////////////////////////////////////////
preBack.x = stage.stageWidth / 2;
preBack.y = stage.stageHeight / 2;
preBack.width = stage.stageWidth;
preBack.height = stage.stageHeight;
preBack.scaleX <= preBack.scaleY ? (preBack.scaleX = preBack.scaleY) : (preBack.scaleY = preBack.scaleX);
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BL-> Bottom Left registration
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
buttons.x=15;
buttons.y=stage.stageHeight-25;
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// BR -> bottom Right registration
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
br_btn.x=stage.stageWidth-110;
br_btn.y=stage.stageHeight-25;
///////////////////////////////////////////////////////////////////////////////////
// TR -> Top Right registration
///////////////////////////////////////////////////////////////////////////////////
intro2_mc.x=stage.stageWidth-125;
intro2_mc.y=40;
}
setPos();
function resizeHandler1(event:Event):void {
setPos();
//setBackground();
}
///////////////
/// contact ///
//////////////
br_btn.email_btn.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler2);
function mouseDownHandler2(event:MouseEvent):void {
navigateToURL(new URLRequest("mailto:shawn@me.com"),"_self" );
}
buttons.addEventListener(MouseEvent.CLICK, btnClick);
var btnName = "";
function btnClick(event:MouseEvent):void
{
btnName = event.target.name;
//gotoAndStop(3);
gotoAndStop(btnName);
}
stop();
stage.addEventListener (Event.RESIZE, resizeHandler2); // **for SSP gallery #2 (still)**
stage.addEventListener (Event.RESIZE, sSubHandler);
// --> Initialize Flex-Sizing & Original Stage positions **for SSP gallery #2 (still)** <-- //
resizeHandler2 (null);
function resizeHandler2 (event:Event):void
{
ssp1.x = 15;
ssp1.y = 30;
ssp1.width = stage.stageWidth-body1_mc.width-body2_mc.width;
ssp1.height = stage.stageHeight-header_mc.height-footer_mc.height;
}
sSubHandler (null);
function sSubHandler (event:Event):void
{
sSub_mc.x=30;
sSub_mc.y=stage.stageHeight-50;
}
dim_mc.alpha = 0;
dim_mc.addEventListener(Event.ENTER_FRAME, dimmer);
function dimmer(event:Event):void
{
dim_mc.alpha += .01;
if(dim_mc.alpha >= .8)
{
dim_mc.alpha = .8;
//dim_mc.removeEventListener(Event.ENTER_FRAME, dimmer);
}
}
sSub_mc.addEventListener(MouseEvent.CLICK, sSubClick);
var sSubBtnName = "";
function sSubClick(event:MouseEvent):void
{
sSubBtnName = event.target.name;
//gotoAndStop(3);
gotoAndStop(sSubBtnName);
}
thanks for looking
you can also see the development site at http://www.shawnconvey.com/dev5