2010-03-08 38 views
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() 

回答

2

这里的问题是,你正在尝试在这个课程可用之前,先学习这个阶段。最好的方法是在Event.ADDED_TO_STAGE的构造函数中添加一个Event监听器,然后一旦发生该事件,就相对于舞台设置x和y。

+0

啊,当然!感谢这一点。 – TheDarkIn1978

+0

只是想补充一点,作为练习,您应该删除init/setup函数中的ADDED_TO_STAGE事件侦听器。根据我的理解,如果您不基于对象等的重新注册,此事件可能会被解雇两次。 – WillyCornbread