Today I almost teared myself apart untill I finally found a good solution for creating diffrent instances of a movieclip from the library! or at least I think this is a good method! The best solution I could come up with anyway and I thought to share it with you as I'm sure a lot of people are looking for a good way to dynamically create different instances of a movieclip being exported for ActionScript in the library.
Here is how the FLA is set up. we have a movieclip in the library, and it's checked for export to ActionScript. (Export in Frame 1 is also checked so the movieclip will be initialized automatically at startup) You are most probably aware that if you are using a high speed internet connection such as broadband you are able to load whole clips in one go. This is really useful when you want to watch a movieclip.
I presume that you already know the fact that if a movieclip has been checked for export to AS from the library, Flash will create a class for it blindly, OR you can setup a class for it manually and just locate the path... if not, maybe you would need to read my other post here, read that first and then get back to here.
Alright, imagine that we have set up our movieclip in the library and the path to its class is something like:
com.myFlashlab.classes.tools.hMenu.BoxStyle1
ok, now whenever you want to create multipie copies from that movieclip (Or any other class, you just need to have the full path to the class and it will work) you need to use the "getDefinitionByName" method like this:
var _myStyleClassName:String = com.myFlashlab.classes.tools.hMenu.BoxStyle1;
var theClassName:Class = getDefinitionByName(_myStyleClassName) as Class;
var theClass:MovieClip = new theClassName();
theClass.name = "box" + i; // i is maybe a variable being used in a loop.
theClass.x = i*50;
addChild(theClass)
I hope this little post will save you a lot of time and you don't have to look for duplicateMovieClip and attachMovieClip equivelents any more
in my last post I tried to create curved boxes with a VERY bad practice! and although after I lernt about "DrawRoundRect" I wanted to continue with my other class but now I decided to use the new method because this one is cleaner and more understanable and as we are going ahead, our application will get very heavy so we must save as much CPU as we can. So now I created the very same curved box with gradient colors but this time we will do it all in codes and we won't create any premade movieclips in the library like what we did in the last post here.
This is the class saved with the name of "CurvedRectacgle.as".
and whenever you want to draw your curved box with the gradient color in it, you may use it like this:
Using this method will save us a lot of CPU and also KB as we are not using all those movieclips in the library and besides that, this class all uses the Sprite and no movieclips involved here I'm sure this version is a lot better, don't you?
Have you ever wished you could have a resizable curved cornered box which you could easily change its gradient colors?! Find the new Class for this here.
well, if so, I'm sure you have tested it and have found out that creating a well shaped curved cornered box in a movieclip will not look good when you try to resize the movieclip! the corners get disordered...
When I give it a second thought, maybe we can add even more features to this simple class... but that's ok for my current use. I hope someone can find this helpful as it gives a good sample on drawing gradients and also about the logic behind how we have saved the curved corners of a box!
Here is the class for a quick review:
If you ever came up with a better version, I'd be glad to have a look also
While walking through AS3 you have certainly faced the problem that you want to use a movieclip from the library in your movie and you do not have any attachMovieClip as you had in AS2!
ok, the problem will be solved when you look at everything including every movieclip as a class. Considering that, let's do a small test and see if we can set a simple movieclip in the library and bring in some other movieclips into the stage and play around with its playhead...
I'm writting this post right at the middle of creating a resizable horizental menu, so I just stopped at the beggining to write this little article and then go ahead with my menu... which I hope I can finish it by tonight
anyway, let's start by creating a new AS3 Flash File from File > New and at the first frame put the follwoing on the action panel:
(Maybe we can sometimes use the document class for our flash creations, but I don't know why I don't like that document class! maybe it's because I still have not found any good reason for using that. it makes me feel a bit limited. so I'm not using it untill I find a good reason for that!)
The first line here actually imports another class into our FLA which will be used as the base for our menu and we will import the library items in that HMenu class and the last line above, simply puts the new class we just called into the stage.
Well, I'm sure you know this, but don't test the movie right now as you still have not created any HMenu class yet, right?!
now save your FLA somewhere and name it anything you want, it doesn't matter but just remember to correct the import path for your own use.
Now open a new .as file from File > New and save it with the name "HMenu.as" in the exact path you entered above and put this code in it:
What you get from this code is that HMenu is actually a MovieClip and it's constructor function will create new instances of another class called Box and adds it into itself, the HMenu class.
maybe you are saying that in this point, we will create another class in a .as file... to some extends, yes, but before doing that, let's get back to our FLA and create our Box class in there by just creating a movieclip in the library!
So, in your FLA, hit Insert > New Symbol and call your new symbol "Box" and select the MovieClip option and at the bottom of that window click the "advanced" button to see the other options.
Check the "Export for ActionScript". in the class field type the path to the same path you used as above when importing the HMenu class. in my case, I typed "com.myflashlab.classes.tools.hMenu.Box".
And leave the Base Class field as it is which should be "flash.display.MovieClip" if you are interested to know what it exactly means by Base Class, you should know that it will actually say that this class you are creating will extend the MovieClip class.
now you're done, hit the OK button and the movieclip will appear in your library. (Maybe it will give you a gentel notice saying that the class file is not avaiable in the path you entered and flash will generate it automatically... or smething like that, well, no problem, let flash do what it needs to do! you go ahead!)
in the Movieclip you just created, create two key frames and in each one draw some shapes or write something, whatever, the intension is to have two frames or more in the movieclip so later we can see how you should control the playhead indicator from the Box class file we will later create.
For right now, test the movie! and you will see that the movieclip you created will be shown on the stage! and of course it loops through the keyframes you created!
If you still have any doubt on why it's showing on the stage, let's have a quick review on what we did so far... First on the FLA we created a class called HMenu and added it to the stage. then in HMenu class we called in a class called Box like this
var myBox:MovieClip = new Box();
** notice the name of the class "Box".
And finally we created a movieclip symbol in the FLA and named it "Box". So flash understand what to do and creates the .as file in runtime (We don't see it but it is there actually!) and that's it, the library movieclip will be used.
ok, nice and easy till here, and now we want to create a Box.as class file that we will actually be able to see so we can control our library symbol! This will be so easy, just open a new .as file and save it with the name of Box.as in the exact same place you used when creating the Symbol in the library... and insert the fowwloing code into the Box.as file.
Test your movie and the playhead indicator will stop at the first frame simply because you had told the movieClip class to stop();
That's all. I'm sure you will find this articla very helpful when you will be dealing with some situations when you want to create some instances of a same movieclip in a for loop...
I will write more about this on my next post when I actually finish creating my HMenu class.
in my other post on http://www.emstris.com/2009/01/custom-event-listeners/ I talked about how you can create a custom event listener and I did mention how easy it was... but now that I have managed to learn a lot more about them, I can tell that creating these custom events are not only easy but also VERY easy and straight
This is what I was trying to do (I did this in my DropDownMenu class which will write about it in my next post).
I wanted my item list class to have some listeners so when ever a user selects an item from the drop down menu, it would understand that event and sets the data and closes the menu...
Thanks to ultrashock.com I found a very straight answer for this and here I try to briefly sumersize everything quickly.
Before anything, you must know that you can dispatch events using the dispatchEvent() method only if you are extending from EventDispatcher class. Don't be scared! if you're class is already extending Sprite or MovieClip then you are also good to go with the rest because Sprite and MovieClip also extend from EventDispatcher class and considering that, you will be able to use the dispatchEvent() method with no problem.
alright, now that you you know this, take a look at the below sample code and have a few seconds thinking about it:
and below will be how you may use it:
Note that you don't have to necessarily use the dispatchEvent() method in a setter function! common, be a little creative. in the above code, the main consentration is on changing the variable "name" so as soon as you change it, the listener will be notified... you can add the line
dispatchEvent( new Event( "nameChanged" ) );
to anywhere in your code that your target change is happening and that's it.
I think it would be worthy to have a look at how I have used the above code in my itemList class:
You see, whenever a new box is selected in the item list, the object which holds the data will be called and the new data will be inserted into it and exactly the same place we have placed our dispatchEvent() method.
Now we can listen to this change! That easy. let me give you a sample... simply like this:
and "onItemChanged" will be function which can do whatever you wish that event handler to do
Yeah, me too, me too. I couldn't beleive how easy it was to create my own listener!
The examples above will surely clear you about how you can create your own listeners but in my dropdown menu class you will see it more and we will actually do something useful with it.
It's really a shame that I spend a couple of good days searching for this! but that what makes coding beautiful, don't you think so?