AS3 add/remove Event Listeners within functions...
Dear All, Hello!
What I am trying to do is, if a user presses keyboard (key – 1), picture 1 should FadeIn and if a user presses keyboard (key – 2) picture one should FadeOut and picture 2 FadeIn and vice-versa. With the code mentioned below it works fine sometimes if I am not pressing any other key while it is performing one action but sometimes it behaves weird also. What I want AS3 to do is not to take any action or in other words it should not listen to keyboard until it finishes the opening or closing animation of a picture or any other object assigned to the key.
Any help or assistance will be greatly appriciated.
stage.addEventListener (KeyboardEvent.KEY_DOWN, KeyDownHandler);/**/
//keyboard keycode for 1 & 2
var key1:uint = 49;
var key2:uint = 50;
var BG1:Image1 = new Image1();
var BG2:Image2 = new Image2();
function KeyDownHandler (keyEvent:KeyboardEvent):void
{
var character:String = String.fromCharCode(keyEvent.charCode);
if (keyEvent.keyCode == key1)
{
var BG1TweenOff:Tween = new Tween(BG1,"alpha",Strong.easeOut,1,0,2,true);
var BG2TweenOff:Tween = new Tween(BG2,"alpha",Strong.easeIn,1,0,2,true);
BG1TweenOff.addEventListener(TweenEvent.MOTION_FINISH, BG1ON);
trace ("Key pressed "+character+".");
}
else if (keyEvent.keyCode == key2)
{
var BG1offTween:Tween = new Tween(BG1,"alpha",Strong.easeOut,1,0,2,true);
var BG2offTween:Tween = new Tween(BG2,"alpha",Strong.easeIn,1,0,2,true);
BG2offTween.addEventListener(TweenEvent.MOTION_FINISH, BG2ON);
trace ("Key pressed "+character+".");
}
else
{
gotoAndStop (currentFrame);
trace ("Key pressed "+character+".");
}
}
function BG1ON(eve:TweenEvent):void
{
BG1.x = 0;
BG1.y = 0;
addChild (BG1);
var BG1offTween:Tween = new Tween(BG1,"alpha",Strong.easeIn,0,1,2,true);
}
function BG2ON(eve:TweenEvent):void
{
BG2.x = 0;
BG2.y = 0;
addChild (BG2);
var BG2offTween:Tween = new Tween(BG2,"alpha",Strong.easeIn,0,1,2,true);
}
