ActionScript

464 views

Published on

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
464
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

ActionScript

  1. 1. Unit 4: Using Text, Dates, Math, and Paths
  2. 2. Topics • Creating objects in Flash • Formatting TextFields with TextFormat objects • Creating TextFields at run time • Using the Date class to display date and time information • Creating random numbers and integers • Understanding nested Timelines and their paths
  3. 3. Formatting TextFields with TextFormat Objects
  4. 4. What is an Object? • “An object is an instance of a class" • A class describes a category of things • It's not the thing itself • A Flash symbol describes a category of things • You don't put a symbol on the Stage, you put an instance of that symbol on the Stage • Symbols are classes, instances of it are objects of that class
  5. 5. What is a Class? • A class is a blueprint of the variables and functions to be attached to each instance ("object") of that class • Property: a variable attached to an object, describing its characteristics • Method: a function attached to an object, enabling its behaviors
  6. 6. How Do I Create an Object? • Objects may be instantiated visually or through code • Not all objects can be created both ways • Visual creation only • Button symbol instances • Insert  New Symbol • Visual creation or ActionScript • MovieClip symbol instances • Insert  New Symbol • createEmptyMovieClip, attachMovie() • TextField objects • Text tool • createTextField() • ActionScript only (because they're not visual) • TextFormat, Date, ColorTransform, Sound, Array, etc.
  7. 7. Clase Cómo se crea la instancia Button Nuevo símbolo o Convertir en símbolo luego, se arrastra al escenario MovieClip Nuevo símbolo o Convertir en símbolo luego, se arrastra al escenario - o - this.createEmptyMovieClip() this.attachMovie() this.duplicateMovieClip() TextField Herramienta Texto - o - this.createTextField() TextFormat Date Transform ColorTransform Sound Array Object var instanceName:ClassName = new ClassName();
  8. 8. How Do I Create a Non-Visual Object? • You call a class name as a function, preceded by the new keyword • A new instance of that class is returned from this function call • You assign it to a variable … it's instance name var instanceName:ClassType = new ClassType(); • To create a TextFormat object var tfFormat:TextFormat = new TextFormat();
  9. 9. How Do I Format a TextField Using Code? • TextFormat objects have the same properties you might otherwise set using the Properties panel • color, font, url, bold, align, leading, etc • setTextFormat() applies a TextFormat object's properties to a TextField instance var tfInputStyle:TextFormat = new TextFormat(); tfInputStyle.font = "Courier"; tfInputStyle.bold = true; txtUserName.setTextFormat(tfInputStyle);
  10. 10. Formatting TextFields UsingActionScript • wt2-4.fla • var thisFormat:TextFormat = newTextFormat(); • with(thisFormat) { align ="right"; bold= true; italic =true; color =0xFF0000; size = 18; font ="Arial"; } set property values • Runnow • txtField1.setTextFormat(thisFormat); • AplicaralField 2 yalField 3 endiferentes tiempos wt4-1.fla
  11. 11. Creating TextFields using ActionScript
  12. 12. How do I create a TextField using ActionScript? • createTextField() is a method of the MovieClip class • Call it to create a TextField in a MovieClip this.createTextField("instanceName",depth,x,y,width,height); • this refers to the object your code is running inside, often the main document Timeline, attaching the new TextField to the Stage • type property determines whether TextField is "input" or "dynamic" instanceName.type = "input"; // or "dynamic"
  13. 13. • wt4-1.fla • Crear instancias en crear objetos • comentarladeclaración,eliminarobjetosdiseño • this.createTextField("txtField1", 1, 20, 40, 200, 30); • this.createTextField("txtField2", 2, 20, 70, 200, 30); • this.createTextField("txtField3", 3, 20, 100, 200, 30); • Establecer propiedades •txtField1.text = “UNO"; •txtField2.text = “DOS"; •txtField3.text = “TRES"; Walkthrough4-2
  14. 14. Displaying Date and Time Information Using Date Objects
  15. 15. How Do I Tell What Time and Date It Is? • Date objects have methods providing date/time info • getMinutes(), getDay(), getDate(), getMonth(), etc • Months begin at 0 (December is month 11) • They may also be set to a specific date/time • setYear(), setMonth(), setDate(), setDay(), etc • They support Coordinated Universal Time (UTC) • Milliseconds elapsed between midnight 1/1/1970 and whatever time has been set for the Date object • getUTCHours(), setUTCDate(), etc
  16. 16. How Do I Quickly Assemble Several Strings? • Compound concatenation works like other compound operators var timeString:String = "The time is: "; var dtNow:Date = new Date(); timeString = timeString + dtNow.getHours(); – means the sames as – timeString += dtNow.getHours(); • Saves a lot of typing when formatting date/time info
  17. 17. LA FECHA … • var timeString:String = "The time is: "; • var dtNow:Date = new Date(); • timeString = timeString + dtNow.getHours(); • - significa lo mismo que - • timeString += dtNow.getHours(); • Para mes/fecha/año: • var txtDate:TextField; • var dateString:String = ""; • var dtNow:Date = new Date(); • dateString += dtNow.getMonth() + "/"; • dateString += dtNow.getDay() + "/"; • dateString += dtNow.getFullYear(); • txtDate.text = dateString; // podría mostrar “27/10/2010"
  18. 18. • AssemblingDateandTimeInformation • Create current Date objects • Create date/time specific Date objects • Calculate elapsed years • Assemble formatted date/time information Walkthrough4-3
  19. 19. Generating Random Numbers and Integers
  20. 20. What is a “Static" Class? • Math is a static class • You do not create Math objects • PI is always PI, and cosines are always figured the same • Classes are used "statically" when there's only one of whatever they refer to • Math, Mouse, System, Key, Stage, etc var matMyMath:Math = new Math(); var circumference:Number = Math.PI * (2 * radius);
  21. 21. How Do I Calculate a Random Number? • Math supports several useful methods including • random() • Returns a random value between 0 and .9999999999999999 • round([number]) • Rounds a number to the nearest integer and returns it
  22. 22. How Do I Calculate a Random Number? • Math can be used to • Generate a random number ( 0 to .9999999999 … ) var random:Number = Math.random(); • Generate a random number in a range ( 0 to 9.99999 … ) var random:Number = Math.random() * 10; • Generate a random integer in a range (0 to 10) var random:Number = Math.round(Math.random() * 10); • Generate a random integer in an offset range ( -5 to 5 ) var random:Number = Math.round(Math.random() * 10) – 5;
  23. 23. • GeneratingRandom Numbers • Generate random numbers • Generate random numbers in a range • Generate random integers Walkthrough4-4
  24. 24. Understanding Nested Timelines and their Paths
  25. 25. What Do _root, _parent, and this Mean?
  26. 26. What Does an Absolute Path Look Like? (not recommended)
  27. 27. What Does a Relative Path Look Like? (recommended)
  28. 28. Is it Safe to Use Absolute Paths?
  29. 29. • UnderstandingNestedTimelinesandtheirPaths • Change properties between objects using relative paths Walkthrough4-5
  30. 30. Summary
  31. 31. Summary • “An object is an instance of a class" • Objects are created visually or by code, and some either way • Insert  New Symbol • createEmptyMovieClip(), createTextField() • TextFormat objects format TextField objects • Date objects inform about now or a date you set • Math is a static class of useful functions and constants (properties) • var integer0to10:Number = Math.round(Math.random() * 10); • One object refers to properties or methods of another through an absolute or relative path
  32. 32. • DisplayingDateInformationinCreatedTextFields,andRandomly PositioningMovieClipInstances • Create TextFields • Format date/time information • Assign random _x and _y positions Lab4

×