帮助 - 使用 stage.addEventListener (ScrollBar) 时出现空对象错误

发布于 2024-08-07 04:46:39 字数 2829 浏览 2 评论 0原文

嘿伙计们,希望得到一些帮助:(已经坚持了几天了。

我正在使用 Lee Brimelow 的 ScrollBar 类 我必须修改它才能在我的 Class 文件中工作,并认为我走在正确的轨道上,但我遇到了可怕的:

错误 #1009:无法访问空对象引用的属性或方法错误。

当我运行调试时,它出现在我有 rollerUp);

代码来自 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;
        myRoller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
    }

This 的 位置。是我的调试向我显示的内容:

alt text

起初我不确定这是否是舞台导致错误或 rollUp 函数的引用,但由于我注释掉了 stage.removeEventListener 并添加了基本跟踪语句,它仍然会抛出错误,所以我相信它与以下内容有关:

stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);

现在我已导入事件。*;到 ScrollBar 类...也许问题出在我的主类中,我为 ScrollBar 创建图形并将 ScrollBar 添加到显示列表?

来自主类的代码:

// 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);

我被困在这里,不确定为什么会出现空引用错误,也不确定我是否以正确的方式创建图形以及正确使用 ScrollBar 类:(任何提示感谢!

alt text


更新代码工作: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;
    }

替代文字

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;
        myRoller.addEventListener(MouseEvent.MOUSE_DOWN, rollerDown);
        stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);
    }

This is what my debug is showing me:

alt text

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!

alt text


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;
    }

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

当爱已成负担 2024-08-14 04:46:39

您的舞台为空,因为新创建的对象尚未出现在舞台上。为了解决这个问题,请在构造函数中使用 ADDED_TO_STAGE 侦听器,然后添加舞台事件。

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.

世界如花海般美丽 2024-08-14 04:46:39

我认为你的问题是 stage=null 。原因只有一个:您试图进入舞台,但 MC 未添加到舞台(则引用为空)。

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).

白昼 2024-08-14 04:46:39

我也同意 Konrad 的观点.. 更好的做法是在捕获 MOUSE_DOWN 事件时启动 MOUSE_UP 事件。

所以移动

stage.addEventListener(MouseEvent.MOUSE_UP, rollerUp);

rollerDown函数的内部。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文