There is no way to prevent a variable from being changed. That's why it's variable :) Constants in other hand are meant to be initialized only once and cannot be changed later on.
The only problem in your logic is using the timeline and executing the same code every time. Your solution is okay. Sometimes people add just a blank key frame, not a whole scene, and at the end of the timeline they put gotoAndPlay(2);
. But the idea is exactly the same as you did - cheers for that! :)
Another solution there is to write something similar to this:
if (!this.initialized) {
// declare everything here, it will be executed only once
this.initialized = true;
}
What this will do is that it will set a root variable (initialized
) to true, and will execute the block only once. The variable won't get changed later on, because there is no code in that frame that will change it again, unlike your code that resets it.
Either ways, the logic is all the same as well as the problem - if you use classes this will almost never happen!
Hope that helps!