as3 stopping sound removes eventListener? - actionscript-3

I am having an issue with stopping a sound and then starting it back up. After I stop the sound and start it again the eventListener seems to be gone.
Now the "easy" fix seems to be just "add" another one when you start the sound again.
This can not be done easily because the sound channel "Praying" has dynamic listener
added to it with a different function called at the end of each. So I would have to know what listener was added to it and what function should be called when done.
Again, I simply want to "pause" the currently prayed prayer with mouse click and start it up in the same spot with another click. But the issue is that it is removing the eventListener with the instructions for what to do after the sound is done playing.
Any thoughts on a work around? Or maybe this is an easy fix?
/// EXAMPLE 1
Praying = OFE.play();
Praying.addEventListener(Event.SOUND_COMPLETE, prayDecade );
/// EXAMPLE 2
Praying = JES.play();
Praying.addEventListener(Event.SOUND_COMPLETE, doSomethingElse);
public function togglePraying(e:Event = null)
{
if(nowPraying)
{
Praying.stop();
nowPraying = ! Praying;
trace("Praying: " + currentSound);
}
else
{
Praying = currentSound.play();
nowPraying = ! Praying;
trace("Praying: " + Praying);
}
}

This is normal, when you call OFE.play(), you get a SoundChannel reference and if you call it another time, you get a NEW REFERENCE. You need to register the event again, but don't forget to remove the listener.
if(nowPraying)
{
Praying.removeEventListener(Event.SOUND_COMPLETE, doSomethingElse);
Praying.stop();
nowPraying = ! Praying;
trace("Praying: " + currentSound);
}

Related

Cant work out why my fading script won't work

I've run across a piece of code that I'm really struggling to get working... It's meant to wait three seconds before fading an object (swslogo), however when I test it, it doesn't seem to work.. anyone know why this might be
var GameMode:Number = 0;
swslogo.alpha = .0;
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
intro();
function intro(e:Event=null):void
{
IntroTimer.reset()
IntroTimer.start();
}
function swsfadein(e:Event=null):void
{
IntroTimer.stop();
swslogo.addEventListener(Event.ENTER_FRAME, fadein)
}
function fadein(e:Event=null){
if(swslogo.alpha <=0){
this.alpha +=0.1;
if(swslogo.alpha >=1){
this.removeEventListener(Event.ENTER_FRAME, fadein);
}
}
}
Edit: Removed accidental line that wasn't meant to be there
From your last few questions I can tell you totally need to learn what a program is, in general, unrelated to AS3 of any other given language, instead of struggling with random pieces of code while treating them like magic spells.
// This part is fine.
var IntroTimer = new Timer(4000,1); //add a 4 second timer
IntroTimer.addEventListener(TimerEvent.TIMER_COMPLETE,swsfadein);
// A method is called. It resets and restarts timer.
intro();
// Another method is called. It stops the times and starts the fading thing.
swsfadein ();
So basically that code in a single go:
Creates timer.
Starts timer.
Stops timer.
Starts fading.
Which obviously suppresses the intended use of the timer.

Removing Children in AS3

My flash game exists of a timeline with multiple frames (I know I should avoid the timeline)
The point of the game is a point and click adventure. The object that you are able to pick up get spawned and destroyed accordingly as you enter and leave the room. now my problem is when entering frame 14 (accessibel from frame 12) it creates a piece of paper which you are able to pick up if you have another item. Now my problem is when you can't or don't pick up the paper and go back to frame 12 (only exit is to frame 12), you can't click on any other object and you are basicly stuck on frame 12. When leaving and entering other rooms it works properly but for some reason it doesn't for on the paper on frame 14.
My code to remove objects works as following
In my Main.as Documentclass I have a function that called as soon as the game starts which does the following
if (lastframe == 14)
{
trace (prop.numChildren);
while (prop.numChildren )
{
prop.removeChildAt(0);
}
}
The lastframe variable is established when moving from frames
this function is found on the frame itself (each exit function on it's own respective frame)
function exitKantine(event:MouseEvent):void
{
Main.lastframe = 14;
gotoAndStop(12);
}
The function to remove the prop actually removes it but then causes all other clickable objects to be unusable.
Thanks for looking at my question and thanks in advance for your suggestions
I would say instead of removing children, add it once in the beginning, add all the listeners in the beginning, and toggle the visibility instead of trying to addChild and removeChild every time you want to hide it. Use an array so you can have a few happening at the same time.
something like this:
private function init():void
{
assignVars();
addListeners();
stage.addChild // make sure this is in document class or you are passing stage to the class using it
}
for (var i = 0; i < _thingsAry.length; i++)
{
if (_thingsAry[i] == 14)
{
_thingsAry[i].visible = false;
trace("the visibility of _thingsAry[" + i + "] is " + _thingsAry[i].visible
}
}

Parameter child must be non-null error in AS3

I have a code to pause the game, and it runs the pause function as shown:
public function onKeyPress(keyboardEvent:KeyboardEvent) :void
{
//Check for pause
if(keyboardEvent.keyCode == Keyboard.P)
{
//If the timer is still running
if(gameTimer.running)
{
gameTimer.stop();
Mouse.show();
pauseText = new PauseText();
pauseText.x = 150;
pauseText.y = 100;
addChild(pauseText);
//If the player is using the mouse, resume by clicking on the player
if(mouseControl)
{
player.addEventListener(MouseEvent.CLICK, resumeGame);
pauseText.pauseInformation.text = "click on yourself";
}
else
{
pauseText.pauseInformation.text = "press 'p'";
}
}
else
{
//Only allow the player to resume with P IF he is using the keyboard
//This prevents cheating with the mouse.
if(!mouseControl)
{
gameTimer.start();
removeChild(pauseText);
pauseText = null;
}
}
}
}
The game runs perfectly fine. On my first playthrough, the pause functions work. However, if later I die and restart the game, then pause it, I get the following message:
TypeError: Error #2007: Parameter child must be non-null.
at flash.display::DisplayObjectContainer/removeChild()
at Game/onKeyPress()
The game still runs fine though. However, everytime I pause, or unpause, this error appears. If I die again, restart, then pause, TWO of these errors appears. From what I can gather it seems as if it attempts to remove the pauseText…but I’ve been removing it just fine on the first playthrough, I’ve used removeChild() then set as null for other parts of my code and it works fine. Additionally, if I add a trace(“a”); statement right after the function header, I get the error before the “a” appears on the output panel.
What’s wrong?
Additional notes:
If I don’t use the pause function at all for my first playthough, there is no error when I call it up on my second playthrough.
put removeChild into 'if' ,this will solve error :
if(pauseText.parent){
pauseText.parent.removeChild(pauseText);
}
but You should anyway check what is the source of problem , maybe 'gameTimer.running' is false on beggining ?
You probably instantiate another Game object (the one that contains the whole game) while not removing the previous game's event listener. That would explain such behavior, since you have more than one KeyboardEvent.KEY_DOWN listener active, and note that when you're stopping the game, you most likely stop the timer in it, so the "else" clause of your "if (gameTimer.running)" statement is executed, but the timer was effectively stop without pauseText to be generated. So, you miss a
removeEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);
in your game destruction code.
if (!mouseControl) {
gameTimer.start();
if (pauseText && contains(pauseText)) {
removeChild(pauseText);
pauseText = null;
}
}

Fake mouseclick on hover AS3

This morning I stumbled to this question:
When hovering a button, is it possible to click it automatically after X seconds? So the user doesn't need to click it with his mouse?
How can I let Flash believe my mouse really clicked some button on the stage or brought up with as3?
I have a lot of buttons in my movie. So I prefer to use some code which will cover this function for all existing or coming up buttons in my movie.
I would normally use a code like this but is there some workaround to accomplish this in a different way? I do no want to add code to every button.
this.addEventListener(MouseEvent.OVER, onMouseClickEvent);
public function onMouseClickEvent(event:Event)
{
trace(event);
if(event.buttonDown) // if button goes down normally
trace("MOUSE CLICKED NORMALLY");
else
trace("left button was not down");
}
The easiest way i think, is to subclass Button.
Then you should add mouse over/out listeners, add click listener that looks like that
:public function clickListener(event:MouseEvent = null){...}
When the mouse is hovering, raise a flag that the mouse is on the object, start a timer and when the timer callback function is called, you check the if the flag (you turn the flag down, when the mouse is out) is true and just call clickListener()
Listen for MouseEvent.MOUSE_OVER and start a timer, at the end of which the button will send the MouseEvent.CLICK event. In the mouseover handler, use the SystemManager to add a listener for MouseEvent.MOUSE_OUT which cancels the timer. The timer removes the listener using the SystemManager as well. So does clicking the button.
Finally! Solved!
This did the trick:
public function runOnce(event:TimerEvent):void {
btnSignal.dispatch("KEYBOARD", btnCode);
}
Robusto & Radoslav Georgiev: Thank you for pointing the right direction!
(I'm answering this a little late but would like to give input for future people).
One way to 'skin this cat' is to simply let your hover event trigger a timer (i.e. 3 seconds). In an EnterFrame or other function let a number or Boolean change when 3 seconds is reached.
//Pseudo code
if(timer == 3)
{ numberVar = 1;
//or
BooleanVar = True;
}
else
{
numberVar = 0;
//or
BooleanVar = false;
}
//end
Then just as you connected your methods to a mouseEvent, connect those same methods to fire when numberVar == 1 or BooleanVar == True. That's it.
For super simplicity and readability let your MouseClickEvent just be numberVar = 1 or BooleanVar = True.
These become super simple to implement over time and in my experience are 'very' error proof. Easy to fix also in the case of a typo or something else. No super elusive imports either. Hope that helped.
Great question by the way (+ 1)
:D

Need to cancel an actionscript command stop() and continue my project

I have an Actionscript on an invisible button in my Flash project and it is supposed to stop on a frame if it is clicked. This is the script on the button:
invisobutton.addEventListener(MouseEvent.CLICK,stopframe);
function stopframe(Event:MouseEvent):void{
stop();
}
with invisobutton being the instance name of the button. It works if you click anywhere inside the invisible button the movie stops on that frame. My problem is that I do not know how to cancel the stop and continue where the movie paused. Is there a counter for the stop() command so that it can be like off and there was an on switch? I thought about using a second function for go to and play like this:
invisobutton.addEventListener(MouseEvent.MOUSE_OUT, restart);
function restart(Event:MouseEvent):void{
gotoAndPlay(*);
but I am not sure how to know what to put into the (*) so that it plays from right where it was stopped.
If someone could please advise me if there is a reverse command to stop() that can turn it off ans start again. I already tried play() but I got errors from Flash over it. I thought about start() but I had never heard of or seen such a command. Or if someone could tell me a way to correctly retrieve the frame number when the stop() command executes.
Assuming that what you want is to use the same button to toggle between play and stop, you can use the following code:
var lastFrame:int;
var isPlaying:Boolean = true;
invisobutton.addEventListener(MouseEvent.CLICK, toggleFrame);
function toggleFrame(Event:MouseEvent):void {
if ( isPlaying ) {
lastFrame = this.currentFrame;
stop();
} else {
this.gotoAndPlay( lastFrame );
}
isPlaying = ! isPlaying;
}