访问未定义的属性错误

时间:2013-04-23 00:19:36

标签: actionscript-3 flash

在我开始学习AS3之后,我遇到了很多错误(尝试动态地将十字准线添加到舞台而不是使用鼠标光标进行射击游戏):

Main.as, Line 13 1180: Call to a possibly undefined method addChild.
Main.as, Line 13 1120: Access of undefined property crosshair.
Main.as, Line 20 1120: Access of undefined property stage.
Main.as, Line 20 1120: Access of undefined property moveCursor.

1 个答案:

答案 0 :(得分:3)

你班上没有构造函数。

试试这个:

package  {
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.ui.Mouse;
    import flash.media.Sound;
    import flash.media.SoundChannel;
    public class Main extends MovieClip {

    public var crosshair:crosshair_mc;


    public function Main()
    {
        //This creates a new instance of the cursor movie clip and adds it onto
        //the stage using the addChild method.
        crosshair = new crosshair_mc();
        addChild(crosshair);

        //Hides the default cursor on the stage so it will not be shown.
        Mouse.hide();

        //Adds an event listener onto the stage with the enter frame event which
        //repeatedly executes the moveCursor function.
        stage.addEventListener(Event.ENTER_FRAME, moveCursor);
    }

    //This function set the x & y positions of the custom cursor to the x & y positions
    //of the default cursor.
    function moveCursor(event:Event) 
    {
      crosshair.x=mouseX;
      crosshair.y=mouseY;
     }
   }
}