Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Coding for a Time-based Medium  : Page 3

Preparing traditional developers to develop in Flash requires some new terminology and careful separation of code and interface elements.


advertisement
Always work from the perspective that the Flash code you develop will need to integrate with design and animation elements later. Jumbling code and design is the greatest threat to a handoff-style workflow between artists and developers. You should divorce core code from the interface and the timeline as much as possible. When user actions or the system invoke application logic, make those calls as simple as possible.

One of the best ways to avoid confusing code and code scope in nested layers of Flash movies is simply to move all core application code to one location in the movie—typically to a single frame, in the movie root or to a layer just off of the root. Doing this allows developers to author code-only Flash movies independently from animations, navigation, or other interface elements. Moving core logic away from timeline elements also averts most of the problems associated with code scope and timeline organization.

Consider a Flash application that includes a member login function. Even when application developers don't know how or where login GUI widgets will appear in the interface, they can still code an ActionScript method that facilitates the login process by abstracting the process to accept a user name and password. The following partial method takes login parameters and configures an XML HTTP request to process the login.

_global.login = function (strScreenname, strPassword) { var request = new XML("<Login screenname=\"\" password=\"\" />"); request.firstChild.attributes.screenname = strScreenname; request.firstChild.attributes.password = strPassword; request.sendAndLoad("http://localhost/login"); ... }

Flash offers several ways to invoke methods such as the one shown above. The simplest is to call the method directly within a frame action. When the playback head enters the frame, the player interprets and executes the script; more commonly, user or system changes invoke application logic.

Flash MX offers an improved event model for user actions closely resembling that of Visual Basic or Java. To delegate actions to events, the developer could first load the handler code inside the movie root or in an identifiable layer off the main timeline such as _root.Membership. Flash MX even lets you declare an object, function, or variable as global using the _global declaration as shown above. An event action, perhaps from a button click that initiates the login process, could then invoke this method from any timeline and scope, as long as an inner scope identifier doesn't take precedence.

on(mouseUp) { login(myLayer.screenname, myLayer.password);}

Flash MX's new event model also lets developers define event handlers using relative or absolute references to the object. For example, the following code handles a button click and invokes the login method.



_root.myLayer.myButton.onClick = _root.Membership.login( _root.myLayer.myLayer.screenname, _root.myLayer.myLayer.password);

To handle user and system actions, Flash MX offers new listener objects. Like Java 1.1 delegation, Flash notifies listeners when requested events fire. Flash listeners support multicasting, where one event invokes several actions. The following code would cause a login validation every time the movie stage resizes.

myListener = new Object(); myListener.onResize = login(myLayer.strScreenname, myLayer.strPassword) Stage.addListener(myListener);

For more information on XML messaging in Flash, see my article "Drive your Flash Front-Ends with SOAP" on the DevX WebDev Zone. For more on the Flash MX event model and Flash MX listeners, visit Macromedia's Flash Support Center



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date