removeEventListener not working - actionscript-3

function Drag(event:MouseEvent):void {
if ((event.target.parent == InventoryMenu) && (event.target is item)) {
var picked:item = item(event.target);
stage.addEventListener(MouseEvent.MOUSE_UP, Drop);
InventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
InventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));
function Drop(event:MouseEvent):void {
if ((event.target.parent == InventoryMenu) && (event.target is item)) {
var dropped:item = item(event.target);
if ((event.target is item) && (event.target.parent == InventoryMenu)) {
if (picked.itemdata("workswith") == dropped.name) {
var itemname:item = item(FetchResult(picked, dropped));
itemname.addChild(itemname.itemdata("filename"));
InventoryMenu.removeChild(picked);
InventoryMenu.removeChild(dropped);
InventoryMenu.addChild(itemname);
InventoryUpdate();
} else if (picked.name != dropped.name) {
trace("No son compatibles");
}
stage.removeEventListener(MouseEvent.MOUSE_UP, Drop);
InventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("down"));
InventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, InventoryNav("up"));
}
}
}
}
}
For some reason the removeEventListener on InventoryArrowDown and InventoryArrowUp isn't working. I'm fairly sure the route is correct as it's a direct copy paste from the addEventListener and it uses no variables.
Any clue what's wrong?

Hard to help you without seeing the code of InventoryNav but maybe the issue is that you should remove the event listeners before your tests.
Also, you should write two different handlers instead of using one and passing an argument like you do.
Here is a modified version of your code that might help:
private function drag(event:MouseEvent):void {
if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
var picked:Item = Item(event.target);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
inventoryArrowDown.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
inventoryArrowUp.addEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);
}
}
private function drop(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_UP, drop);
inventoryArrowDown.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavDown);
inventoryArrowUp.removeEventListener(MouseEvent.MOUSE_OVER, inventoryNavUp);
if ((event.target.parent == inventoryMenu) && (event.target is Item)) {
var dropped:Item = Item(event.target);
if ((event.target is Item) && (event.target.parent == inventoryMenu)) {
if (picked.itemdata("workswith") == dropped.name) {
var itemname:Item = Item(fetchResult(picked, dropped));
itemname.addChild(itemname.itemdata("filename"));
inventoryMenu.removeChild(picked);
inventoryMenu.removeChild(dropped);
inventoryMenu.addChild(itemname);
inventoryUpdate();
} else if (picked.name != dropped.name) {
trace("No compatible sons");
}
}
}
}

Related

EventListener for key combinations in AS3?

im tryin to figure out condition AND for "shortcut" for quitting standalone app from flash. I would like to push 2 keys and combination of these two keys "C+M" should quit my app.
Heres my code but its still not working. I tryed to make shure that app allow me to push multiple buttons at the same time and after that I created the function for quitting.
Any answers be great.
var keyPressedC:Boolean;
var keyPressedM:Boolean;
addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);
function check_keys(event:Event):void
{
if(keyPressedC)
trace("pushed C")
if(keyPressedM)
trace("pushed M")
}
function check_key_down(event:KeyboardEvent):void
{
if(event.keyCode == 67)
keyPressedC = true;
if(event.keyCode == 77)
keyPressedM = true;
}
function check_key_up(event:KeyboardEvent):void
{
if(event.keyCode == 67)
keyPressedC = false;
if(event.keyCode == 77)
keyPressedM = false;
}
import flash.system.fscommand;
stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
function enterKeyHandlercm(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.C && event.keyCode == Keyboard.M)
{
fscommand("quit");
}
}
Edited, still not working:
var keyPressedC:Boolean;
var keyPressedM:Boolean;
addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);
function check_keys(event:Event):void
{
if(keyPressedC)
trace("pushed C")
if(keyPressedM)
trace("pushed M")
}
function check_key_down(event:KeyboardEvent):void
{
if(event.keyCode == 67)
keyPressedC = true;
if(event.keyCode == 77)
keyPressedM = true;
}
function check_key_up(event:KeyboardEvent):void
{
if(event.keyCode == 67)
keyPressedC = false;
if(event.keyCode == 77)
keyPressedM = false;
}
import flash.system.fscommand;
stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
function enterKeyHandlercm(event:KeyboardEvent):void
{
if (keyPressedM == true && keyPressedC == true)
{
fscommand("quit");
}
}
In your enterKeyHandlercm block, your logic should be evaluating the keypressed value, not the keyCode value.
function enterKeyHandlercm(event:KeyboardEvent):void
{
if (keyPressedM == true && keyPressedC == true)
{
fscommand("quit");
}
}
With this code, a different MC is added for each of your 5 key possibilities (c up, c down , m up, m down, c+m down).
package {
import flash.display.*;
import flash.events.*;
import flash.system.fscommand;
import flash.system.System; //add this if you try System.exit(0);
public class FlashTest extends MovieClip
{
public function FlashTest()
{
var keyPressedC:Boolean;
var keyPressedM:Boolean;
// need to add eventListener to stage
// default values work fine.
stage.addEventListener(KeyboardEvent.KEY_DOWN, check_key_down);
stage.addEventListener(KeyboardEvent.KEY_UP, check_key_up);
stage.addEventListener(Event.ENTER_FRAME, check_keys);
function check_key_down(event:KeyboardEvent):void
{
if(event.keyCode == 67)
{
keyPressedC = true;
newBall(-100);
}
if(event.keyCode == 77)
{
keyPressedM = true;
newBall();
}
}
function check_key_up(event:KeyboardEvent):void
{
if(event.keyCode == 67)
{
keyPressedC = false;
newBall(-50);
}
if(event.keyCode == 77)
{
keyPressedM = false;
newBall(50);
}
}
function enterKeyHandlercm(event:KeyboardEvent):void
{
if (keyPressedM == true && keyPressedC == true)
{
newBall(100);
fscommand("quit");
// or try System.exit(0);
}
}
function newBall(x:Number=0):void
{
var ball:Sprite = new Sprite();
ball.graphics.lineStyle();
ball.graphics.beginFill(0x000000);
ball.graphics.drawCircle(0,0,20);
ball.graphics.endFill();
addChild(ball);
ball.x = stage.stageWidth/2+x;
ball.y = stage.stageHeight/2;
}
}
}
}
Please forgive my verbosity, but this way we aren't missing anything. The reason I added the ball constructor was because I only have my laptop with me so I had to use an online IDE and I don't know how to find an output window or run a debugger and it doesn't take system commands. But what I can confirm with the ball method is that when "c" and "m" are pressed together, a unique MC is instantiated. This means our code now causes flash to register a unique event when both keys are simultaneously pressed.

AS3 : EventListener won't be removed in an [IF]

I have searched how to pass arguments through EventListeners, and I used the method without calling an anonymous function to remove the EventListener later.
The issue is that the EventListener will be removed if out the IF function, but not if it is in the IF function.
How could I do that ?
The code :
function dragShip(m:MouseEvent):void
{
var func:Function = dispositionShip(m.target);
if (isDragging == false)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, func);
m.target.startDrag(true);
isDragging = true;
}
else
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, func);
isDragging = false;
placeShip(m.target , mouseX , mouseY , m.target.rotation);
}
// if the EventListener is put here, it gets removed, but not if put just in the else
}
NOTE : dispositionShip()returns a function.
Edit : Here is the following part of the code :
function dispositionShip(shipTarg):Function
{
return function(k:KeyboardEvent):void
{
rotateShip(k,shipTarg);
};
}
function rotateShip(k:KeyboardEvent,ship:Object):void
{
if (k.keyCode == 39)
{
ship.rotation += 90;
}
else if (k.keyCode == 37)
{
ship.rotation -= 90;
}
}
Moreover, if I replace rotateShip(k,shipTarg); by a simple trace, it also does not work.
Everytime you call
function dispositionShip(shipTarg):Function
{
return function(k:KeyboardEvent):void
{
rotateShip(k,shipTarg);
};
}
You're creating a new anonymous Object of type Function that calls rotateShip(), so when you call stage.removeEventListener(KeyboardEvent.KEY_DOWN, func); your func is a different Object to the func you passed into addEventListener(), so it doesn't match the orginal listener and doesn't get removed.
A better way to do this would be to store the current mouse target in a member var. IE:
var currentShip:Object;
function dragShip(m:MouseEvent):void
{
if (isDragging == false)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress);
m.target.startDrag(true);
isDragging = true;
currentShip = m.target;
}
else
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPress);
isDragging = false;
placeShip(m.target , mouseX , mouseY , m.target.rotation);
currentShip = null;
}
}
function keyPress(k:KeyboardEvent):void
{
rotateShip(k,currentShip);
}

Presets with actionscript 3.0

This is the presets variables and to nav with multiple selections, the code below is not really correct and functions perfectly without errors, however it does not work.
var aClicked:Boolean = false;
var yyClicked:Boolean = false;
var tClicked:Boolean = false;
var oClicked:Boolean = false;
a.addEventListener(MouseEvent.CLICK, gotosomething1);
function gotosomething1(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateT();
}
yy.addEventListener(MouseEvent.CLICK, gotosomething33);
function gotosomething33(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateT();
}
o.addEventListener(MouseEvent.CLICK, gotosomethinggg);
function gotosomethinggg(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateT();
}
t.addEventListener(MouseEvent.CLICK, gotosomething99);
function gotosomething99(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateT();
}
function activateT()
{
if (aClicked && yyClicked && oClicked)
{
t.addEventListener(MouseEvent.CLICK, gotosomething99);
}
}
yy.addEventListener(MouseEvent.CLICK, gogogo);
function gogogo(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateYY();
}
t.addEventListener(MouseEvent.CLICK, gotosomethingplease);
function gotosomethingplease(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateYY();
}
o.addEventListener(MouseEvent.CLICK, gotoi);
function gotoi(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateYY();
}
a.addEventListener(MouseEvent.CLICK, millionare);
function millionare(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateYY();
}
function activateYY()
{
if (aClicked && tClicked && oClicked)
{
yy.addEventListener(MouseEvent.CLICK, gogogo);
}
}
t.addEventListener(MouseEvent.CLICK, gp1);
function gp1(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateA();
}
a.addEventListener(MouseEvent.CLICK, gp2);
function gp2(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateA();
}
o.addEventListener(MouseEvent.CLICK, gp3);
function gp3(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateA();
}
yy.addEventListener(MouseEvent.CLICK, gp4);
function gp4(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateA();
}
function activateA()
{
if (yyClicked && tClicked && oClicked)
{
a.addEventListener(MouseEvent.CLICK, gp2);
}
}
o.addEventListener(MouseEvent.CLICK, ooo);
function ooo(event:MouseEvent):void
{
gotoAndStop(89);
oClicked = true;
activateO();
}
t.addEventListener(MouseEvent.CLICK, ttt);
function ttt(event:MouseEvent):void
{
gotoAndStop(89);
tClicked = true;
activateO();
}
a.addEventListener(MouseEvent.CLICK, aaa);
function aaa(event:MouseEvent):void
{
gotoAndStop(89);
aClicked = true;
activateO();
}
yy.addEventListener(MouseEvent.CLICK, yyy);
function yyy(event:MouseEvent):void
{
gotoAndStop(89);
yyClicked = true;
activateO();
}
function activateO()
{
if (yyClicked && tClicked && aClicked)
{
o.addEventListener(MouseEvent.CLICK, ooo);
}
}
This should work for what you are looking for:
import flash.events.MouseEvent;
import flash.events.Event;
var btnStates:Array = [false]; // Track states for each button - works w/ any # of btns
var btnCounter:int = 0; // Count each Active button
var btnActivateNumber = 4; // The number of buttons
this.addEventListener(MouseEvent.CLICK, btnClick); // Click on Anything Event
function btnClick(e:Event):void
{
var btnIndex = e.target.parent.getChildIndex(e.target); // Btn Index
trace("BtnIndex:", btnIndex); // Make Sure btns are are children of same parent
switch(e.target.name) // Check the name of item Clicked
{
case "a": // Checks that you clicked on button and not something else
case "yy": // Each of these cases could be done seperately
case "t": // Use descriptive names when possible
case "o": // Add additional buttons to track if needed
if(!btnStates[btnIndex]) // Btn not pressed before
{
btnCounter++; // Another Button Turned On
btnStates[btnIndex] = true; // Track Btn State
// Add Specific code related to button ie. Animation etc.
e.target.alpha = .75 // lighten Button
}
else // Use if you want the button to turn off on 2nd press
{
btnCounter--; // Button Turned Off
btnStates[btnIndex] = false; // Btn State Reset for this btn
}
break;
default:
trace("OtherItemClicked:", e.target.name); // Something else was clicked
// If you are getting wrong item when you click on your button
// You may have a child item in your button you will need to disable.
// set btnName.mouseChildren = false; or item.mouseEnabled = false;
break;
}
if(btnCounter == btnActivateNumber) // Test if enough buttons are pressed.
{
this.removeEventListener(MouseEvent.CLICK, btnClick); // CleanUp
trace(btnActivateNumber,"Buttons Activated");
// Place Your Navigation Action Code Here
}
}
Your question is a bit unclear. Do you want an action to happen when the 3rd button is clicked, or the fourth?
In any case, you can create a dictionary that has boolean values for each button. The key is the button, the value is set to true when that button is clicked. You can then count how many keys are positive in that dictionary to find out when "enough" have been clicked.

to create quiz using StartDrag and stopDrag

Actually i have a quiz.fla. In the file ,two of them fill inthe blank questions and others are
multiple questions.
square1_mc must run only once not twice. İf user correct selected, doesnt run it again.
However,if mybadscoretext is 1 not increase 2,3,4. :S
how i can do all?
stop();
var myScore:Number = 0;
var myBadScore:Number=0;
square1_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
stage.addEventListener(MouseEvent.MOUSE_UP, drop);
function drag(e:MouseEvent):void
{
e.target.startDrag();
}
function drop(e:MouseEvent):void
{
square1_mc.stopDrag();
if (square1_mc.hitTestObject(square2_mc)== true)
{
square1_mc.x=129;
square1_mc.y=133;
this.graphics.clear();
this.graphics.lineStyle(2, 000000);
this.graphics.moveTo(square1_mc.x, square1_mc.y);
this.graphics.lineTo(square2_mc.x, square2_mc.y);
this.graphics.endFill();
myScore += 1;
score_txt.text=String(myScore);
square1_mc.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
}
else
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
}
}
Add a variable to keep track of whether the bad score has been added:
var badMarked:Boolean = false;
function drag(e:MouseEvent):void
{
e.target.startDrag();
badMarked = false;
}
[...]
function drop(e:MouseEvent):void
{
if (square1_mc.hitTestObject(square2_mc)== true)
{
[...]
}
else if ( !badMarked )
{
square1_mc.x=129;
square1_mc.y=133;
myBadScore += 1;
mybadscore_txt.text=String(myBadScore);
badMarked = true;
}
}

Actionscript 3 mouse_over play movie, mouse_out reverse movie

I'm trying to make some flash buttons with a mouse_over animation that plays in reverse on mouse_out. Now I have it working for one of my three movie clip instances.
I am using e.currentTarget.play() instead of having a function for each movie clip, but how do I do the same for my playReverse function? I tried putting e.currentTarget.prevFrame() instead of mc1.prevFrame() but it did not work. My code is as follows:
mc1.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc2.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc3.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc1.addEventListener(MouseEvent.MOUSE_OUT,mout);
mc2.addEventListener(MouseEvent.MOUSE_OUT,mout);
mc3.addEventListener(MouseEvent.MOUSE_OUT,mout);
function mover(e:MouseEvent):void {
stopPlayReverse();
e.currentTarget.play();
}
function mout(e:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
function playReverse(e:Event):void {
if (mc1.currentFrame == 1) {
stopPlayReverse();
} else {
mc1.prevFrame();
}
}
function stopPlayReverse():void {
if (this.hasEventListener(Event.ENTER_FRAME)) {
this.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
Any idea how I can fix this?
Your function playReverse only use mc1 how can it work with the others movie clip.
If you choose to do it that way you can keep a track of the current MovieClip playing in reverse.
You will have to add more logic if you want that the play in reverse always finish when passing over one clip to another.
var currentMovieClip:MovieClip=null;
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
currentMovieClip = mc;
this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
}
function playReverse(e:Event):void {
if (currentMovieClip==null) {
return;
}
if (currentMovieClip.currentFrame == 1) {
stopPlayReverse();
} else {
currentMovieClip.prevFrame();
}
}
Another way that implies that you can have each clip to finish their play in reverse
function mover(e:MouseEvent):void {
stopPlayReverse(e.currentTarget as MovieClip);
e.currentTarget.play();
}
function mout(e:MouseEvent):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc !== null) {
mc.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
}
function playReverse(e:Event):void {
var mc:MovieClip = e.currentTarget as MovieClip;
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
}
function stopPlayReverse(mc:MovieClip):void {
if ((mc!==null) && mc.hasEventListener(Event.ENTER_FRAME)) {
mc.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}