Monday, 19 August 2013

AS3 Mouse down not working

AS3 Mouse down not working

I have a button that has been added to the stage. When it is created, it
moves into the visible area of the stage, and calls an activate function,
which adds an event listener to it that looks for mouse down. For some
reason, however, this does not work. Any ideas as to why? I've tried
adding a listener to the object via the one that created it, but that
doesn't work either.
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");
}
}
}

No comments:

Post a Comment