AS3鼠标不起作用

时间:2013-08-19 15:12:24

标签: actionscript-3 mouseevent

我有一个已添加到舞台的按钮。创建时,它会移动到舞台的可见区域,并调用activate函数,该函数会向其添加一个查找鼠标的事件侦听器。但是,出于某种原因,这不起作用。任何想法为什么?我尝试通过创建它的对象添加一个侦听器,但这也不起作用。

    package menus {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.*;

public class MenuPlayButton extends MovieClip {
    private var _stageWidth, _stageHeight:int;
    private var comeInTimer:Timer;
    private var buttonSpeed:Number;

    public function MenuPlayButton(stageWidth:int, stageHeight:int) {
        _stageWidth = stageWidth;
        _stageHeight = stageHeight;

        alpha = 1;
        rescaleMe();
        repositionMe();
        comeIntoMenu();
    }

    private function rescaleMe():void {
        var oldWidth = this.width;
        var oldHeight = this.height;

        this.height = _stageHeight/10;
        this.width = (this.height * oldWidth) / oldHeight;
    }

    private function repositionMe():void {
        this.x = 0 - this.width;
        this.y = _stageHeight * 0.56;
    }

    private function comeIntoMenu():void {
        //Sets button's original speed
        buttonSpeed = _stageHeight / 40;

        //Adds timer that moves in the button
        comeInTimer = new Timer(10,0);
        comeInTimer.addEventListener(TimerEvent.TIMER, comeInTimerListener);
        comeInTimer.start();
    }

    private function comeInTimerListener(e:TimerEvent):void {
        if(x < 0) {
            x += buttonSpeed;
            buttonSpeed *= 0.93;
        } else {
            x = 0;
            activate();
        }
    }

    private function activate():void {
        //Kills off timer
        comeInTimer.removeEventListener(TimerEvent.TIMER, comeInTimerListener);
        comeInTimer.stop();
        comeInTimer = null;
        this.addEventListener(MouseEvent.MOUSE_DOWN, clicked);
        trace("Button should be activated"); //This gets traced
    }

    function clicked(e:MouseEvent) {
        trace("Button pressed");
    }
}

}

1 个答案:

答案 0 :(得分:0)

首先点击的功能必须无效!尝试将代码更改为

This.addEventListener(MouseEvent.CLICK,clicked):void; {

然后

单击

函数(e:MouseEvente.CLICK):void; {