PDA

View Full Version : Preloading With External Classes (Willing to Pay $$)



hoyle_billy
April 30th, 2009, 01:09 PM
Hello, I'm having problems preloading my Man.swf file.

My Preloader.swf consists of this simple code:

var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent .PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLET E, done);
l.load(new URLRequest("Main.swf"));
function loop(e:ProgressEvent):void
{
var perc:Number = e.bytesLoaded / e.bytesTotal;
percent.text = Math.ceil(perc*100).toString();
}
function done(e:Event):void
{
removeChildAt(0);
percent = null;
addChild(l);
}

The problem has something to do with the fact that my Main.swf refrences a few external classes(ScrollBar.as, ScrollBox.as) that control some scrolling functionality.

When I run the Main.swf on its own, everything works pefectly. When I try to load the movie by launching the Preloader.swf file I get the following errors:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ScrollBar$iinit()
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at flash.display::MovieClip$iinit()
at ScrollBox$iinit()
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at flash.display::MovieClip$iinit()
at Main$iinit()
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at ScrollBox$iinit()
at flash.display::Sprite/flash.display:Sprite::constructChildren()
at flash.display::Sprite$iinit()
at flash.display::MovieClip$iinit()
at Main$iinit()

Please help!

DfKimera
April 30th, 2009, 01:44 PM
Something in the constructor function of the ScrollBar object is null when it shouldn't. Mind sharing that code with us?

cshaheen
April 30th, 2009, 02:45 PM
I am willing to bet $$ that you are trying to reference the STAGE in your scrollbar / scrollbox construction. Since you are loading this movie into another one, the movie won't be on the stage until after it's been completely loaded in. It works fine on its own, because it starts out on the stage. It's all about load order.

Best solution is to listen for Event.ADDED_TO_STAGE, and THEN perform any stage specific requirements.

hoyle_billy
April 30th, 2009, 03:05 PM
You guys are awesome, thanks so much for responding!

Cshaheen, sounds like you know exactly what the problem is. Here's the code for my ScrollBar.as

package
{
import flash.display.*;
import flash.events.*;

public class ScrollBar extends MovieClip
{
private var xOffset:Number;
private var xMin:Number;
private var xMax:Number;

//stage instances
public var track:MovieClip;
public var scrollBar:MovieClip;
public var scrollButton:MovieClip;
public var masker:MovieClip;
public var thumbnails:MovieClip;
public var interiorThumb:MovieClip;

public function ScrollBar():void
{
scrollButton.buttonMode = true;
xMin = 0;
xMax = track.width - scrollButton.width;
scrollButton.addEventListener(MouseEvent.MOUSE_OVE R, scrollButtonOver);
scrollButton.addEventListener(MouseEvent.MOUSE_OUT , scrollButtonOut);
scrollButton.addEventListener(MouseEvent.MOUSE_DOW N, scrollButtonDown);
stage.addEventListener(MouseEvent.MOUSE_UP, scrollButtonUp);
}

private function scrollButtonOver(e:MouseEvent): void
{
e.target.alpha = 1;
}

private function scrollButtonOut(e:MouseEvent): void
{
e.target.alpha = .6;
}

private function scrollButtonDown(e:MouseEvent): void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, scrollButtonMove);
xOffset = mouseX - scrollButton.x;
}
private function scrollButtonUp(e:MouseEvent): void
{
stage.removeEventListener(MouseEvent.MOUSE_MOVE, scrollButtonMove);
}
private function scrollButtonMove(e:MouseEvent): void
{
scrollButton.x = mouseX - xOffset;
if(scrollButton.x <= xMin)
scrollButton.x = xMin;
if(scrollButton.x >= xMax)
scrollButton.x = xMax;
dispatchEvent(new ScrollBarEvent(scrollButton.x / xMax ));
e.updateAfterEvent();
}
}
}


I'm not sure I know how to set this up to listen for the Event.ADDED_TO_STAGE. Sorry, I'm new to AS3.

cshaheen
April 30th, 2009, 03:30 PM
No problem. It's very easy. And you only need to worry about it during the construction phase. References to the stage in other functions aren't a problem, unless those functions are called during construction. Just change your constructor to this:
ActionScript Code:


public function ScrollBar():void
{
scrollButton.buttonMode = true;
xMin = 0;
xMax = track.width - scrollButton.width;
scrollButton.addEventListener(MouseEvent.MOUSE_OVE R, scrollButtonOver);
scrollButton.addEventListener(MouseEvent.MOUSE_OUT , scrollButtonOut);
scrollButton.addEventListener(MouseEvent.MOUSE_DOW N, scrollButtonDown);
addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
}





then add another function, called onAddedToStage, that does this:


private function onAddedToStage(e:Event):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, scrollButtonUp);
}



This is something you should do for ALL your classes that depend on being added to the stage. It's the root problem to tons of issues that people don't understand, so best to start making good habits.