View Full Version : How to get an object to remember its state when returning to its frame
cauchy
August 28th, 2008, 02:12 AM
Let's say you begin with a rectangle placed manually on the stage on frame 1. On the frame script you move its coordinates. Next you leave the frame to frame 2. You do something in frame 2, then return to frame 1. The rectangle is now back at its original coordinates and forgets where it had been moved in code. Is there anyway to get the rectangle to return to its last prior state without a lot of hacking to explcitly store and restore the location? Thanks.
Not sure why I can't edit text here after I've written it, but just to clarify, the frame script that moves the rectangle in frame 1 is assumed to only be operative the first time through, or it could be something like rect_mc.x += 10, so its location should always be increasing each time control returns to frame 1.
glosrfc
August 28th, 2008, 08:10 AM
Frame 1:
if (a == undefined) {
a = 20
}
myRect._x = a;
Frame 2:
a += 10;
However there are better ways of moving an MC without resorting to swapping back and forth between frames:
Frame 1:
myRect._x = 20;
myRect.onEnterFrame = function() {
this._x += 10;
if (this._x == Stage.width) {
delete this.onEnterFrame;
}
};
cauchy
August 28th, 2008, 07:25 PM
Thank you Glos. I realize now that the question as I posed it was a bit too simplistic for the problem I have. What I actually have is a number of complex movie clips on stage that have their intial states changed in various ways. It is a bit unrealistic to attempt to restore every possible condition on the multiple clips using explicit coding, so I'm looking for some magic way to get the clips to show their last states before leaving the frame.
As a simple example, if we change the color of the clip in frame 1, go to frame 2, then return to frame 1, is there some way for the clip to reinitialize to its last state without explicitly setting the color in code. In reality, I have multiple clips at multiple locations, multiple colors, and multiple other states that is a bit problematic to explicitly restore individually.
Interestingly, these movie clips are linked to class files, and I can see in debug mode that all the states for these clips are intact (by looking at the variable contents), but the problem is that the visual representation on stage is no longer synchronized with these variables.
Frame 1:
ActionScript Code:
if (a == undefined) {
a = 20
}
myRect._x = a;
Frame 2:
ActionScript Code:
a += 10;
However there are better ways of moving an MC without resorting to swapping back and forth between frames:
Frame 1:
ActionScript Code:
myRect._x = 20;
myRect.onEnterFrame = function() {
this._x += 10;
if (this._x == Stage.width) {
delete this.onEnterFrame;
}
};
glosrfc
August 28th, 2008, 07:35 PM
Can you not create a function which, when the MC leaves the stage, stores their existing states as properties of the object?
After all, that's the very heart of OOP
http://www.debreuil.com/docs/ch01_Objects.htm
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.