|
|
||||
|
|
Ultrashock Tutorials > Flash MX > Common Component Issues | |||
|
||||
|
|
Common Component Issues |
|
||
|
Coding Practices When creating components it is always best to stick to good coding practices. For components to be useful they must be reusable and extensible. This section will not cover all the good coding practices, but rather, it will cover a few that people have had problems with, for example: using classes for your components, using Object.registerClass(), and remembering to create member variables, not just instance variables. The Importance of Classes Many people still try to create components based on how they created smart clips in the past. Components are best based on classes, which give you a lot of advantages. One benefit is that the component code is stored more efficiently. The functions for the component methods live in only one place—the class prototype—not in every instance of the component. By contrast, with the smart clip approach, each instance declares its own copies of the functions, increasing ram usage. And probably the most important advantage is extensibility. By using classes, new components can take advantage of inheritance, gaining abilities from existing components. This adds to their reusability since components can be extended to do things that were previously outside their power. And finally, if you don’t use classes, you can’t take advantage of the #initclip directive which means you will have to do ugly hacks like back in the Flash 5 days. Object.registerClass() If you are making components, it's crucial to understand Object.registerClass(). Here is a quick review: Object.registerClass() allows you to associate the linkage of the component movie clip(a string identifier) with a class. That way, when the movie clip is dragged on stage or attached, it will be an instance of the class you define. Its syntax is as follows: Object.registerClass(stringname, classref); If you have a MovieClip with the linkage name “FComponentSymbol” and a class FComponentClass, the Object.registerClass() syntax would be as follows: Object.registerClass(“FComponentSymbol”,FComponentClass); #initclip Purpose #initclip 0 This fixes the problem that arose in Flash 5 where you would have to wait a frame per level of nesting for the component to initialize. So when working with inheritance you have to at least set this number to Super class plus one. But I like to leave my self some room for further expansion so I set it to plus three or so. //Foo Movieclip //we set #initclip Foo + 3 Always remember to define your classes in #initclip when working with components, if you do not the class will be redefined in every instance of the component. Scope Naming Conventions You will probably want to follow Macromedia’s standard for naming. Looking at their components, they usually do the following:
Versioning Macromedia does several things when it comes to versioning, so I am going to take an educated guess on the best way to do things. First of all, it's always best to keep the version in a comment at the beginning of the code block. I usually use JavaDoc style to comment my code. Here is an example of a class description: /** That way developers can easily see inline to the code what version they have. This is especially good when using external includes. The other thing you will want to do is to keep something in the library that states its version. Macromedia’s way of doing this is to create a symbol in Flash UI Components/Core Assets/Version and name the symbol “Version – Your Component Name” in this symbol create a TextField and put on the first line the component name, on the second line put version info and date. Macromedia uses a letter and then a two-digit number to define version. Such as “b07” I think b is the release and 07 is the minor version. Below is a screenshot of how this looks.
Simple! Put a space in front of your component name in the library. This will make it appear above all the folders. Nesting Components Nesting components can be quirky if it is your first time doing it. You have to use the attachMovie() method when working with them. Attaching Movieclips Extensibility Tip
You can find the complete source in the Nesting.fla file. Live Preview Live Preview is a very common issue for component developers and a seperate
article/tutorial has been written on this topic. Click
here to be taken to the Live Preview tutorial. |
||||
|
|
©2002 Ultrashock.com Inc. - All rights reserved
|
|