How to play movie clip just once? - actionscript-3

I've done some research on this site and figured out most of what I want to do. I click on a block. The block moves. When done it plays another movie clip. Problem is it loops the second movie clip. I found one query that deals with this but can't figure out how to make it work with the below code. Once the if statement is true it just keeps playing the clip (as it should), but I dunno how to make it play just once. Any suggestions much appreciated.
block_1.stop();
block_1.addEventListener(MouseEvent.CLICK, block_1_Click);
function block_1_Click(e:MouseEvent):void {
block_1.play();
}
block_1.addEventListener(Event.ENTER_FRAME, wavePlay);
function wavePlay(e:Event):void {
if (block_1.currentFrame==block_1.totalFrames) {
wave_mc.play();
}
}

Adobe's doc about the ENTER_FRAME event says:
[...] Dispatched when the playhead is entering a new frame. If the playhead is not moving, or if there is only one frame, this event is dispatched continuously in conjunction with the frame rate. [...]
That means that the ENTER_FRAME event keeps running until you remove its listener.
Your code should be:
block_1.stop();
block_1.addEventListener(MouseEvent.CLICK, block_1_Click);
function block_1_Click(e:MouseEvent):void {
block_1.play();
}
block_1.addEventListener(Event.ENTER_FRAME, wavePlay);
function wavePlay(e:Event):void {
if (block_1.currentFrame==block_1.totalFrames) {
wave_mc.play();
block_1.removeEventListener( Event.ENTER_FRAME, wavePlay );
}
}

Related

How do I change the size of a movieclip in flash by clicking on it? (Actionscript 3)

Everything I've found is for making a separate button, but I'm trying to make it so that clicking on the movieclip itself makes it change size. The reason is that it's being animated moving across the page, so clicking it is the challenge. Can anyone help? This is the code I tried:
info_btn_mc.buttonMode = true;
info_btn_mc.addEventListener(MouseEvent.CLICK, openInfo);
stop();
function openInfo(e:MouseEvent):void {
enemy_first.play("shrink");
}
You cannot use play("shrink"), the method
play() (Moves the playhead in the timeline of the movie clip) has no arguments. use gotoAndPlay/gotoAndStop instead that received an argument called frame:Object (frame number or label (String).
enemy_first.addEventListener(MouseEvent.CLICK, openInfo);
enemy_first.stop();
function openInfo(event:MouseEvent):void
{
trace('event.currentTarget:', event.currentTarget);
event.currentTarget.gotoAndPlay("shrink");
}

flash as3 first time a button pressed has no reaction

I am trying to make my first game in actionscript 3 and using 3 keyframes. this is the code from my first keyframe:
gamestart.addEventListener(MouseEvent.MOUSE_DOWN, start);
function start(e:MouseEvent):void
{
gotoAndStop(2);
}
And in my second keyframe, I have a keyboard listener.
stage.addEventListener(KeyboardEvent.KEY_DOWN ,pressButton);
function pressButton(e:KeyboardEvent):void
{
trace("aaa");
}
My problem is that, after entering the second frame, the second frame doesn't seem to get "focused on", which means I still need to click it to be able to use its keyboard events. Anyway to force focus on a frame?
Setting stage focus is as simple this:
stage.focus = stage;

Adobe Flash. Mouseover animation endless loop

I'm having some problems while making a button for a site. I created a button and an animation, then imported that animation as an .swf to the button file. I used the following code(with btn being the button and mc the movie clip):
btn.addEventListener(MouseEvent.MOUSE_OVER,btnF);
function btnF(e:MouseEvent):void
{
mc.play();
}
As you can imagine, when the mouse is over the button, the animation plays, but it doesn't stop... Any Solutions?
Details: Using Actionscript 3.0
And yes, i made the movie clip with a stop() code at the end... so I don't see why it loops.
It might be simpler to do it this way: create a button, on default state have the graphic for the button. On the over state use a movieclip with your animation. That should work without any code
The problem is that the MOUSE_OVER event will always fire if the mouse is over that "btn" display object. So the stop() code works at the end, but the play overwrites that command.
Here is a possible solution:
btn.addEventListener(MouseEvent.MOUSE_OVER, btnF);
function btnF(e:MouseEvent):void{
{
if(mc.currentFrame == 1)
{
mc.play();
}
}
//i called the instance of the button : buthead and the instance of the movie clip : head_mc
//on the first frame of the movie clip, i've put a stop function
buthead.addEventListener(MouseEvent.MOUSE_OVER, function(event:MouseEvent):void {
head_mc.play();
});

Tweening with TimeLine animation

Easy one for you guys but new to me. I have an animated walking character called mcPlayer. Inside of it's timeline I have frame labels at various animated states "walkingLeft","walkingRight" and "Idle". The walking animations are of him walking in one spot. I want to be able to use buttons to move the character with actionscript to various targets on the stage and have the corresponding animation play as it moves.So the most direct way I could think to do it is like this...
import com.greensock.*;
btnRight.addEventListener(MouseEvent.CLICK, moveRight);
btnLeft.addEventListener(MouseEvent.CLICK, moveLeft);
function moveRight(Evt:MouseEvent):void{
TweenLite.to(mcPlayer,2,{x:450});
mcPlayer.gotoAndPlay("walkingRight");
}
function moveLeft(Evt:MouseEvent):void{
TweenLite.to(mcPlayer,2,{x:450});
mcPlayer.gotoAndPlay("walkingLeft");
}
I have tried different commands on the mcPlayer timeline,like, putting a stop(); at the beginning of each anima. I have tried putting a gotoandplay(); at the end of each anima so it will go to the beginning and loop.I would like to use the timeline as little as possible.
How do I...
1. Have the animation play continuously while the tween is in motion
2. Have the animation stop when it has reached its destination
and finally have the anima"idle" play after mcPLayer has reached its target.
To loop an animation you would test for the last frame of the animation and then loop back, you can do that within a tween with the onUpdate parameter and use onUpdateParams to pass any data needed for the update. Such as the animation label and the last frame of the animation.
If you want to do something after a tween is completed, such as change to the idle animation, you'd use the onComplete parameter.
Here's an example of what you might do :
btnRight.addEventListener(MouseEvent.CLICK, moveRight);
btnLeft.addEventListener(MouseEvent.CLICK, moveLeft);
function moveRight(Evt:MouseEvent):void{
// lastframe should be replaced with whatever the frame the walk right animation ends on.
TweenLite.to(mcPlayer, 2, {x:450, onUpdate:updateHandler, onUpdateParams:['walkingRight', lastFrame], onComplete:idleHandler);
mcPlayer.gotoAndPlay("walkingRight");
}
function moveLeft(Evt:MouseEvent):void{
// lastframe should be replaced with whatever the frame the walk left animation ends on.
TweenLite.to(mcPlayer, 2, {x:10, onUpdate:updateHandler, onUpdateParams:['walkingLeft', lastFrame], onComplete:idleHandler);
mcPlayer.gotoAndPlay("walkingLeft");
}
function updateHandler(loopLabel:String, lastFrame:int):void
{
if (mcPlayer.currentFrame == lastFrame)
{
mcPlayer.gotoAndPlay(loopLabel);
}
}
function idleHandler():void
{
mcPlayer.gotoAndPlay("idle");
// this is also where you'd do anything else you need to do when it stops.
}
I am not sure how you have your content set up, I was just guessing that you had all the animations on the timeline of the mcPlayer with labels for each animation based on what you had said.
Depending how your content is set up, you might need to implement differently. But the concepts are still the same - using onComplete and onUpdate to handle things for those particular events.

currentFrame identifiers [AS3]

I'm completely new to Flash and AS3, and I'd have thought this would be a simple question, but I've been unable to find an answer online or in the documentation. I'm trying to use addEventListener (Event.ENTER_FRAME) and (.currentFrame) to disable a button for part of an animation sequence. While the animation sequence fades in, the Button Hand cursor is visible and people can select it before the sequence is completed. I want this button disabled for the first 213frames of the main timeline, which is when the button becomes visible.
Earlier, I was successfully able to disable a Rewind button for parts of a different movie scene using the code below with a few insignificant things changed.
Skip_btn.addEventListener(MouseEvent.CLICK, SkipToGoToScene);
function SkipToGoToScene(event:MouseEvent):void
{
MovieClip(this.root).gotoAndPlay(1, "Opening");
}
//Skip_btn functions
Skip_btn.addEventListener(Event.ENTER_FRAME, skipDisable);
function skipDisable(event:Event) {
if (this.currentFrame < 213)
{ Skip_btn.mouseEnabled = false;
} else {
Skip_btn.mouseEnabled = true;
}
if (this.currentFrame > 213) {
Skip_btn.removeEventListener(Event.ENTER_FRAME, skipDisable);
}
}
The problem is that before I could just use "this.currentFrame" as the button was on the same timeline that it controlled, whereas now it's embedded in a MovieClip that is on the main timeline. What can I swap "this" for so I can reference this main timeline? Also, could someone fill me in on what the other "identifiers" are for ".currentFrame", as I'm not too sure how it works. The documentation examples ask for Movieclips such as "MyMovie_mc.currentFrame", but what if you just want to reference a main timeline?
If the button is on main timeline you could just use
this.root.getChildByName("Skip_btn").mouseEnabled = true;
And if you start playing animation on main timeline use
MovieClip(this.root).currentFrame