play/pause button in flash - actionscript-3

//how can i return the symbol of pause button in the end of Movie Clip ,it back to a play button ?
import flash.display.MovieClip;
stop();
play1.addEventListener(MouseEvent.CLICK,playbutt);
function playbutt(event:MouseEvent):void{
MovieClip(root).play();
MovieClip(root).am.play();
gotoAndStop(2);
}

Your question isn't very clear but from what I gather is that you need to add a listener to whatever you have playing, and then execute a gotoAndStop(1); How you do this however depends on what you are playing and what would be the most effient way of listening for its completion.
If you are playing music or a video, there is usually a Object.COMPLETE that you can use.

Related

My AS3 code will not run my animation properly

I want to be able to press any key on my keyboard so as to appear to be typing my specific
text. No matter what key is pressed, the same scripted text appears, letter by letter(one per keystroke)/frame by frame. Can anyone help me here? As it stands, my animation just continues to loop unless I hit enter (then it stops until pressed again.)
stop();
var keyList:Object = new Object();
keyList.onKeyUp = function () {
nextFrame();
}
Key.addListener(keyList);
stop();
Using Flash cc-thanks
Your code appears to be AS2 rather than AS3?
If your looking for AS3 functionality, you'll want to do something like listen to the stage for keyboard events, and then have an event handler that progresses the frame.
stage.addEventListener(KeyboardEvent.KEY_DOWN,_handleKeyboardPress);
function _handleKeyboardPress(e:KeyboardEvent)
{
nextFrame();
}
Your code looks as though it would work if you are publishing to AS2 though?

How do I stop a timed event from happening?

I am making a game where a user clicks on insects and tries to get a good high score. When the user clicks on one insect, the insect plays an animation and stays in its spot for 2 seconds in this code:
import flash.events.MouseEvent;
import flash.display.MovieClip;
import fl.motion.Animator;
import flash.events.*;
play();
var mysound:squish = new squish();
this.addEventListener(MouseEvent.CLICK, kill);
this.dead = false;
function kill(e:MouseEvent):void
{
this.dead=true;
mouseChildren=false
mysound.play();
gotoAndPlay(21);
this.removeEventListener(MouseEvent.CLICK, kill);
flash.utils.setTimeout(removeSelf,2000);
}
function removeSelf():void
{
this.parent.removeChild(this);
}
When the user pauses the game, the enemies are stopped and they turn invisible. The only problem is that when the user clicks on the insects, and hits the pause button, the insects stay there for 2 seconds. How do I remove the timer when the person pauses the game so that no insects are on the screen?
A few tips:
Never use timeouts, they will produce poor, unsustainable, hard to debug code. Use Timer instead. Timer provides nice little features like: repeat times, pausing, resuming and resetting the timer. So you could set the timers timeout to 2000ms, repeat once and set an event listener TimerEvent.TIMER_COMPLETE to remove the insect etc. You can pause the timer when you pause the game and resume when needed.
If you do however use timeouts, assign a uint to it and use a clearTimeout() method to stop it.
construction such as this.parent.removeChild(this); is ugly. Instead, dispatch an event as an alternative and let the parent to manage the dead bugs (remove from arrays, display list etc).
Storing a sound in every bugs instance will take way more memory than needed. Make a static sound library class and call the play() method from it.

replay btn in flash cs6 using actionscript 3 is not behaving correctly

I have a flash animation that using action script at the end to stop the animation and has a button for the user to click and replay at the animation. When they click and replay the animation the last scenes that were on the screen don't disappear as the animation restarts. My button has an instance name of replay_btn. Any suggestions?
my action script code is
stop();
import flash.events.MouseEvent; function onClick(event:MouseEvent):void { gotoAndPlay(1); } replay_btn.addEventListener(MouseEvent.CLICK, onClick);
This has been answered now. It seem like there was a bug in flash causes the last scenes to hang while replay the start of the animation. The workaround was to make the replay button go forward one frame to a blank white frame and then direct it back to the start.
Assumed the button is inside the movieclip you want to startover.
stop();
import flash.events.MouseEvent;
function onClick(event:MouseEvent):void {
replay_btn.removeEventListener(MouseEvent.CLICK, onClick);
MovieClip(parent).gotoAndPlay(1);
}
replay_btn.addEventListener(MouseEvent.CLICK, onClick);

Checking if Mouse Click was not in a MovieClip

I have number of movieClips on stage, with each having it's own Event Listener. Once Click/Touch the event is called each movie clip does something. For example one movieClip makes about 6 other movie clips visible.
What I want to do is, when the user Touch/Click somewhere else on stage, where there is no movieClip I want to know, so I can perform some actions such as make some movieClips invisible.
P.S the reason why I say Touch/Click is I'm developing this app for Android, however to make testing easier I'm currently testing everything in PC with MouseEvent, rather than TouchEvent. Once I get all the features working, I will switch over to TouchEvent and test it in mobile.
Many Thanks,
Mike
Add event listener to the stage. And in the event handlers of your inner movieclips use event.stopPropagation function to prevent bubble event to the container.
I would just do a check, it's simple and quick and doesn't require adding code to alter propagation.
import flash.events.MouseEvent;
stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
function onClick(e:MouseEvent):void
{
if(e.target == stage)
{
trace("click click");
}
}

Adobe Flash CS3 sound in movieclip

There's an animation (MovieClip) in my scene. There's a sound accompanying animation. I inserted it into the first frame of the animation.
I want this animation to be inactive initially. I plan to activate it when some button is pressed.
I don't know how to set it "inactive" from within Adobe Flash workspace, so I just put in constructor of my scene:
MovieClip(getChildByName("MyAnim")).stop();
By it does not prevent that sound to start playing when my scene appears!
How do I manage it?
In my experience, it usually isn't a good idea to insert sounds directly onto the timeline in flash. I strongly advice to import the .mp3 into the Flash program and play it by code using the AS3 Sound class.
EDIT: This way, the sound won't play until you use the Sound Class .play() method, which you could do at the beginning of your animation on the timeline OR from the button that activates it.
Here is a nice Republic of Code tutorial giving you a step by step on how to play a Sound file using the AS3 Sound Class.
Put the sound on frame two in your movieclip. Then go to that frame when you click the button.
private function onButtonClick(event : MouseEvent) : void
{
mySoundClip.gotoAndStop(2);
}