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

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;
}

Related

clickTAG and stop tag won't work simultaneously

I've created a swf banner and have added a stop(); action so it doesn't loop. As soon as I add the clickTAG to the file, the stop tag is ignored and continues to loop. I'm using Flash CC and as3. How do I get them both to work. I've done it before in older versions.
Here is my click tag:
clickTAG.addEventListener(MouseEvent.MOUSE_UP, onClick);
function onClick(e:MouseEvent):void {
var click_url:String = root.loaderInfo.parameters.clickTAG;
if(click_url) {
navigateToURL(new URLRequest(click_url), '_blank');
}
}
Here is my stop:
stop();
They are on different layers.
Are you having some output in the "COMPILER ERRORS" panel?.
please note that the code must be inserted into Actions Frame at first Frame and that you must have a transparent movie clip in the Stage with "clickTAG" as instance name.

Audio ActionScript 3

import flash.media.Sound;
var sound1 = new MenuTheme();
sound1.play(1000);
if(currentFrame == 2(your dedicated frame)
{
sound1.stop();
}
It comes up with Error TypeError: Error #1006 I want it to stop on the next frame after clicking a button otherwise this error stops everything :(
How to get audio to stop on next scene?
Rather than checking to see if the current frame is 2, I would suggest stopping the sound when the button is clicked, like so (with an example button named buttonToNextFrame):
buttonToNextFrame.addEventListener(MouseEvent.MOUSE_DOWN, goToNextFrame);
function goToNextFrame (e: MouseEvent) {
sound1.stop();
//Other stuff you would like done when the button is pressed.
gotoAndStop(2);
}
If there is a way to bypass the button to get to frame 2 and you still want the sound to stop, then you would have to either:
stop the sound in every event/way used to get to frame 2
stop the sound directly on frame 2 with a plain sound1.stop()
Let me know if you have any further questions!

as3 stopping sound removes eventListener?

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);
}

AS3 Button to stop Movieclip after its finished playing

Ok, so I'm a beginner at AS3 and Flash and I managed to put this code together for an animation. A Button called start_btn is supposed to start and stop a movieclip called main_mc. On the first click of the Button, the Movieclip is supposed to play (which it does), however on the second click, the movie stops in the middle of its animation (which I don't want). My question is, when you click the Button a second time, how can i get the Movieclip to finish playing its animation then stop on the last frame?
I thought about using if (main_mc.currentFrame == main_mc.totalFrames); {main_mc.stop(); but the Movieclip still does not stop on the last frame. The Movieclip itself also has a gotoAndPlay(2); command on the last frame so that the animation repeats before the Button is clicked a second time.
here is the code i have:
`start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
function mainaniS(event:MouseEvent):void
{
main_mc.play();
start_btn.removeEventListener(MouseEvent.CLICK, mainaniS);
start_btn.addEventListener(MouseEvent.CLICK, mainaniSt);
}
function mainaniSt(event:MouseEvent):void
{
if (main_mc.currentFrame == main_mc.totalFrames);
{main_mc.stop();}
start_btn.removeEventListener(MouseEvent.CLICK, mainaniSt);
start_btn.addEventListener(MouseEvent.CLICK, mainaniS);
}`
Try main_mc.gotoAndStop(main_mc.totalFrames).
I was going to provide a quick and dirty solution, but decided instead to try and explain a few of the issues with your current implementation and attempt to refactor and explain and better one. Unfortunately I don't have access to Flash right now, so the code is untested.
You're adding and removing event listeners often, which is generally a bad idea. Instead, since you're using a single button to perform multiple functions it would make sense to track the button state in a separate variable. In this case, a boolean for whether or not the movieclip is currently playing.
var playing:Boolean;
Now we can combine the mainaniS and mainaniSt into one and perform a different action based on whether or not the movieclip is playing, and just keep the one eventlistener on the button. I've also taken the liberty of naming the method something more meaningful:
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
You may be wondering why we don't call main_mc.stop() in the first block: the reason is that you don't want to stop the movieclip as soon as you click the button, but after the movieclip has finished playing if the button has been clicked. Therefore, we just set playing to false to indicate that we want it to stop later.
Finally, we need to make sure the movieclip stops upon completion, but only if playing is false. To do this we add a listener to movieclip that is called every frame, and checks whether playing is false, and if it's on the last frame. Note that the last frame is actually totalFrames - 1: this is because the frame numbers start from zero rather than one (i.e. if totalFrames is 3, the frame numbers will be 0, 1, 2).
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}
All the refactored code together:
var playing:Boolean;
start_btn.addEventListener(MouseEvent.CLICK, onStartClick);
main_mc.addEventListener(Event.ENTER_FRAME, animate);
function onStartClick(event:MouseEvent):void
{
if(playing) {
playing = false;
}
else {
playing = true;
main_mc.play();
}
}
function animate(event:Event):void {
if(!playing && main_mc.currentFrame == main_mc.totalFrames - 1) {
main_mc.stop();
}
}

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