【问题标题】:Actionscript Adding Custom Class To .flaActionscript 将自定义类添加到 .fla
【发布时间】:2011-01-24 22:58:00
【问题描述】:

我在这里遗漏了一些基本的东西。我有一个非常简单的自定义类,它绘制了一个圆圈和一个复选框,并且仅在选中复选框时才允许拖动该圆形精灵。一个复选框组件被手动添加到我的 .fla 中的库中。

来自我的 .fla 项目的操作面板:

var ball:DragBall = new DragBall();
addChild(ball);

我的自定义类 .as 文件(与 .swf 位于同一文件夹中)

package
{
import fl.controls.CheckBox;
import flash.display.Sprite;
import flash.events.MouseEvent;

public class DragBall extends Sprite
    {
    private var ball:Sprite;
    private var checkBox:CheckBox;

    public function DragBall():void
        {
        drawTheBall();
        makeCheckBox();
        assignEventHandlers();
        }

    private function drawTheBall():void
        {
        ball = new Sprite();
        ball.graphics.lineStyle();
        ball.graphics.beginFill(0xB9D5FF);
        ball.graphics.drawCircle(0, 0, 60);
        ball.graphics.endFill();
        ball.x = stage.stageWidth / 2 - ball.width / 2;
        ball.y = stage.stageHeight / 2 - ball.height / 2;
        ball.buttonMode = true;
        addChild(ball);
        }

    private function makeCheckBox():void
        {
        checkBox = new CheckBox();
        checkBox.x = 10;
        checkBox.y = stage.stageHeight - 30;
        checkBox.label = "Allow Drag";
        checkBox.selected = false;
        addChild(checkBox);
        }

    private function assignEventHandlers():void
        {
        ball.addEventListener(MouseEvent.MOUSE_DOWN, dragSprite);
        ball.addEventListener(MouseEvent.MOUSE_UP, dropSprite);
        }

    private function dragSprite(evt:MouseEvent):void
        {
        if (checkBox.selected) {ball.startDrag();}
        }

    private function dropSprite(evt:MouseEvent):void
        {
        if (checkBox.selected) {ball.stopDrag();}
        }
    }
}

从 .fla 编译会导致以下错误,我不明白

 TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at DragBall/drawTheBall()
    at DragBall()
    at DragBall_fla::MainTimeline/frame1()

【问题讨论】:

    标签: actionscript-3 class package


    【解决方案1】:

    这里的问题是你试图在这个类可用之前访问阶段。最好的方法是在 Event.ADDED_TO_STAGE 的构造函数中添加一个事件侦听器,然后在此事件发生后设置相对于舞台的 x 和 y。

    【讨论】:

    • 只是想补充一点,作为一种做法,您应该在您的 init/setup 函数中删除 ADDED_TO_STAGE 事件侦听器。据我了解,如果您不基于对象的重新设置等,此事件可能会被触发两次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-05
    • 2022-01-25
    • 2018-07-15
    • 2020-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多