AS3 move object back to first frame from current position in tween - actionscript-3

I have an object with an instance name of arrow that moves across the page on a motion tweet when a button is pressed. When the button is released I want the arrow to move back to the start of the tween frame by frame.
I have set up an if statement when the button is released and its current frame is more than 1 to go back 1 frame.
So far the arrow is ignoring the if and stopping due to the else statement.
btn.addEventListener(MouseEvent.MOUSE_OUT, out);
function out(e:MouseEvent){
if(arrow.currentFrame > 1){
arrow.gotoAndStop(arrow.currentFrame - 1);
}else{
stop();
}
}

You would probably be better off making another tween on the timeline that goes backwards then just playing that. Another option is to use a third party tweening library like tweenlite since in there you can easily setup animations to run forward or in reverse.
Are you sure arrow is the name of the movie clip that you want to run the frames backwards on, or is that the instance of the element that changes over some other movie-clips timeline?

Related

Actionscript-3 looping issue--Adobe Animate CC

My employer decided they wanted me to start doing animation with Adobe's new "Animate CC" application. My issue is that I don't know how to loop my animation outside of the Adobe Animate environment. I am new to Adobe Animate CC and ActionScript, unfortunately, so I will probably need a relatively basic answer to understand why my solution isn't working. From what I can tell, my ActionScript code is being ignored by the IDE completely.
In the IDE and in the browser test command, the animation plays beyond frame 100, to the end, and then flashes a frame of white before repeating. I need it to loop without this white frame interrupting the screen, whether that be through a loop or some other means that I'm just not aware of.
For context: my project has about 100 layers of content and I'm unfamiliar with how this program works. I've thoroughly searched the web for tutorials on how to do what I need to do, but I've come up empty handed.
I have an actions layer among my motion tweens and other layers
https://gyazo.com/6e0b8502d98b6c9903bb96ac3a939bae
I've been trying to use gotoAndPlay(0) at frame 100 to start the animation over from the beginning.
https://gyazo.com/704ee7158bae6dfd149b6283cfa33451
Basically, how do I use Action-Script in Adobe Animate CC in order to infinitely loop my animation until closed?
Thanks everyone.
Your flicker may be a result of having an extra blank keyframe on one of your layers.
Assuming that you don't have any additional scripts to stop your animation (e.g. stop()), the Timeline should loop automatically whether your animation is inside a MovieClip or on the main Timeline. You shouldn't have to put any script on your timeline or in a separate AS file to make an animation loop. I would suggest this method.
Additionally, although you have the code specifying that you want it to go the first frame, it will ignore your call because the timeline is still playing and therefore the priority. One way you can combat this is by adding a stop(); function and a delay timer that contains your gotoAndPlay(0) function. This will take focus away from playing the Timeline and will allow you to execute your script. I wouldn't suggest this method because it seems a bit redundant.
However, if you're curious one way that you could approach this is shown below, simply add this script to the frame you want the animation to restart at.
//Stop the Timeline
stop();
//Create a delay timer for 5 miliseconds that is executed once
var timer:Timer = new Timer(5,1);
//Add an event listener that calls once the timer is complete
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerHandler);
//Start the timer
timer.start();
//Timer handler that is called once the delay timer is complete
function timerHandler(event:TimerEvent){
//Go to and play the first frame
gotoAndPlay(0);
}

How to make a movieclip play on a specific frame in AS3?

I'm incredibly rusty at Flash having not touched it in probably 10 years and can't seem to figure this out, or find it online:
I have a MovieClip with two layers, each having a Shape Tween. Basically its a Door that opens and closes.
I dropped it onto the main timeline but now I need it to start and stop. This is where I'm now struggling since the last time I used Flash actions could go on specific keyframes.
I made a new layer called actions just to keep things organized and currently have:
barrier1.stop();
I just want something that lets me state a frame, say 57 to have barrier1 start playing on. Tried using play(); and Event.ENTER_FRAME with no luck. How would I set this up?
Well it is easy with the instance name of your movieClip
barrier1.stop(); // Stops the movieClip
barrier1.play(); // Resumes
barrier1.gotoAndStop(12) // Goes to 12nd frame and stop
barrier1.gotoAndPlay(12) // Goes to 12nd frame and play
barrier1.currentFrame // returns barrier currentframe
For capturing frame from scene level:
this.addEventListener(Event.ENTER_FRAME,onLoop);
function onLoop(event:Event){
if(barrier1.currentFrame == 57){
trace("BARRIER is in 57. frame");
}
}
Inside on the animation clip on the first frame
var root:MovieClip = this.parent as MovieClip
root.makeStartSceneAnimation()
**in timeline scene level [root]**
function makeStartSceneAnimation(){
/// barrier started to play
}
If you are using timeline, you can add Key frame on the desired frame, and then add stop(); as Action in the action layer. But bear in mind that if you do this in the main timeline - it will stop everything. If you want to stop that MovieClip, then you have to do this inside MoviceClip's timeline.

how to set actionscript code to execute only once

Okay so I have a movieclip called a_mc, if you click the movieclip, it goes to frame 5, and then on frame 5 there is a button called close_btn where if you click the button, it goes back to frame 1 and it is supposed to make a_mc invisible. Here is the actionscript code for frame 1.
stop();
a_mc.addEventListener(MouseClick.CLICK, aClicked);
function aClicked(event:MouseEvent):void {
gotoAndStop(5);
}
and on frame 5, the actionscript code is
stop();
close_btn.addEventListener(MouseEvent.CLICK, closeCLicked);
function closeClicked(event:MouseEvent):void {
gotoAndStop(1);
a_mc.visible = false;
a_mc.removeEventListener(MouseEvent.CLICK, aClicked);
}
see, the problem is, in frame 5, I make a_mc invisible and remove the event listener and go back to frame 1 and on frame one, it always executes the actionscript code so it again creates the event listener and makes a_mc visible. Any idea on how to stop this from happening?
I tried putting the code from frame 1 into a package and then a class and then a constructer method but it is saying
"Syntax error: package is unexpected"
Could you put all the code that you want to execute once in frame 1? - don't call stop() and let it run to the next frame.
Then put the rest of your code in other key frames and don't use gotoAndStop(1) so frame 1 is only called once?
You can try not removing the event listener on a_mc in frame 5, and then in frame 1 check if the event listener is already present (a_mc.hasEventListener()) as a signal that frame 1 has already been shown. Not exactly a 'bets practices' solution, but it might work.
Unfortunately, depending on the actual conents of those clip, and what happens in other frames, it may be the problem you're having is a consequence of the way movieclip object works in flash. When a frame is changed, flash instantiates new objects on the stage (added in new frame), and removes the ones not needed anymore (depending on the contents, but generally it's true). The 'a_mc' object that you manipulate in frame 5 may not be the same 'a_mc' object that is on the stage when you go back to frame 1. It may have been deleted and reinstantiated in the meantime.
To avoid things like that, it would be a better solution to have controlling code in a class outside of the timeline of the animating clip, or at least to keep the state in a separate object. I work in Flash Builder so I can't help you with the details of such organization in Flash Pro (which I presume you're using), but you could probably have all code on the frame 1 of the main clip, and then put the other movieclips with buttons and stuff as children of the main clip. That way main clip can control the state, and know what to show when.

Action Script 3. How to play animation once?

I'm creating flash game and I have animations for character movements. How correctly setup animations to play It only 1 time after action? For example: If I click "Space" button - character jumping and "Jumping" animation starts playing forever, if character stay on the ground jumping animation continue playing. How to stop It when character stay on the ground? Or if I use attack animation It loop forever too.
In normal state character should use Hero.gotoAndStop("staying");
Here is my jumping code:
if (Hero.y_speed > 0 && Hero.hitTestObject(ground))
{
Hero.y_speed = 0;
Hero.x_speed = 0;
if (space)
{
if (true)
{
Hero.gotoAndStop("jumping");//here starts jumping animation loop non stop
stop(); //this not working
Hero.y_speed = -20;
}
else
{
}
}
}
You need to put a stop(); on the last frame of the MovieClip, so that the MovieClip stops playing once the playhead reaches there.
Given that there is an MovieClip on the frame, you need to tell that MovieClip containing the jump animation to stop.
You would need to have a stop on the last frame of that animation. You'd put that stop on that animation's timeline.
edited to answer comment :
You could probably use a line like this to switch to the "staying" animation when complete.
parent.gotoAndStop("staying");
In this case you wouldn't need to have a stop as the playhead would be moving off of that animation in going to the staying animation.
I think this particular post : flash event listener for movieclip end?
gives a particularly good example of extending a MovieClip giving it a much welcomed ANIMATION_COMPLETE event. So if you are looking to handle things via code exclusively, that would be a good direction to choose.

Touch movement will not animate

Ok, so I'm in the process of developing a game in Adobe Flash (air) it will be a mobile game.I asked this question a while ago, but animation doesn't work so know I'm back.Anyway, the problem I was having was that there was no continuous movement when I held down a button, so I had to constantly keep pressing the button to move.the person told me to use ticks, so it's how I built the movement engine, all around ticks.When I go to add my animation, it goes once and just stops (the animation) here is my code:
myButton.addEventListener(MouseEvent.MOUSE_DOWN,mouseDown);
function mouseDown(e:Event):void {
stage.addEventListener(MouseEvent.MOUSE_UP,mouseUp); //listen for mouse up on the stage, in case the finger/mouse moved off of the button accidentally when they release.
addEventListener(Event.ENTER_FRAME,tick); //while the mouse is down, run the tick function once every frame as per the project frame, this is where I add the players animation.
}
function mouseUp(e:Event):void {
removeEventListener(Event.ENTER_FRAME,tick); //stop running the tick function every frame now that the mouse is up
stage.removeEventListener(MouseEvent.MOUSE_UP,mouseUp); //remove the listener for mouse up
}
function tick(e:Event):void {
//do your movement
}
What I'm saying is, is there anyway to get around the problem? How can I animate with the tick!
Like others, I find the code perfectly fine. Since, I don't have access to entire code and timeline, I have the below guess about the problem:
Since, you want to start animation only on mouse down, then you must be having a stop() action in the first frame of your animation. (Please correct me if this isn't the case). The last frame of your animation must be having gotoAndPlay(x) action again for looping.
Based on above assumption, when your animation loops, it again reaches a frame which has stop() action already defined.