Hey guys, hoping for some help with this :( been stuck on it for a couple days now.
I'm creating a ScrollBar using Lee Brimelow's ScrollBar class. I've had to modify it to work inside of my Class files and think I'm on the right track, but I'm getting the dreaded:
Error #1009: Cannot access a property or method of a null object reference error.
When I run debug, it hits on the line where I have rollerUp);
CODE FROM SCROLLBAR CLASS:
import flash.display.*;
import flash.events.*;
import caurina.transitions.*;
public class ScrollBar extends MovieClip
{
private var yOffset:Number;
private var yMin:Number;
private var yMax:Number;
private var thumbsnailTab:MovieClip;
private var theRoller:MovieClip;
public function ScrollBar(myRoller:MovieClip, myTrack:MovieClip, thumbsnails:MovieClip):void
{
yMin = 0;
yMax = myTrack.height - myRoller.height;
theRoller = myRoller;
thumbsnailTab = thumbsnails;
my开发者_开发知识库Roller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
}
This is what my debug is showing me:
At first I wasn't sure if it was the stage reference that is causing the error or the rollerUp function, but since I commented out the stage.removeEventListener and added a basic trace statement it still throws up an error so I believe it has something to do with:
stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
Now I have imported events.*; to the ScrollBar class... maybe the problem is in my main class where I create the graphics for the ScrollBar as well as add the ScrollBar to the display list?
CODE FROM MAIN CLASS:
// Creating Graphics
track1 = new Track;
track1.x = 0;
track1.y = 0;
roller1 = new Roller;
roller1.x = 0;
roller1.y = 0;
sc1 = new EmptyMov;
sc1.x = 764;
sc1.y = 470;
sc1.addChild(track1);
sc1.addChild(roller1);
// Adding ScrollBar to Stage
scroll1 = new ScrollBar(roller1, track1, tab1);
container.addChild(sc1);
container.addChild(scroll1);
addChild(container);
I'm stuck here, not sure why I'm getting that Null reference error, as well as not sure if I'm creating the graphics the right way as well as using the ScrollBar class correctly :( any tips appreciated!
Update Code Working! :D
public function ScrollBar(myRoller:MovieClip, myTrack:MovieClip, thumbsnails:MovieClip):void
{
yMin = 0;
yMax = myTrack.height - myRoller.height;
theRoller = myRoller;
thumbsnailTab = thumbsnails;
myRoller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
}
private function rollerDown(e:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
stage.addEventListener(MouseEvent.MOUSE_MOVE, rollerMove);
yOffset = mouseY - theRoller.y;
}
Your stage is null because the newly created object is not yet on the stage. To get around this, use an ADDED_TO_STAGE listener in the constructor which then adds your stage events.
I think your problem is that stage=null. The reason can by only one: you trying to get to stage, when the MC is not added to it (then the reference is null).
I agree with Konrad, also.. it is better practice to initiate the MOUSE_UP event when the MOUSE_DOWN event is captured.
so move the
stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
inside of the rollerDown function.
精彩评论