Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming in AS3. AS3 vs MXML MXML is content/structure AS3 ties in with the MXML to create the a functioning program.

Similar presentations


Presentation on theme: "Programming in AS3. AS3 vs MXML MXML is content/structure AS3 ties in with the MXML to create the a functioning program."— Presentation transcript:

1 Programming in AS3

2 AS3 vs MXML MXML is content/structure AS3 ties in with the MXML to create the a functioning program

3 The Script Tag We include AS3 inside a script block When you type the script tag and hit enter FD will automatically create a CDATA block and here is where all the true coding begins!

4 <![CDATA[ //AS3 goes here ]]>

5 Functions Everything in flash is based off of functions Here is the format for an AS3 function: scope function name(parameters):return type { Body }

6 Example private function test():void { } This function does nothing Its named test It returns nothing Its scope is set to private

7 Event Based Programming Functions are called by the programmer or by the program when an event occurs Events are properties of a control The button is set to be clicked and when it is the test() function will be called

8 The Code <![CDATA[ import mx.controls.*;//import the library/package containing the control classes private function test():void { Alert.show("Hello world!"); } ]]>

9 The Import Statements These are required when using classes in certain packages Since the Alert class exists in the mx.controls package we import it by importing everything in that package We could have also written import mx.controls.Alert; Be aware of the followig error, as it is due to not importing the proper package G:\projects\prjFlex2\src\Main.mxml(19): Error: Access of undefined property Alert.

10 What Events? FD helps you with this in its popup window (events have a lighting bolt symbol) You could also research the reference info at http://help.adobe.com/en_US/FlashPlatform/ reference/actionscript/3/mx/controls/Button. html#protectedMethodSummary http://help.adobe.com/en_US/FlashPlatform/ reference/actionscript/3/mx/controls/Button. html#protectedMethodSummary Search for the events section

11 Variables The format for variable declaration is: var name:type; A primitive value is a value that belongs to one of the following data types: String, Number, int, uint, Boolean, Null, and void. A complex value is a value that is not a primitive value. Data types that define sets of complex values include: Object,Array, Date, Error, Function, RegExp, XML, and XMLList. All values in ActionScript 3, whether they are primitive or complex, are objects.

12 Sample Code private function test():void{ var n1:Number; var n2:Number; n1 = Number(txtNumber1.text); n2= Number(txtNumber2.text); var answer:Number; answer = n1 + n2; Alert.show("Answer is " + answer); }

13 Type conversions Number() is a built in function that converts non-numbers to number types A type conversion is said to occur when a value of one data type is transformed into a value of a different data type. Type conversions can be either implicit or explicit. Implicit conversion, also called coercion, is sometimes performed at runtime. For example, if the value 2 is assigned to a Boolean typed variable, the value 2 is converted to the Boolean value true before assignment. Explicit conversion, also called casting, occurs when your code instructs the compiler to treat a variable of one data type as if it belongs to a different data type. When primitive values are involved, casting actually converts values from one data type to another. To cast an object to a different type, you wrap the object name in parentheses and precede it with the name of the new type. For example, the following code takes a Boolean value and casts it to an integer:

14 var myBoolean:Boolean = true; var myINT:int = int(myBoolean); trace(myINT); // 1 Implicit conversions Implicit conversions happen at runtime in a number of contexts: In assignment statements When values are passed as function arguments When values are returned from functions In expressions using certain operators, such as the addition (+) operator Explicit conversions It's helpful to use explicit conversions when you compile in strict mode, because there are times when you do not want to generate a compile-time error. Use explicit conversions when you know that coercion will convert your values correctly at runtime. For example, when working with data received from a form, cast certain string values to numeric values. The following code generates a compile-time error even though the code would run correctly in standard mode: var quantityField:String = "3"; var quantity:int = quantityField; // compile time error in strict mode If you want to continue using strict mode, but would like the string converted to an integer, you can use explicit conversion, as follows: var quantityField:String = "3"; var quantity:int = int(quantityField); // Explicit conversion succeeds.

15 Default Values Data typeDefault value Booleanfalse int0 NumberNaN Objectnull Stringnull uint0

16 Exercises Create a program that allows a user to enter the cost of 4 items, a tax rate and a discount rate (both in percentages). It then displays the final pre-tax cost, cost wit tax, discount applied to this final cost and then the final discounted cost (all in a nicely designed and labeled GUI). Make sure you use the proper type and number of variables as well as incorporate the necessary type conversions. Post your project to your unit 1 webpage.


Download ppt "Programming in AS3. AS3 vs MXML MXML is content/structure AS3 ties in with the MXML to create the a functioning program."

Similar presentations


Ads by Google