Skip to main content
Inspiring
February 6, 2019
Answered

Recalling the last frame of a movieclip as the current frame.

  • February 6, 2019
  • 2 replies
  • 456 views

Ok, I am finally moving over from AS2 to AS3 creating an escape game. As you move from frame to frame on the main timeline, I want some tweened movieclips to stay in there last position (frame) where you left them. For example, you unlock a closed door. The tween animation is the door swinging open and the last frame has a stop(); applied to it. Clicking on the open door takes you to the next frame. Going back through the door brings you back to the previous frame. I would like to show the original door movieclip tween on it's last frame - opened instead of resetting to frame 1. The only reference I have is the AS2 code.

This is placed on the first frame of the tween:

if(_root.mcName_mc_lastFrame){

      this.gotoAndStop(_root.mcName_mc_lastFrame);

}

And this is placed on the frame I want to be recalled whenever the player comes back to the room:

_root.mcName_mc_lastFrame=this._currentframe;

Can someone show me the equivalent of this code in AS3? Greatly appreciated!

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

One approach is to create some properties on the main timeline itself so you can check the states of things when you move forth and back.

Here is a sample of how you can achieve this.

AS3 code:

[Frame 1]

import flash.events.MouseEvent;

function checkSave():void

{

    if (!this.started)

    {

        this.states =

        {

            door:

            {

                open:false

            }

        };

      

        this.started = true;

    }

  

    setDoor();

}

function setDoor():void

{

    door.addEventListener(MouseEvent.CLICK, checkDoor);

    if (this.states["door"].open)

        door.gotoAndStop(door.totalFrames);

}

function checkDoor(e:MouseEvent):void

{

    if (e.currentTarget.currentFrame == 1)

    {

        e.currentTarget.gotoAndPlay(2);

        this.states["door"].open = true;

    }      

    else if (e.currentTarget.currentFrame == e.currentTarget.totalFrames)

        gotoAndStop("darkRoom");

}

function backToStart(e:MouseEvent):void

{

    gotoAndStop("start");

}

stop();

checkSave();

[Frame 2]

backButton.addEventListener(MouseEvent.CLICK, backToStart);

FLA download:

animate_cc_as3_keep_states.zip - Google Drive

I hope this helps.

Regards,

JC

2 replies

Inspiring
February 6, 2019

Thank you for a quick reply! And for the .fla example. My fault, I forgot to mention I would like a separate button to open the door, like an elevator button instead of clicking on the door MC itself. Can this be integrated into your existing code?

Thank you.

JoãoCésar17023019
Community Expert
Community Expert
February 6, 2019

Nice!

Sure. No problem.

Here is:

AS3 code:

import flash.events.MouseEvent;

function checkSave():void

{

    if (!this.started)

    {

        this.states =

        {

            door:

            {

                open:false

            }

        };

       

        this.started = true;

    }

   

    setDoor();

}

function setDoor():void

{

    door.addEventListener(MouseEvent.CLICK, checkDoor);

    if (this.states["door"].open)

        door.gotoAndStop(door.totalFrames);

    else

        button.addEventListener(MouseEvent.CLICK, callElevator);

}

function callElevator(e:MouseEvent):void

{

    if (door.currentFrame == 1)

        door.gotoAndPlay(2);

}

function checkDoor(e:MouseEvent):void

{

    if (door.currentFrame == door.totalFrames)

    {

        this.states["door"].open = true;

        gotoAndStop("darkRoom");

    }

}

function backToStart(e:MouseEvent):void

{

    gotoAndStop("start");

}

stop();

checkSave();

FLA download:

animate_cc_as3_keep_states_02.zip - Google Drive

Regards,

JC

Inspiring
February 7, 2019

Hey, Thanks! It worked great. I'm going to try and apply this method to other ideas as well. I have never seen anyone go the extra mile and produce a flash file to go with it.

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
February 6, 2019

Hi.

One approach is to create some properties on the main timeline itself so you can check the states of things when you move forth and back.

Here is a sample of how you can achieve this.

AS3 code:

[Frame 1]

import flash.events.MouseEvent;

function checkSave():void

{

    if (!this.started)

    {

        this.states =

        {

            door:

            {

                open:false

            }

        };

      

        this.started = true;

    }

  

    setDoor();

}

function setDoor():void

{

    door.addEventListener(MouseEvent.CLICK, checkDoor);

    if (this.states["door"].open)

        door.gotoAndStop(door.totalFrames);

}

function checkDoor(e:MouseEvent):void

{

    if (e.currentTarget.currentFrame == 1)

    {

        e.currentTarget.gotoAndPlay(2);

        this.states["door"].open = true;

    }      

    else if (e.currentTarget.currentFrame == e.currentTarget.totalFrames)

        gotoAndStop("darkRoom");

}

function backToStart(e:MouseEvent):void

{

    gotoAndStop("start");

}

stop();

checkSave();

[Frame 2]

backButton.addEventListener(MouseEvent.CLICK, backToStart);

FLA download:

animate_cc_as3_keep_states.zip - Google Drive

I hope this helps.

Regards,

JC