Animation and Effects with Macromedia Flash MX 1002004 [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Animation and Effects with Macromedia Flash MX 1002004 [Electronic resources] - نسخه متنی

Jen deHaan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید




Understanding Global Variables in Flash


ActionScript allows you to create global variables, which are variables that are accessible on every Timeline and scope within a Flash document. You'll use some in this project's menu.

By putting variables in the global scope, you don't have to worry about providing a fully qualified path to a function in a specific scope. Global variables also allow you to modify values directly rather than having to pass them to a function as an argument and return the modified values. To create a global variable, you use the identifier _global before you type the variable name. You do not use the var syntax when you create a global variable; therefore, to create a global variable, you could type the following:


_global.myVariable = 10;

Chapter 6 on ActionScript fundamentals.

Exercise 2: Setting global variables


The dynamic menu system uses certain global variables to track the current X coordinate where the next button should be placed. After you add each navigation button to the Stage and position it, the variable containing the X coordinate increments (increases) by the width of the button. This helps ensure that no buttons overlap.

Using global variables in small files helps you create this menu quickly and easily. Using global or private variables could be a security issue in larger applications, and can also make application maintenance difficult. If you end up building such applications, consider adopting an application architecture that allows you to avoid global variables.


1. Select frame 1 of the actions layer; then add the following code to the first line of the Actions panel:


_global.navXPos = 20;

This line of ActionScript declares a variable named navXPos in the _global scope and assigns a value of 20.

2. You can see the variables defined in the _global scope in a few different ways. The easiest way to view global variables is to test the current Flash document in the authoring environment and then select Debug > List Variables. Flash launches the Output panel and displays the following text:


Global Variables:
Variable _global.navXPos = 20
Level #0:
Variable _level0.$version = "WIN 7,0,19,0"
Movie Clip: Target="_level0.my_mc"

You cannot strict type global variables. _global is technically an object that already exists when you create your SWF file. If you attempt to declare a new property in the _global scope at runtime using var _global.navXPos:Number = 20; it results in a syntax error when you try to publish or test the Flash document.

This code tells you that there is one global variable defined, _global.navXPos, with a value of 20. You use this variable, later in this project to keep track of how far the buttons are on the X axis. Every time you dynamically create a new button, the value of _global.navXPos increments to prevent the buttons from overlapping. You initially set the number to 20, so the first button indents 20 pixels instead of being directly on the left edge of the Flash document. If you want the navigation buttons to appear closer to the left edge of the document, you decrease the value of _global.navXPos here; or if you want to shift the navigation further to the right, you increase the value instead.



The Debugger Panel


You can also view global variables using Flash's Debugger panel. To launch the debugger, select Control > Debug Movie. This is the same as using Control > Test Movie, except that Flash displays the Debugger panel. This panel lets you view and change variables within your SWF file. Before you can view global variables, you first need to start the debugger by pressing the Continue button in the Debugger panel. By default, the debugger is paused so you can set breakpoints in your code. When the debugger starts, Flash stops on any line of code that has a breakpoint set. A breakpoint allows you to pause the playback of a SWF file to view which variables are set, as well as their current value in the SWF. This helps make debugging in your Flash document much easier if you're experiencing unexpected results in your documents.

Because the _global scope is technically an object, you can also loop over the structure using a for..in loop to display all the global values, as shown in the following code example:


for (var item in _global) {
trace(item+": "+_global[item]);
}

/ 123