Danh mục tài liệu

Creating and Referencing Global Elements

Số trang: 4      Loại file: pdf      Dung lượng: 14.58 KB      Lượt xem: 3      Lượt tải: 0    
Xem trước 2 trang đầu tiên của tài liệu này:

Thông tin tài liệu:

Việc tạo và tham chiếu toàn cầu không có yếu tố phủ nhận rằng, ngay cả với một sự hiểu biết triệt để các đường dẫn mục tiêu, đôi khi khó khăn để theo dõi họ, đặc biệt là khi bạn có thời hạn trong vòng thời hạn trong vòng thời hạn.
Nội dung trích xuất từ tài liệu:
Creating and Referencing Global Elements < Day Day Up >Creating and Referencing Global ElementsTheres no denying that even with a thorough understanding of target paths, itssometimes hard to keep track of them—especially when you have timelines withintimelines within timelines. As you might imagine, target paths can become quite long. Tohelp simplify things, ActionScript provides access to a global object. As a global element,this special Flash object exists apart from any specific timeline and is omnipotent inrelation to your Flash project: you can reference it from any timeline without a target pathand take advantage of its power to reference other timelines and the variables, functions,and other dynamic elements they contain.Lets take a look at how you create a global element and how you convert an elementwith a target path into a global element.Creating a global element, such as a variable, is as simple as this:_global.myVariable = hello;Because this variable is now a global element, you can reference it from any timeline byits name:_root.myMovieClip_mc.favoriteGreeting = myVariable;or_root.favoriteGreeting = myVariable;You can also use the global identifier to create global functions (see Lesson 5, UsingFunctions):_global.myFunction = function(){ //actions...}To call the function from any timeline, use its name:myFunction();The same syntax is used to create instances of objects (see Lesson 4, Using ObjectClasses):_global.myDateObject_date = new Date();You can easily convert an element with a target path (such as a movie clip instance) sothat it can be referenced globally—that is, without a path. For example, suppose a movieclip instance in your project has a target path of _root.car_mc.engine_mc.piston_mc. Ifyou want to make it easier to reference this instance, you give it a global address:_global.myPiston_mc = _root.car_mc.engine_mc.piston_mc;To control that instance, you simply reference its global address from any timeline:myPiston_mc.play();Is there a benefit to using a global element? Its a matter of preference as well as what agiven project dictates. In general, though, any element you use frequently or thats usedby a number of timelines is a good candidate for becoming a global element.One thing to keep in mind, though, is that naming collisions can occur when you useglobal elements—that is, an element in a timeline (for example, a variable) may end upwith the same name as a global element. For example, suppose you have a global variablenamed myVariable. Suppose you also have a movie clip instance namedmyMovieClip_mc, which contains a variable named myVariable. You have a globalvariable and a variable in a movie clip instance, both with the same name.You would have a problem if myMovieClip_mc attempted to reference the globalvariable named myVariable using this:myVariableThis is because this timeline has its own local variable named myVariable. If you use thissyntax, Flash wont be able to tell whether the script is referencing the local variable(within the movie clip instance) or the global one—a conflict it resolves by automaticallyreferencing the closest variable to the timeline itself, which is the local one.An easy way to avoid these naming collisions is to preface global element names with asmall g:gMyVariablegFavoriteColorTIPAnother thing to remember is that a global element uses memory that can be freed onlyby deleting the global element. Therefore, using lots of global elements may not be anefficient use of memory. < Day Day Up >