質問

ここでは基本的なものがありません。円とチェックボックスを描く非常にシンプルなカスタムクラスがあり、チェックボックスがチェックされている場合にのみその円スプライトをドラッグすることができます。チェックボックスコンポーネントが私の.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()
役に立ちましたか?

解決

ここでの問題は、このクラスが利用できる前にステージにアクセスしようとしていることです。これを行う最良の方法は、event.added_to_stageのコンストラクターにイベントリスナーを追加することです。このイベントが発生したら、ステージに対してxとyを設定します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top