'Restart flash on click
I'm trying to restart my flash piece with a restart button. I use gotoAndPlay(0), but nothing happens. I'm sure the click event handler is being called because I used a trace statement to verify.
rs.addEventListener(MouseEvent.CLICK, restart);
function restart(event:MouseEvent):void {
gotoAndPlay(0);
}
Solution 1:[1]
The first frame is frame 1, not 0.
Not sure why adobe decided against making frames zero-based, but they did :/
Solution 2:[2]
If you have added objects to the stage, like buttons or graphics, but never actually used the stage's timeline, the stage will start and stay at the first frame. So 'gotoAndPlay' wont work in this case. It would be only useful to restart an animation anyway, as it won't reset any code on its own.
You need to decide what parts you actually want to reset and what parts you can keep. You probably don't want to remove assets from memory you loaded at the beginning just to download them again. Some objects may be kept, others should be removed.
As far as I know there is no easy way to reset a flash application, other than maybe reloading the whole page. Here are some general steps to 'reset' an application by hand:
- Create a method for your initialization code:
object creation, adding to the display list, adding event listeners. - On a click: remove all objects from the stage, remove all their event listeners.
- Call the initialization method again.
Ideally you set the references in your init method to a new variable so the old ones can be garbage collected. Depending on the code structure you may have to manually set some to null. Make sure you don't keep any references to objects you don't need any more.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jonatan Hedborg |
| Solution 2 | kapex |
