我在这里缺少一些基本的东西。我有一个非常简单的自定义类,可以绘制一个圆圈和一个复选框,并且只有检查了复选框,才允许拖动该圆圈。手动将复选框组件添加到我的.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()
有帮助吗?

解决方案

这里的问题在于,您正在尝试在该课程可用之前访问舞台。做到这一点的最好方法是在构造函数中添加事件侦听器。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top