ActionScript 3.0 - 2 gotoAndPlay() commands in one function - function

I have the following Adobe Flash (ActionScript 3.0) movie:
When a button is pressed I want to play frame 17 to 24, and after this, I want to go back and play frame 10 to 16 in the same animation. I've tried something like this but unfortunately doesn't works:
button.addEventListener(MouseEvent.CLICK, buttonClick);
function buttonClick(event:MouseEvent):void{
gotoAndPlay(17);
gotoAndPlay(10);
}
In other short words: after gotoAndPlay(17); I want to gotoAndPlay(10);
Thanks for your attention!

Try this:
stop();
// Properties.
var queue:Array = [];
var currentBlock:Point;
// Queue a section of timeline to play.
function queueBlock(start:int, end:int):void
{
queue.push(new Point(start, end));
}
addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event):void
{
if(!currentBlock)
{
if(queue.length > 0)
{
// Select and remove first block to play.
currentBlock = queue[0];
queue.splice(0, 1);
gotoAndPlay(currentBlock.x);
}
}
else
{
play();
if(currentBlock.y == currentFrame)
{
// Got to the end of the block, end it.
currentBlock = null;
stop();
}
}
}
Which will let you do this:
// Demo:
queueBlock(17, 24);
queueBlock(10, 16);

Related

as3 flash gotoAndPlay

I am making a small platformer game at flash and I just added the first enemy.
I get this in the "output":
" TypeError: Error #1006: gotoAndPlay is not a function.
at scratch_theGame_nojumping_tryEnemy_fla::MainTimeline/fl_enemyAlerted() "
My code on the enemy layer is the following:
var enemyAlerted:Boolean = false;
var alerted:Boolean = false;
var enemy:Object = new Object();
stage.addEventListener(Event.ENTER_FRAME, fl_alerted);
stage.addEventListener(Event.ENTER_FRAME, fl_enemyAlerted);
function fl_alerted(event:Event):void
{
if (char.x > 400)
{
alerted = true;
}
}
function fl_enemyAlerted(event:Event):void
{
if (alerted = false)
{
enemy.gotoAndPlay("alert");
}
if (alerted = true)
{
enemy.gotoAndPlay("chase");
}
}
My goal here is to make the enemy loop the 'alert' animation from the start until my character has come close enough to be noticed, and then the enemy should play the 'chase' animation. Can anyone help? Thank you in advance.

How to stop multiple Movieclips at a time by play/pause button in AS3

I have 4 Movieclips in timeline.these movieclips have inner animation. they are classically tween in timeline. I have to stop and play these four movieclips at a time by play/pause button. Noted that, I have used the following code for that purposes.
It can stop only one MovieClip which is in the upper layer, and the rest 3 Movieclips can't be stopped by play/ pause button. (I have tried all movie clips are in same instance name and also tried with different instance).
Here is my code:
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
first_sl.play();
this.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
first_sl.stop();
this.stop();
}
You can try like when you are stopping main timeline with stage.stop(); also use mc.stop(); ect. and when u play them use stage.play(); also mc.play();
import flash.events.MouseEvent;
var Playing: Boolean = false;
var lastPosition: Number = 0;
play_btn.addEventListener(MouseEvent.CLICK, onPlayClick);
pause_btn.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void {
if (Playing == false) {
Playing = true;
mc.play();
mc2.play();// other 2 mc's too
stage.play();
}
}
function onPauseClick(event: MouseEvent):void {
Playing = false;
lastPosition = this.position;
mc.stop();
mc2.stop(); // other mc's
stage.stop();
}
And if you are using last position for timeline also use it for mc's so it will be sync..
hope this on help tho :)
To stop the main timeline you can use stop() or MovieClip(root).stop() and to play it again, you can use play() or MovieClip(root).play() :
var playing: Boolean = false;
btn_play.addEventListener(MouseEvent.CLICK, onPlayClick);
btn_pause.addEventListener(MouseEvent.CLICK, onPauseClick);
function onPlayClick(event: MouseEvent):void
{
if (!playing) {
play(); // you can also use : MovieClip(root).play();
mc1.play();
mc2.play();
mc3.play();
mc4.play();
playing = true;
}
}
function onPauseClick(event: MouseEvent):void
{
if (playing) {
stop(); // you can also use : MovieClip(root).stop();
mc1.stop();
mc2.stop();
mc3.stop();
mc4.stop();
playing = false;
}
}
Hope that can help.

AS3 - How to get function to run after currentFrame updates?

I have 2 copies of a MovieClip (mcA) on my main timeline. Within mcA, I have 5 more MovieClips (mcB) and this code:
addEventListener(MouseEvent.CLICK, clickedPoint);
function clickedPoint(e:MouseEvent) {
e.target.play();
setStat(e.target.parent);
}
So I toggle the frame of the mcB that was clicked, and I run a function that references the corresponding mcA on the main timeline.
In setStat, I call another function that checks the currentFrame of all the mcBs in the mcA.
getPoints(stat) {
var points = 0;
for(var i = 1; i <= 5; i++)
{
if(stat["pnt"+i].currentFrame == 2) points++;
}
trace(points);
return points;
}
My problem is that the setStat function seems to run before the currentFrame of the mcB that has been clicked on updates.
How should I change my code so that the mcB that is clicked on registers as having changed its currentFrame when I call setStat?
You could call setStat() after the next frame update. A simple way to do this is to add an ENTER_FRAME event handler which removes itself when first invoked:
function clickedPoint(e:MouseEvent) {
e.target.play();
e.target.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
e.target.removeEventListener(Event.ENTER_FRAME, arguments.callee);
setStat(e.target.parent);
});
}
You can encapsulate this behavior in a function that behaves like setTimeout:
function callNextFrame(target:DisplayObject, callback:Function, ...params:Array):void {
target.addEventListener(Event.ENTER_FRAME, function(e:Event):void {
target.removeEventListener(Event.ENTER_FRAME, arguments.callee);
callback.apply(null, params);
});
}
// Usage:
callNextFrame(e.target as DisplayObject, setStat, e.target);

How to stop a movieClip from playing in Flash CS6 AS3 (with Box2D)

I've spent 14 hours on this problem. This is a basic collision checker that sets a MovieClip animation to play when a collision occurs. The clip is
currentBallObject.clip.
It works. The clips plays. But it repeats over and over.
private function checkCollisions():void
{
var i = balls.length;
while (i--)
{
var currentBallObject = balls[i];
if (currentBallObject.contact)
{
//condition hits ground
if (currentBallObject.ground)
{
currentBallObject.body.SetPosition(new b2Vec2(currentBallObject.startX / PIXELS_TO_METRE, currentBallObject.startY / PIXELS_TO_METRE));
currentBallObject.body.SetLinearVelocity(new b2Vec2(0, 0));
currentBallObject.body.SetAngularVelocity(0);
//currentBallObject.texture.pop();
}
else // it hit player
{
// assign clip texture to current position
currentBallObject.clip.x = currentBallObject.body.GetPosition().x * PIXELS_TO_METRE;
currentBallObject.clip.y = currentBallObject.body.GetPosition().y * PIXELS_TO_METRE;
// whisk old object away
currentBallObject.body.SetPosition(new b2Vec2(currentBallObject.startX / PIXELS_TO_METRE, currentBallObject.startY / PIXELS_TO_METRE));
currentBallObject.body.SetLinearVelocity(new b2Vec2(0, 0));
currentBallObject.body.SetAngularVelocity(0);
currentBallObject.contact = false;
}
}
}
}
I added this code to delete the MovieClip or somehow get rid of it after it has played through once. (42 frames). I also tried to add a frameListener and at least a dozen other suggestions. When I add
stop()
The animation doesn't play. It just loads the last frame. The code I have now is:
private function updateClips():void
{
var i = balls.length;
while (i--)
{
var currentBallObject = balls[i];
if(currentBallObject.clip)
{
var frame:int = currentBallObject.clip.currentFrame;
//trace(currentBallObject.clip.currentFrame);
if(frame == 42)
{
currentBallObject.clip._visible = false;
currentBallObject.clip.removeMovieClip();
currentBallObject.clip.enabled = -1;
}
}
}
}
I've tried counting the frames, putting it a run-once function, a frame exit listener, I am out of ideas. I just want to make a MovieClip run one time through. I also tried putting stop() in the timeline and then the animation didn't play. It just loaded the last frame.
Right now the collisions work but the animations stay on the screen forever, looping forever.
Edit: I got the code by Patrick to run without errors.
I added the event listener with the others like this:
_input = new Input(stage);
...
addEventListener(Event.ENTER_FRAME, oEF);
addEventListener(Event.ENTER_FRAME, update);
time_count.addEventListener(TimerEvent.TIMER, on_time);
time_count.start();
}
And then created a function:
private function oEF(e:Event):void{
var i = balls.length;
while (i--)
{
var currentBallObject = balls[i];
if (currentBallObject.clip.currentFrame >= currentBallObject.clip.totalFrames)
{
currentBallObject.clip.stop();
currentBallObject.clip.removeEventListener(Event.ENTER_FRAME, oEF);
if (currentBallObject.clip.parent) currentBallObject.clip.parent.removeChild(currentBallObject.clip);
}
}
}
But I still get the same problem as any other result. The MovieClip disappears on contact without the animation happening.
Through debugging I've learned more. The value of currentFrame starts off going 1-40 then stays at 40 for the rest of the execution.
So the MovieClip is always on the last frame for every object.
if clip is a MovieClip then you can use clip.totalFrames in an enterframe listener.
function oEF(e:Event):void{
if (this.currentFrame >= this.totalFrames){
this.stop();
this.removeEventListener(Event.ENTER_FRAME, oEF);
if (this.parent) this.parent.removeChild(this);
}
}
this.addEventListener(Event.ENTER_FRAME, oEF);
I figured it out.
if (currentBallObject.clip.currentFrame == currentBallObject.clip.totalFrames)
{
trace("Frame before kill: ", currentBallObject.clip.currentFrame);
currentBallObject.clip.x = 0;
currentBallObject.clip.y = 0;
}
The above block goes right after
var currentBallObject = balls[i];
It checks if the movieClip is on the last frame and then gets rid of the clip by setting clip.x & clip.y to 0. I could find no other way to stop the movie in this particular case.

Flash - Play movie clip in reverse?

I'm trying to get a movie clip to play in reverse when I mouse_out from it (it plays on mouse_over).
My actionscript is as follows:
mc.stop();
mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc.addEventListener(MouseEvent.MOUSE_OUT,mout);
function mover(e:MouseEvent):void
{
mc.play();
}
function mout(e:MouseEvent):void
{
//Play in reverse
}
How would I achieve this?
Thanks
The best way would be to use an ENTER_FRAME event listener. Basically this is what you want to do
function mover(e:MouseEvent):void {
stopPlayReverse();
mc.play();
}
function mout(e:MouseEvent):void {
this.addEventListener(Event.ENTER_FRAME, playReverse, false, 0, true);
}
function playReverse(e:Event):void {
if (mc.currentFrame == 1) {
stopPlayReverse();
} else {
mc.prevFrame();
}
}
function stopPlayReverse():void {
if (this.hasEventListener(Event.ENTER_FRAME)) {
this.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}
This would play your MovieClip in reverse until it hits frame 1, then it will stop.
If the motion allows, you can use a Tween (for example if you want to change the alpha, location or scale). On the MouseOut you can call .yoyo() for the Tween, which will play it in reverse.
Something like this:
var tween:Tween;
mc.addEventListener(MouseEvent.MOUSE_OVER,mover);
mc.addEventListener(MouseEvent.MOUSE_OUT,mout);
function mover(e:MouseEvent):void
{
tween = new Tween(obj, "alpha", None.easeNone, 1, 0, 1, true);
}
function mout(e:MouseEvent):void
{
tween.yoyo();
}
TweenLite.to(mc, 2, {frame:1});
If your tween is not all that serious you can literally make a whole new set of frames in reverse and just play it from that keyframe.
That's the poor man's reverse tween.
By using a boolean which works as a trigger and Event.ENTER_FRAME, you can do the following to reverse frames:
//Boolean and Event.ENTER_FRAME
var reversing:Boolean = false;
AddEventListener(Event.ENTER_FRAME, reverse);
function reverse(e:Event){
if(reversing)
{
//mc is displaying previous frame every frame
mc.prevFrame();
//until the currentFrame is 1
if(mc.currentFrame == 1)
reversing = false;
}
}
Your //Play in reverse would set reversing = true;
You can do it passing the movie clip object to find the correct object to reverse. This manner helps you to use with the child you want. You can get it with:
fMoveBack(this.getChildByName("my_clip"));
function fMoveBack(my_clip:MovieClip):void {
this.addEventListener(Event.ENTER_FRAME, playReverse(my_clip));
}
function playReverse(mc:MovieClip):Function {
return function playReverse(e:Event):void {
if (mc.currentFrame == 1) {
stopPlayReverse(mc);
} else {
mc.prevFrame();
}
};
}
function stopPlayReverse(mc:MovieClip):void {
if (this.hasEventListener(Event.ENTER_FRAME)) {
this.removeEventListener(Event.ENTER_FRAME, playReverse);
}
}