Languages

Fsharp
ActionScript
xBase
Clean
GPSS
PureBasic
Sieve
Erlang
JOVIAL
Mercury
Linda
DataFlex
PostScript
FoxPro2
VFP
Cobol
Prolog
Jython
Awk
VisualBasic
JavaScript
Matlab
ASP
Haskell
Csharp
D
Smalltalk
Nemerle
Pixilang
Java
SQL
Python
ObjectPascal
Ruby
Perl
Pascal
Assembler
PHP
C
Functions  Add function  Users  Registration  Enter   About  ASCII Table  Our helpers

Flash


1 Referencing Movie Clips in Flash MX

2 Referring to Nested Instances

3 Authoring Instance References with Insert Target Path

4 Storing references to clips in data containers

Referencing Movie Clips in Flash MX



(Page 1 of 4 )

In this third part of a four-part series, you will learn how to store references to clips in data containers, refer to nested instances, and more. It is excerpted from chapter 13 of the book ActionScript for Flash MX: the Definitive Guide, second edition, written by Colin Moock (O_Reilly; ISBN: 059600396X). Copyright © 2005 O_Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O_Reilly Media.

Referring to the Current Instance or Movie

We don’t always have to use an instance’s name when referring to a clip. Code attached to a frame in an instance’s timeline can refer to that instance’s properties and methods directly, without any instance name.

For example, to set the_alphaproperty of a clip namedcloud_mc, we can place the following code on a frame in thecloud_mctimeline:

  _alpha = 60;

Similarly, to invoke the play() method oncloud_mcfrom a frame in thecloud_mctimeline, we can simply use:

  play();

This technique can be used on any timeline, including timelines of main movies. For example, the following two statements are synonymous if attached to a frame on the main timeline of a Flash document. The first refers to the main movie implicitly, whereas the second refers to the main movie explicitly via the global_root property:

  gotoAndStop(20);
  _root.gotoAndStop(20);

However, not all methods can be used with an implicit reference to a movie clip. Any movie clip method that has the same name as a corresponding global function, such as duplicateMovieClip() or unloadMovie(), must be invoked with an explicit instance reference. Hence, when in doubt, use an explicit reference. We’ll have more to say about method and global function conflicts later in in this chapter under “Method Versus Global Function Overlap Issues.”

Note that it’s always safest to use explicit references to variables or movie clips rather than using implicit references. Implicit references are ambiguous, often causing unexpected results and confusing other developers reading your code.

Self-references with the this keyword

When we want to refer explicitly to the current instance from a frame in its timeline or from one of its event handlers, we can use the this keyword. For example, the following statements are synonymous when attached to a frame in the timeline of our cloud_mcinstance:

  _alpha = 60;        // Implicit reference to the current timeline
  this._alpha = 60;   // Explicit reference to the current timeline

There are three reasons to usethisto refer to a clip even when we could legitimately refer to the clip’s properties and methods directly.

  • First, explicit references are easier for other developers to read, because they make the intention of a statement unambiguous.
  • Second, when used without an explicit instance reference, certain movie clip methods are mistaken for global functions by the interpreter. If we omit thethisreference, the interpreter thinks we’re trying to invoke the analogous global function and complains that we’re missing the target movie clip parameter. To work around the problem, we usethis, as follows:

      this.duplicateMovieClip("newClouds_mc", 0); // Invoke a method on an instance

      // If we omit the this reference, we get an error
      duplicateMovieClip("newClouds_mc", 0);      // Oops! 
  • Third, usingthis, we can conveniently pass a reference to the current timeline to functions that operate on movie clips:

      // Here_s a function that manipulates clips
     
    function moveClipTo (theClip, x, y) {
      
    theClip._x = x;
      
    theClip._y = y;
     }

     
    // Now let_s invoke it on the current timeline
     
    moveClipTo(this, 150, 125);

    New and experienced object-oriented programmers alike, take note: the meaning ofthisinside a method is a reference not to the current timeline but to the object through which the method was invoked. If an object’s method needs to refer to a specific movie clip, a reference to that clip should be passed to the method as a parameter.
1 2 3 4
Preparing the Next Generation of STEM Innovators

The National Science Board is releasing a new report, "Preparing the Next Generation of STEM Innovators: Identifying and Developing our Nation's Human Capital," during a press conference at the National Press Club on September 15, 2010 at 9:45 a.m.

The development of our nation's human capital through our education system is an essential building block for future innovation. Currently, the abilities of far too many of America's young men and women ...

More at http://www.nsf.gov/news/news_summ.jsp?cntn_id=117669&WT.mc_id=USNSF_51&WT.mc_ev=click


This is an NSF News item.

PycckaR
BepcuR


Articles
Articles


Library
Library


Downloads
Downloads

Google Chrome Golf 6
 © Internet, books, teachers and Rudevich Alexander brains.