actionscript
TRANSCRIPT
![Page 1: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/1.jpg)
Unit 4: Using Text, Dates, Math, and Paths
![Page 2: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/2.jpg)
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
![Page 3: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/3.jpg)
Formatting TextFields with TextFormat Objects
![Page 4: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/4.jpg)
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
![Page 5: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/5.jpg)
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
![Page 6: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/6.jpg)
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.
![Page 7: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/7.jpg)
Clase Cómo se crea la instancia
Button Nuevo símbolo o Convertir en símbololuego, 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()
TextFormatDate
TransformColorTransform
SoundArrayObject
var instanceName:ClassName =new ClassName();
![Page 8: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/8.jpg)
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();
![Page 9: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/9.jpg)
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);
![Page 10: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/10.jpg)
Formatting TextFields Using ActionScript
• wt2-4.fla
• var thisFormat:TextFormat = new TextFormat();
• with(thisFormat)
{align = "right";
bold = true;
italic = true;
color = 0xFF0000;
size = 18;
font = "Arial";
}
set property values
• Run now• txtField1.setTextFormat(thisFormat);
• Aplicar al Field 2 y al Field 3 en diferentes tiempos
wt4-1.fla
![Page 11: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/11.jpg)
Creating TextFields using ActionScript
![Page 12: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/12.jpg)
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"
![Page 13: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/13.jpg)
• wt4-1.fla• Crear instancias en crear objetos
• comentar la declaración, eliminar objetos diseñ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";
Walkthrough 4-2
![Page 14: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/14.jpg)
Displaying Date and Time Information Using Date Objects
![Page 15: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/15.jpg)
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
![Page 16: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/16.jpg)
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
![Page 17: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/17.jpg)
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"
![Page 18: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/18.jpg)
• Assembling Date and Time Information • Create current Date objects
• Create date/time specific Date objects
• Calculate elapsed years
• Assemble formatted date/time information
Walkthrough 4-3
![Page 19: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/19.jpg)
Generating Random Numbers and Integers
![Page 20: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/20.jpg)
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);
![Page 21: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/21.jpg)
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
![Page 22: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/22.jpg)
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;
![Page 23: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/23.jpg)
• Generating Random Numbers • Generate random numbers
• Generate random numbers in a range
• Generate random integers
Walkthrough 4-4
![Page 24: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/24.jpg)
Understanding Nested Timelines and their Paths
![Page 25: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/25.jpg)
What Do _root, _parent, and this Mean?
![Page 26: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/26.jpg)
What Does an Absolute Path Look Like? (not recommended)
![Page 27: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/27.jpg)
What Does a Relative Path Look Like? (recommended)
![Page 28: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/28.jpg)
Is it Safe to Use Absolute Paths?
![Page 29: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/29.jpg)
• Understanding Nested Timelines and their Paths • Change properties between objects using relative
paths
Walkthrough 4-5
![Page 30: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/30.jpg)
Summary
![Page 31: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/31.jpg)
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
![Page 32: ActionScript](https://reader034.vdocuments.net/reader034/viewer/2022042715/55905a361a28ab8a208b4572/html5/thumbnails/32.jpg)
• Displaying Date Information in Created TextFields, and Randomly Positioning MovieClip Instances • Create TextFields
• Format date/time information
• Assign random _x and _y positions
Lab 4