Posts Tagged ‘ide’
-
Creating MovieClips dynamically at run-time using the Linkage Class
15th Sep 20096I found myself needing to create a MovieClip dynamically at run-time. But all I had was a string representation of its Class, as set in the Linkage properties for the Symbol. In my situation the string had been stored in an xml file. Now you could use a standard switch/case block to do this, checking the string and creating a new MovieClip as required. But in this case there were hundreds of possible things it could have been, and it seemed a very “hacky” way to do it.
So how do you go about creating an actual display object from just the Linkage Class value? Thankfully it’s pretty easy, and this new bit of code now sits happily in my “everyday functions” collection!
Edit: Updated to be a little more robust, and removed an un-needed cast:
[as]
public function createMovieClipFromLinkageValue(linkageValue:String):MovieClip
{
try
{
var libraryReference:Class = getDefinitionByName(linkageValue) as Class;
}
catch (error:ReferenceError)
{
trace(error);
}if (libraryReference)
{
return new libraryReference();
}return new MovieClip;
}var newClip:MovieClip = createMovieClipFromLinkageValue(“playerFishMC”);
[/as]Make sure you have imported flash.utils.getDefinitionByName.
Simply pass this function the Linkage value as entered in the IDE, and it’ll spit a MovieClip back at you (providing it was a MovieClip in the first place). I’m sure you can see how to extend it to return a Sprite instead, should you require that.
Hire Us
All about Photon Storm and our
HTML5 game development services
Recent Posts
OurGames
Filter our Content
- ActionScript3
- Art
- Cool Links
- Demoscene
- Flash Game Dev Tips
- Game Development
- Gaming
- Geek Shopping
- HTML5
- In the Media
- Phaser
- Phaser 3
- Projects
Brain Food