AS3 - Removing event listener failed - actionscript-3

here's my code, i don't know why it doesn't remove the event listener.
What i want to do is having some button to turn the sound on and off according to the beat. On the first click it worked perfectly, but when i want to remove the sound on the second click it cannot remove the event listener.
plat.BEAT1.addEventListener(TouchEvent.TOUCH_BEGIN, BEATDown(plat.BEAT1, hihatz, 2));
plat.BEAT2.addEventListener(TouchEvent.TOUCH_BEGIN, BEATDown(plat.BEAT2,cymbalz, 4));
function BEATDown (padNum, sounz, tiempo) {
return function (e:TouchEvent) {
var currentSound:Sound = null;
var currentSoundChannel:SoundChannel;
var active:int;
if (padNum.currentFrame == 1) {
padNum.gotoAndStop(3);
padNum.addEventListener(Event.ENTER_FRAME, PlayBeats);
active = 1;
} else {
padNum.gotoAndStop(1);
padNum.removeEventListener(Event.ENTER_FRAME, PlayBeats);
active = 0;
}
function playSound(sound:Sound):void
{
if (active == 0)
{
// Stop playing ANY sound
currentSound = null;
currentSoundChannel = null;
}
else
{
// Play a different sound
currentSound = sound;
currentSoundChannel = sound.play();
}
}
function PlayBeats(event:Event):void
{
if (tiempo == 1) {
if (fl_SecondsElapsed <= 4) {
playSound(sounz);
}
}
if (tiempo == 2) {
if (fl_SecondsElapsed == 1 || fl_SecondsElapsed == 3) {
playSound(sounz);
}
}
if (tiempo == 4) {
if (fl_SecondsElapsed == 1) {
playSound(sounz);
}
}
}
}
}
EDIT:
The listener i want to remove is padNum.addEventListener(Event.ENTER_FRAME, PlayBeats);
plat.BEAT1 is the button instance. I use multiple instance to trigger different sound, each toggle sound on and off according to the Tiempo count.

I would say it's because you don't add / remove your listener on the same object;
is plat.BEAT1 et plat.BEAT2 are the same? I guess not.
I would recommend you to try by adding the EnterFrame listener on stage on this or on the parent element to the if the event is Removed correctly. Then if you want your object works independently, try to target the right object.
if (padNum.currentFrame == 1) {
padNum.gotoAndStop(3);
stage.addEventListener(Event.ENTER_FRAME, PlayBeats);
active = 1;
} else {
padNum.gotoAndStop(1);
stage.removeEventListener(Event.ENTER_FRAME, PlayBeats);
active = 0;
}

Related

I need a working pause and resume code on my project

I tried many of code so far pause and play to game but did not work many of them.
I got a moving object on stage also I have timer dynamic text.
I need a working pause and play code on my project.
Example my moving object
/*Fish 3 move*/
var balik3x:Number=7;
var balik3y:Number=Math.random()*15
 
stage.addEventListener(Event.ENTER_FRAME,h3);
function h3(oly:Event) {
balik3.x+=balik3x;
balik3.y+=balik3y;
if ((balik3.x>=stage.stageWidth-balik3.width/2)|| (balik3.x <= balik3.width/2 )) {
balik3x*=-12;
}
if ((balik3.y>=stage.stageHeight-balik3.height/2)|| (balik3.y <= balik3.height/2 )) {
balik3y*=-1;
}
}
balik3.mouseEnabled = false;
My timer code
UPDATE: #www0z0k Did you mean like this.The timer pause and mouse over bug not exist anymore.But timer is not resume when I click to play button.
time.text="0:10";
var dispSecs=09;
var dispMins=0;
 
var timerInterval=setInterval(countDown,1000);
var control:Timer = new Timer(1000,0)
control.addEventListener(TimerEvent.TIMER, keko)
control.start();
 
function keko (evt:Event):void{
if(dispMins <1 && dispSecs <1 )
{
timeisup.visible = true;
timeisup.play();
}
}
 
function countDown()
{
dispSecs--;
if (dispMins == 0 && dispSecs == 0)
{
clearInterval(timerInterval);
}
else if (dispSecs == 0)
{
dispSecs = 59;
if (dispMins > 0)
{
dispMins--;
}
}
time.text = prependZero(dispMins) + ":" + prependZero(dispSecs);
}  
 
function prependZero(num)
{
if(num<10)
{
num=""+num;
}
return(num);
}
The last code I tried.But it's not work exactly. UPDATED
play1.addEventListener(MouseEvent.CLICK, resumeGame);
function resumeGame(event:MouseEvent):void{
addEventListener(TimerEvent.TIMER, keko);
stage.frameRate = 30;
}
pause1.addEventListener(MouseEvent.CLICK, pauseGame);
function pauseGame(event:MouseEvent):void{
stage.removeEventListener(TimerEvent.TIMER, keko);
clearInterval(timerInterval);
stage.frameRate = 0.01;
}
Not specific to your code but you'll get the idea
private function togglePause():void{
// check if the event listener exists
if (!stage.hasEventListener(Event.ENTER_FRAME)){
// add it if it doesn't
stage.addEventListener(Event.ENTER_FRAME, tick);
} else {
// remove it if it does
stage.removeEventListener(Event.ENTER_FRAME, tick);
}
}
I think there might be a small mistake , you have added the event listener to this and try to remove the event listener from stage. try this code and if this doesnt work let me know I will provide you another solution
play1.addEventListener(MouseEvent.CLICK, resumeGame);
function resumeGame(event:MouseEvent):void{
addEventListener(TimerEvent.TIMER, keko);
stage.frameRate = 30;
}
pause1.addEventListener(MouseEvent.CLICK, pauseGame);
function pauseGame(event:MouseEvent):void{
stage.removeEventListener(TimerEvent.TIMER, keko);
clearInterval(timerInterval);
stage.frameRate = 0.01;
}

Adding multiple instances of the same object to the stage

I'm making a "spit" card game and I'm trying to be able to have 3 cards on the stage at once to be able to drag to the pile. So my question is how do I have multiple instances of the same object on the stage, but be able to assign each card different frames, etc. Here is the code to understand the context of it.
"cardArray" has 53 frames, 52 of which have a card face, 53rd is a "deck" picture.
//variables, constants
var cardDeck:Array = new Array();
var timer:Timer = new Timer(2000);
var j:int = 0;
var card:MovieClip = new CardArray();
var cardInDeck:Boolean;
const deckSize:int = 52;
//events:
card.addEventListener(MouseEvent.MOUSE_DOWN,fDown);
card.addEventListener(MouseEvent.MOUSE_UP,fUp);
startRandomise();
//This is where the unshuffled "deck" is created - values loaded into the array.
function createDeck():void
{
var i:int;
for(i =0; i<deckSize; i++)
{
cardDeck[i] = i+1;
}
}
function randomizeDeck( a : *, b : * ):int
{
return (Math.random()>.5) ? 1 : -1;
}
//Grab the value from the (presumably) current frame of the card
function convertCard(cardNo:int):int
{
var deckPos:int;
if (cardNo <= deckSize/4)
{
deckPos = cardNo;
}
else if (cardNo > deckSize/4 && cardNo <= deckSize/2)
{
deckPos = cardNo -13;
}
else if (cardNo > deckSize/2 && cardNo <=deckSize*3/4)
{
deckPos = cardNo -deckSize/2;
}
else if (cardNo >39)
{
deckPos = cardNo -39;
}
return deckPos;
}
btn.addEventListener(MouseEvent.MOUSE_UP,showArray);
function showArray(event:MouseEvent):void
{
if(j<deckSize /2)
{
addChild(card);
card.gotoAndStop(cardDeck[j]);
trace(cardDeck[j]+","+deckCompare[j]);
j++;
}
if(cardInDeck)
{
card.x = 200;
card.y = 200;
cardInDeck+false;
}
}
function fDown(evt:MouseEvent)
{
card.startDrag();
}
function fUp(evt:MouseEvent)
{
stopDrag();
if(card.hitTestObject(deck))
{
trace("deck: "+convertCard(deck.currentFrame));
trace("card: "+convertCard(card.currentFrame));
if(convertCard(card.currentFrame) == convertCard(deck.currentFrame)+1 || convertCard(card.currentFrame) == convertCard(deck.currentFrame)-1 || convertCard(card.currentFrame) == 13 && convertCard(deck.currentFrame) == 1 || convertCard(card.currentFrame) == 1 && convertCard(deck.currentFrame) == 13 || convertCard(deck.currentFrame) == 14)
{
deck.gotoAndStop(card.currentFrame);
removeChild(card);
cardInDeck=true;
}
else
{
card.x = 200;
card.y = 200;
}
}
else
{
card.x = 200;
card.y = 200;
}
}
function startRandomise():void
{
createDeck();
cardDeck.sort(randomizeDeck);
cardDeck.sort(randomizeDeck);
trace("random: "+cardDeck);
trace("deckCompare: "+deckCompare);
card.x = 200;
card.y = 200;
deck.gotoAndStop(53);
}
To have multiple cards, you need to instantiate more than one card. I don't know anything about the card game spit and don't understand the purpose of many of your functions, but you'll want to do something along these lines:
Create a function that generates (instantiates) a new card:
function createCard(faceFrame):MovieClip {
var tmpCard:MovieClip = new CardArray(); //this creates a new instance of a card
tmpCard.addEventListener(MouseEvent.MOUSE_DOWN, cardMouseDown); //make the card call the mouse down function when that event is triggered
tmpCard.gotoAndStop(faceFrame); //assign the card it's face (frame)
addChild(tmpCard); //add the card to the screen
return tmpCard;
}
Then, in your mouse down handler (I renamed it to be more clear what is is), you can do the following to card that was mouse downed:
function cardMouseDown(evt:MouseEvent) {
//the events currentTarget property is reference to the item that you attached the listener to, so in this case the card who triggered the mouse down
card = evt.currentTarget as MovieClip; //assign the current clicked card to the global card variable (so we can access it in the mouse up function)
card.startDrag();
//add the mouse up listener on the stage, since there are cases where you can drag so fast that the mouse isn't over the item it's dragging (and then if you were to release the mouse at that moment it wouldn't dispatch the event)
stage.addEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
}
Then your mouse up function would look something like this:
function stageMouseUp(evt:MouseEvent) {
//remove the mouse up listener from the stage
stage.removeEventListener(MouseEvent.MOUSE_UP, stageMouseUp);
card.stopDrag();
if(card.hitTestObject(deck))
{
......//rest if the code
Seems like you would want to generate a new card in the showArray function you have? So something like this:
function showArray(event:MouseEvent):void
{
var newCard:MovieClip = createCard(cardDeck[j]);
j++
newCard.y = newCard.x = 200;
trace(cardDeck[j]+","+deckCompare[j]);
}
As an aside, you should consider calling your Card object something more sensible, like Card instead of CardArray, as it's not an array...

Starling mouseover detection

I'm making a point & click game where is clickable objects. When player moves mouse over the object, there appear a tooltip beside the cursor. It works almost as intended with the code below:
private function added():void
{
removeEventListener(Event.ADDED, added);
this.addEventListener(TouchEvent.TOUCH, onTouch);
}
protected function onTouch(e:TouchEvent):void
{
var touchHover:Touch = e.getTouch(this, TouchPhase.HOVER);
if (touchHover)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
else
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
}
However, it has a strange problem I don't understand. If I move mouse over object and then move it downwards still staying over the object, it triggers hiding function in every second frame or so. Same thing happens when I move cursor to the right, but not when moving it up or left.
So my question is what is wrong with my code? Is this even the best way to go to detect when mouse rolls over object and when it rolls away?
EDIT: I have been going through following iterations with each of them the same problem:
var touch:Touch = event.getTouch(this);
if (touch == null) {
// Set Object alpha to 0;
//trace("pois");
Game.hoverText.hideMessage(name_);
}
else if (touch.phase == TouchPhase.HOVER) {
// Set Object alpha to 1;
//trace("paalla");
if (Game.hoverText.message != name_)
Game.hoverText.message = name_;
}
else {
// for a phase BEGIN/MOVE/STATIONARY case
// see if the touch is over the bounds of the tile (assuming 'this' is the tile)
HELPER_POINT.x = touch.globalX;
HELPER_POINT.y = touch.globalY;
this.globalToLocal(HELPER_POINT, HELPER_POINT);
if(this.hitTest(HELPER_POINT, true) != null)
{
// Set Object alpha to 1; over tile
trace("paalla");
}
else
{
// Set Object alpha to 0; not over tile
trace("pois");
}
}
var touchHover:Touch = e.getTouch(this);
if (touchHover && touchHover.phase == TouchPhase.HOVER)
{
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
}
if (touchHover == null)
{
//mouse leaves the object
trace("hide");
Game.hoverText.hideMessage(name_);
}
Also here is swf for demonstating the problem:
http://www.students.tut.fi/~salmi26/ScorpionBox.html
private function isPressed(event:TouchEvent):void
{
var touch:touch = event.getTouch(this);
if(touch.phase == TouchPhase.BEGAN){
trace("show");
//mouse is hovered over this object. Therefore call Hovertext:
if (Game.hoverText.message != name_)
Game.hoverText.message = name_
} else if(touch.phase == TouchPhase.ENDED){
trace("release");
//stop doing stuff
removeEventListener(Event.ENTER_FRAME, onButtonHold);
}
}
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;
private var touches:Vector.<Touch>;
private var touch:Touch;
private var touchStage:Touch;
private function onTouch(e:TouchEvent=null):void {
touches= e.getTouches(DisplayObject(e.target));
if (touches.length == 1)
{
touch = touches[0];
if (touch.phase == TouchPhase.BEGAN){
m_TouchTarget = touch.target;
//HERE IS A MOUSE DOWN
}
if (touch.phase == TouchPhase.ENDED){
m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);
if (stage.hitTest(m_TouchEndedPoint, true) == m_TouchTarget)
{
//HERE IS A MOUSE UP
}
}
if (touch.phase == TouchPhase.MOVED){
m_TouchEndedPoint = new Point(touch.globalX, touch.globalY);
//HERE IS A MOUSE OUT
if (stage.hitTest(m_TouchEndedPoint, true) != m_TouchTarget)
{
//HERE IS A MOUSE OVER
}
}
}
}

AS3: How do I play a sound when the currentframe ==?

My code below unfortunately results in the sound repeating at the framerate of the movie. I just need a simple "play a sound at a specific frame" function but can't figure this one out. Thanks for your help.
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
This seems about right. Are you sure the naturepage.ocean_btns movieclip is not stopped at the second frame or looping very few frames over and over?
Another option is to use a flag to see if you already played the sound:
var alreadyPlayedSound:Boolean = false;
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2 && !alreadyPlayedSound)
{
alreadyPlayedSound = true;
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
}
}
Or to remove the event listener once you played the sound:
addEventListener(Event.ENTER_FRAME,soundFunction);
function soundFunction(event:Event)
{
if (naturepage.ocean_btns.currentFrame == 2)
{
ocean_channel.stop();
ocean_channel = ocean1.play(0,int.MAX_VALUE);
mySO.data.ocean = "2";
mySO.flush();
removeEventListener(Event.ENTER_FRAME,soundFunction);
}
}

Change Variable on Mouse Event

I'm trying to change the variable that's set to 0 into 1 on a mouse event.
I have 3 movie clips 'maskedbgmc' and when clicked its supposed to change a variable.
But it doesn't change the variable as far as i can see. What do i miss?
Thanks for your time :)
var checkCard1:Number = 0;
maskedbg_mc.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked1);
function cardChecked1 (event:MouseEvent):void {
checkCard1 = 1;
}
var checkCard2:Number = 0;
maskedbg_mc2.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked2);
function cardChecked2 (event:MouseEvent):void {
checkCard2 = 1;
}
var checkCard3:Number = 0;
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
}
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
Note that events happen asynchronously. This means that your if statements will execute the moment this code is interpreted, but the variables will only be modified when the mouse events occur. If you trace your variables value inside your event handler functions, you will likely see that it does indeed change when the mouse button is pressed.
Maybe what you want to do is add your if statements into a function, like so:
function checkCards() : void {
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}
You can then invoke this method inside your event listeners, and it will check the card variables using the above logic. An example of it used inside the cardChecked3() method:
maskedbg_mc3.addEventListener(MouseEvent.MOUSE_DOWN, cardChecked3);
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
checkCards();
}
Hope this helps.
Does the event have to be a MouseEvent.MOUSE_DOWN? If you are only interested in clicks I would try changing that to a MouseEvent.CLICK.
Just a thought tho.
Try changing your code to this for the last part
function cardChecked3 (event:MouseEvent):void {
checkCard3 = 1;
if(checkCard1 == 1) {
trace('Nice!');
}
else if(checkCard2 == 1) {
trace('Better!');
}
else if(checkCard3 == 1) {
trace('King!');
}
}