Beginning Flash, Flex, and AIR Development for Mobile Devices

Beginning Flash, Flex, and AIR Development for Mobile Devices

by Jermaine G. Anderson
     
 
Easily create cross-platform, native applications for mobile devices using Flash

Adobe Flash allows you to develop the rich, creative, interactive media experiences for the web. This easy-to-follow guide covers everything you need to know to create rich mobile applications using Flash. Packed with examples, this book shows you how to build both Web-embedded

Overview

Easily create cross-platform, native applications for mobile devices using Flash

Adobe Flash allows you to develop the rich, creative, interactive media experiences for the web. This easy-to-follow guide covers everything you need to know to create rich mobile applications using Flash. Packed with examples, this book shows you how to build both Web-embedded Flash and packaged AIR application for mobile devices. You?ll discover how Web-based Flash applications are able to run across multiple platforms, ranging from mobile devices to televisions to PCs. Plus, hands-on instructions detail how to leverage the unique hardware and software features that are available to each various device and platform.

  • Serves an ideal starting point for learning to create rich, interactive applications for mobile devices
  • Includes hands-on examples that demonstrate how to best use the variety of hardware and software features
  • Escorts you through mobile app concept to completion with sample code and step-by-step instructions
  • Discusses such topics as building AIR applications for Android, BlackBerry, and Symbian devices; enabling multitouch; developing for multiple screen sizes; maintaining app data; and much more

Start building Flash apps for mobile devices today with Beginning Flash Development for Mobile Devices.

Product Details

ISBN-13:
9780470948156
Publisher:
Wiley
Publication date:
10/25/2011
Pages:
400
Product dimensions:
7.42(w) x 9.10(h) x 0.79(d)

Read an Excerpt

Beginning Flash, Flex, and AIR Development for Mobile Devices


By Jermaine G. Anderson

John Wiley & Sons

Copyright © 2011 John Wiley & Sons, Ltd
All right reserved.

ISBN: 978-0-470-94815-6


Chapter One

An Introduction to Flash, Flex, and AIR

WHAT YOU WILL LEARN IN THIS CHAPTER:

* An overview of the Adobe Flash platform

* Outlining the key concepts of the ActionScript 3.0 language

* Exploring the Flex framework and MXML components

* A brief introduction to Adobe AIR

In this chapter you'll take a look at each of the core elements of the book: Flash, Flex, and AIR.

First you'll cover a number of the core aspects of Flash and the programming language ActionScript 3.0, which this book uses.

You'll then explore the key features of the Flex framework and MXML components, looking at examples through code snippets.

Lastly, you'll be introduced to features of AIR, the Adobe Integrated Runtime.

ADOBE FLASH

Adobe's Flash platform consists of several Flash-based runtime clients: Flash Player, Flash Lite, and Adobe AIR. Each run time has its own set of functions and APIs that are specific for that run time.

The Flash platform also encompasses a component framework, Flex. All these elements, the runtime clients and component frameworks, support and utilize the SWF format.

Flash is predominantly used for the web deployment of rich content and applications. It is installed as a web browser plug-in, and can also run content in standalone mode. The Adobe Flash logo is shown in Figure 1-1.

Flash on Mobile Devices

There are currently two ways in which Flash has dedicated support on mobile devices. These involve using Flash Lite and Flash Player, respectively.

Flash Lite 4.0

Flash Lite runs Flash content and applications intended to run on performance-limited mobile devices. Flash Lite offers a different set of capabilities compared with Flash Player. Until recently Flash Lite supported only the ActionScript 2.0 (AS2). Currently Flash Lite is in version 4.0 and the run time now supports ActionScript 3.0 (AS3).

To learn more about Flash Lite and how you can create mobile applications using the technology, check out the book Professional Flash Lite Mobile Application Development, by Jermaine Anderson (Wrox, 2010).

Flash Player 10.x

Flash Player 10.1 was the first release of Flash Player aimed at supporting the development of content and SWF format deployment to both traditional web browser and mobile devices.

At the time of writing, a beta for Adobe Flash Player 11 was underway, allowing developers to preview new and enhanced features targeting the next release of the run time. With the potential for new APIs to be dropped, none of the features could be covered in this book, but look for an update. For more information, visit the Adobe Labs website (labs.adobe.com/technologies/) and search for Adobe Flash Player 11.

This book centers on the development of Flash content targeting the latest release, Adobe Flash Player 10.3 using AS3.

Flash is fully supported on Google Android and the BlackBerry Tablet OS mobile platforms. Unless you've been hiding under a rock for the past few years, you'll know Flash isn't supported on the Apple iOS platform. However, using AS3 and AIR, you can target your applications to run on the platform via standalone applications.

ACTIONSCRIPT 3.0

AS3 is an object-oriented language for creating media content for playback in the Flash runtime clients' Flash Player, Flash Lite, and Adobe AIR.

ECMAScript

The core of the AS3 language is based on the ECMAScript 4th Edition language specification, which you can view on the ECMA International website at www.ecmascript.org/. You can view the AS3 specification at http://livedocs.adobe.com/specs/actionscript/3.

AS3 has a syntax that is very similar to Java; it is also similar to the widely used JavaScript, the popular language used in web browsers. If you are from either of these programming disciplines, then AS3 will be very familiar.

ECMAScript 4 defines a number of rules for how code is written in AS3, including grammar and syntax. These dictate that code be written in a particular way, which should mean that code written by one developer will be recognizable to another.

A number of core programming concepts, such as variables, functions, classes, objects, expressions, and statements, form part of ECMAScript 4 and are inherited by AS3.

ECMAScript 4 also defines several built-in data types for AS3, allowing developers to utilize frequent data types such as an array, Boolean, number, and string.

Key Concepts

You need to grasp a number of important key concepts when programming with AS3; these will stand you in good stead for the rest of this book. Here you'll take a look at some of these concepts.

Classes, Objects, and Instances

A class is what gives an object its properties and the features by which an object accomplishes particular tasks through its methods and functions. A class is essentially a blueprint for an object.

AS3 classes are text-based files identified by the .as file extension. A class is defined by the class keyword, followed by the name of the class, which should start with a capital letter. It is this class name that must match the filename the class is created in. The following snippet shows you an example of a class called Mobile that's being defined:

class Mobile {}

An object represents part of a program that describes a particular thing. That thing could be a shape that has three sides and is colored blue; a film that has a PG rating; or a mobile device that allows you to store contacts. An object can be anything you can think of.

Objects have properties that give them their character and also have methods that allow them to carry out particular tasks.

In AS3, objects are created from classes by instantiating them, calling the new keyword before the class name and parentheses (). The following snippet shows you an example of the Mobile class being instantiated:

new Mobile();

Packages

A package defines the path to a class, which should be uniquely identified in respect to other classes that may have the same class name. Packages also reflect the folder structure.

Ultimately, packages are used to avoid conflicts between classes.

It is best practice to give packages meaningful names. Grouping similar classes together in a package is common practice and makes it easier to search and utilize different classes when programming.

The Mobile class could well be placed in a package called devices, sitting alongside another class called Tablet, if that class were to be created.

package devices { class Mobile {} }

While this example shows a fully qualified package, the naming convention is usually more granular, and with the package name defined for a class set as a dot-delimited, reverse-DNS-style string. For example, the devices package declaration could quite easily have been referenced as part of the Wrox.com domain, with the Chapter 1 name com.wrox.ch1.devices, as shown in the following snippet:

package com.wrox.ch1.devices { class Mobile {} }

Packages are also used when importing classes into AS documents, denoted by the import keyword, as shown in the following code snippet where the package com.wrox.ch1.devices example is used again:

import com.wrox.ch1.devices.Mobile;

A class has to be referenced by its full package reference before it can be used.

Functions, Methods, and Scope

A function is what allows an object to do a particular task and perform a set of reusable instructions. Functions are defined using the function keyword followed by the name of the task.

Class Scope Modifiers

Four keywords give scope to the properties and functions of an object: public, private, protected, and internal.

The public scope modifier means that a variable property or function can be accessed anywhere. The protected scope modifier means that only the current class and subclasses can use the variable or function. The private scope modifier restricts access to within the class only, and the internal scope modifier restricts the scope to the package it is contained in.

The Class Constructor

The constructor of a class is what initializes an object and creates it. The constructor is basically a class method, which must be declared public. The following snippet shows the constructor for the Mobile class, which is simply Mobile():

package devices { public class Mobile { public function Mobile() { // Code to initialize Mobile } } }

Return Types

Depending on the set of instructions found in the function, the return type gives an indication of the value that can be generated by the function call. In the following snippet, a public function called launchApp() is defined, with the return type specified as void:

package devices { public class Mobile { public function Mobile() { // Code to initialize Mobile } public function launchApp():void { // Code to launch an app on the mobile. } } }

The void keyword indicates that a value isn't expected to be returned by the function. Also note that class constructors don't specify a return type.

Variables

A variable is a unique identifier associated with an object that can hold a reference to a value. In AS3, data types are given to variables so that they can be checked at compile time. Variables are defined by the var keyword and then followed by the variable name.

In a class, variables that are added outside of functions can be referenced within the scope of the class; these are known as instance variables. These describe an object's characteristics and are what give an object its properties.

The following code snippet shows two private variables being declared: contacts, an Array data type, and phoneNumber, a Number data type:

package devices { public class Mobile { private var contacts:Array; private var phoneNumber:Number; public function Mobile() { // Code to initialize Mobile } public function launchApp():void { // Code to launch an app on the mobile. } } }

The following code demonstrates creating a Mobile object and assigning it to the variable mobileObj:

var mobileObj:Mobile = new Mobile();

Notice here that the data type assigned to the object is Mobile. Whenever you instantiate an object in this way, you need to define the data type on the variable; otherwise, you'll get a "variable has no type declaration" warning.

Within a method or function, instance variables can be used to set or reference data values. In the following code snippet the phoneNumber variable is assigned a value within the constructor method of Mobile, using the this keyword:

package devices { public class Mobile { private var contacts:Array; private var phoneNumber:Number; public function Mobile() { this.phoneNumber = 011003637; } public function launchApp():void { // Code to launch an app on the mobile. } } }

Static Variables and Methods

A static reference relates to class methods and variables that can be referenced without instantiating the class and creating an object.

In the following code snippet, the Mobile class is given the static variable deviceType which is defined as a string and is given the value Smartphone. A static function called switchOn() is also defined.

package devices { public class Mobile { public static var deviceType:String = "Smartphone";

private var contacts:Array; private var phoneNumber:Number;

public function Mobile() { this.phoneNumber = 011003637; }

public function launchApp():void { // Code to launch an app on the mobile. }

public static function switchOn():void { // Code to switch on the device. } } }

The following code demonstrates how you would call the switchOn() function:

Mobile.switchOn();

Parameters and Arguments

A parameter is a local variable that is defined and given a data type in the parentheses of a function declaration.

Instance methods allow you to define functions that exhibit an object's features and the things it can do. Parameters can be defined on instance methods to allow values to be passed to an object.

The following snippet shows a new instance method for the Mobile class defined, called addContact(). The method has a public scope and has two parameters: cName, a String data type representing a contact's name, and cNumber, a Number data type representing the contact's mobile number.

package devices { public class Mobile { public static var deviceType:String = "Smartphone";

private var contacts:Array; private var phoneNumber:Number;

public function Mobile() { this.phoneNumber = 011003637; }

public function addContact(cName:String, cNumber:Number):void { // Code to add a new contact }

public function launchApp():void { // Code to launch an app on the mobile. }

public static function switchOn():void { // Code to switch on the device. } } }

Note that local variables are only temporary.

To invoke or call the addContact() method, you need to use an instance of the Mobile class and supply what are called the arguments for the method. In this case there are two arguments, as shown in the following snippet:

var mobileObj:Mobile = new Mobile(); mobileObj.addContact("Olivia", 736300110);

Here the values Olivia and 736300110 are both arguments. Each argument needs to match the data types of the parameters specified for the method.

Conditional and Loop statements

A conditional statement is code that executes when a specific condition has been met.

In the following code snippet another static function called switchOff() has been added to the Mobile class. Here you will see a conditional if statement that checks the variable isOn, a Boolean value which is initially set to false in the class. In switchOn(), there is an if statement to check to see if the isOn value is false; this is indicated by the exclamation (!) preceding the Boolean value (that is, !isOn). The switchOff() function demonstrates another way of writing the same by asking whether the isOn variable is equal to false, and then executing the code within the else block of the if statement by setting isOn to false.

package devices { public class Mobile { public static var deviceType:String = "Smartphone";

private var contacts:Array; private var phoneNumber:Number; private var isOn:Boolean = false;

public function Mobile() { this.phoneNumber = 011003637; } public function addContact(cName:String, cNumber:Number):void { // Code to add a new contact }

public function launchApp():void { // Code to launch an app on the mobile. }

public static function switchOn():void { if(!isOn) { isOn = true; // Now add code to switch on the device. } else { // Do nothing, device is already on. } } public static function switchOff():void { if(isOn == false) { // Do nothing, device is already off. } else { isOn = false; // Now add code to switch off the device. } } }

}

A loop is a statement in which code is executed for as long as a particular condition is met.

The following code snippet shows how the switchOn() function could be updated to include a while loop statement, which executes the code within the block as long as the seconds variable is more than 0. The value held by seconds reduces by 1 each time the loop runs.

(Continues...)



Excerpted from Beginning Flash, Flex, and AIR Development for Mobile Devices by Jermaine G. Anderson Copyright © 2011 by John Wiley & Sons, Ltd. Excerpted by permission of John Wiley & Sons. All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.

Meet the Author

Jermaine G. Anderson works within the Software Engineering department of BSkyB in London, where he manages an agile team responsible for short-form media and the online video platform.

Customer Reviews

Average Review:

Write a Review

and post it to your social network

     

Most Helpful Customer Reviews

See all customer reviews >