Skip to content

Instantly share code, notes, and snippets.

@Aupajo
Created February 8, 2009 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Aupajo/60142 to your computer and use it in GitHub Desktop.
Save Aupajo/60142 to your computer and use it in GitHub Desktop.
easy actionscript movieclip switching
/*
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