Flash AS3从外部actionscript控制按钮错误

时间:2013-04-30 08:08:45

标签: actionscript-3 flash button actionscript

帮助。基本上我在不同的框架有2个按钮。如果第1帧上的按钮被点击,它将转到并停在第2帧。如果第2帧上的按钮被点击,它将转到并停在第1帧。我想要做的是控制此按钮而不是外部动作文件。第一个按钮运行没有问题,而第二个按钮似乎没有正确响应,并有此错误消息:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at src::Main/init()
    at src::Main()

以下是代码:

package src 
{
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.MovieClip;

/**
 * ...
 * @author vimoetz
 */
public class Main extends MovieClip 
{

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        this.gotoAndStop("1");
        button1.addEventListener(MouseEvent.CLICK, gotoFrame2);
        button2.addEventListener(MouseEvent.CLICK, gotoFrame1);
    }

    public function gotoFrame2 (e:MouseEvent)
    {
        this.gotoAndStop("2");
    }

    public function gotoFrame1 (e:MouseEvent)
    {
        this.gotoAndStop("1");
    }

}

}

1 个答案:

答案 0 :(得分:1)

您需要从init功能中删除此行:

button2.addEventListener(MouseEvent.CLICK, gotoFrame1);

并且函数gotoFrame2改变如下:

public function gotoFrame2 (e:MouseEvent)
    {
      this.gotoAndStop("2");
      if (!button2.hasEventListener(MouseEvent.CLICK)){
        button2.addEventListener(MouseEvent.CLICK, gotoFrame1);
      }
    }