Created
February 8, 2009 02:17
-
-
Save Aupajo/60142 to your computer and use it in GitHub Desktop.
easy actionscript movieclip switching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Paste the code into the timeline. Set up your library so each clip has a class, and: | |
switchClip(Menu); | |
switchClip(Film); | |
It also returns the new clip, so you can chain methods like this: | |
switchClip(Film).addEventListener(Event.ENTER_FRAME, function(event:Event) { | |
trace('The film is playing!'); | |
}) | |
Quick and dirty. | |
*/ | |
var currentClip:DisplayObject; | |
// Switches the current clip, assumes a center point orientation, | |
// returns self for chaining | |
function switchClip(clip:Class):DisplayObject { | |
if(currentClip) { | |
if(currentClip is MovieClip) { | |
(currentClip as MovieClip).stop(); | |
} | |
removeChild(currentClip); | |
} | |
currentClip = new clip(); | |
// We don't get valid stage properties until the stage is initialised, | |
// so into an event we go | |
currentClip.addEventListener(Event.ADDED_TO_STAGE, function(event:Event) { | |
currentClip.x = stage.width / 2; | |
currentClip.y = stage.height / 2; | |
event.target.removeEventListener(Event.ENTER_FRAME, arguments.callee); | |
}); | |
addChild(currentClip); | |
return currentClip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment