Showing posts with label _currentframe. Show all posts
Showing posts with label _currentframe. Show all posts

Monday, September 19, 2011

Flash reverts MovieClips to Original Negative Depth when moving to a Previous Frame

I have been working with Flash for over six years and there are still things that surprise me about Actionscript 2 - surprise but not delight!  This week, I was trying to create a new layer in between two existing ones on the stage, using code only.  I figured that I could create a new MovieClip and swapDepths to put it in the correct location.  Everything worked great except that my created MovieClip would disappear as I navigated to a previous frame.

It bothered me so much, that I created a very simple tester of the problem.  It has an actions layer and one layer with a thick red line drawn on it.  It has a text box that reports which of the four frames you are on.  It has a green button which goes to the next frame and a red button which goes to the previous frame.

On the first frame, I create an empty MovieClip with code, swap its depth with the stage MovieClip and draw a blue line on it.  Everything looks great as you go from Frame 1 to 2 to 3 to 4, but go back from Frame 3 to Frame 2 or from Frame 4 to Frame 3 and the blue line disappears.  If you trace the depth of the stage MovieClip, you see that Flash re-establishes it at its original depth when you navigate to the previous frame. Since your code-created MovieClip is at that depth, it gets wiped out. At least this is my current explanation.

Maybe you have a better theory and want to download the .fla to check it out.  Regardless, I currently think that there is no good way to simulate a timeline layer with code.

The code is:

stop();
trace("library_mc starts at depth: "+this.library_mc.getDepth());
this.library_mc.lineStyle(3,0xFF0000,100);
this.library_mc.moveTo(50,100);
this.library_mc.lineTo(200,150);
if (counter == undefined) counter = 0;
counter++;
trace("creating code_mc for the time: "+counter);
this.createEmptyMovieClip("code_mc", this.getNextHighestDepth());

this.code_mc.swapDepths(this.library_mc);

this.code_mc.lineStyle(1,0x0000FF,80);
this.code_mc.moveTo(200,50);
this.code_mc.lineTo(50, 150);

green_btn.onRelease = function(){
	this._parent.nextFrame();
}

red_btn.onRelease = function(){
	this._parent.prevFrame();
}
frame_txt.text = "I am on frame 1";
this.onEnterFrame = function(){
	frame_txt.text = "I am on frame " + _currentFrame;
	trace("library_mc depth "+this.library_mc.getDepth());
}

Tuesday, September 2, 2008

How does Flash Pass Control as it gotoAndPlays frames?

One of the pleasures of working on CLIPS is the chance to work with some top-notch Ontario educators, like Greg Rodrigo of Georgian College. As part of this summer's work, we we trying to figure out how actionscript is executed as control is passed to different frames and the onEnterFrame method is called. Greg created a little test file similar to the following:

Create seven keyframes and put the following code in each frame:
  1. onEnterFrame = function() {
    trace("I am on frame " + _currentframe);
    }
    trace("Frame 1");

  2. gotoAndPlay(4);
    gotoAndPlay(3);
    trace("Frame 2");

  3. trace("Frame 3");
    gotoAndPlay(5);

  4. trace("Frame 4");
    gotoAndPlay(1);

  5. trace("Frame 5");

  6. trace("Frame 6");
    delete onEnterFrame;

  7. stop();
    trace("Frame 7");
So here is what the teacher would ask you to do:
Determine what Flash's output window will contain when this file is tested.
We learned a lot about how Flash operates by trying to make this prediction.

STOP: Try it yourself now.






If that question is too hard or open-ended, here are some other questions that might be simpler:
  1. What will the output be the first time the onEnterFrame method is executed?
  2. Will the first output "Frame 2" occur before or after the first output "Frame 4"?
  3. Why does "I am on frame 5" not appear in the output window? (I am not sure what the answer to this question is, so perhaps it is not simpler.)
Please feel free to leave a comment about the last question.

If you don't feel like typing this in yourself, I have posted the .fla source file.