我有一个已添加到舞台的按钮。当它被创建时,它会移动到舞台的可见区域,并调用一个激活函数,该函数会为其添加一个事件侦听器来查找鼠标按下。但是,由于某种原因,这不起作用。关于为什么的任何想法?我已经尝试通过创建它的对象向对象添加一个侦听器,但这也不起作用。
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");
}
}
}