1
我在這裏丟失了一些基本信息。我有一個非常簡單的自定義類,它繪製了一個圓圈和一個複選框,並且只有在複選框被選中時才允許拖動該圓形精靈。在我的.fla中手動添加一個複選框組件。Actionscript將自定義類添加到.fla
var ball:DragBall = new DragBall();
addChild(ball);
我的自定義類。至於文件(位於同一文件夾中的.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編譯:
從我的.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()
啊,當然!感謝這一點。 – TheDarkIn1978
只是想補充一點,作爲練習,您應該刪除init/setup函數中的ADDED_TO_STAGE事件偵聽器。根據我的理解,如果您不基於對象等的重新註冊,此事件可能會被解僱兩次。 – WillyCornbread