[Respond when the text field is selected or unselected]
When you get focus: FocusEvent.FOCUS_IN
When the focus is lost: FocusEvent.FOCUS_OUT
When removing focus through the keyboard (Tab key): FocusEvent.KEY_FOCUS_CHANGE
When removing focus through the mouse: FocusEvent.MOUSE_FOCUS_CHANGE
The FocusEvent class has a relatedObject property. As for the FOCUS_IN event, the relatedObject property is the reference address of the object that just had the focus; for the FOCUS_OUT, KEY_FOCUS_CHANGE and MOUSE_FOCUS_CHANGE events, the relatedObject property is the reference address of the object that just received the focus.
Both FOCUS_IN and FOCUS_OUT events occur after the focus changes, so both are non-cancellable events. For the KEY_FOCUS_CHANGE and MOUSE_FOCUS_CHANGE events, the default behavior can be cancelled using the () method:
(FocusEvent.KEY_FOCUS_CHANGE, onKeyFocus);
private function onKeyFocus(event:FocusEvent):void {
if( == "") {
();//When the field has no text, it is not allowed to use the Tab key to remove the focus.
}
//It is the reference address of the object that has the focus just now, that is, the reference address of the next object that has the focus
}
【Respond to user input behavior】
(TextEvent.TEXT_INPUT, onTextInput);
private function onTextInput(event:TextEvent):void {
if( == "a" && == 0) {
(); //TEXT_INPUT is an event that can be cancelled. TextEvent has a text attribute containing the characters entered by the user. This example ensures that the first letter entered by the user is not "a"
}
}
【Add hyperlink to text】
Open the webpage:
Example 1:
var css:StyleSheet = new StyleSheet();
("a{color:#0000FF;} a:hover{text-decoration:underline;}");
= css;
= "<a href='' target='_blank'>Link text</a>";
Example 2:
= "Link text";
var formatter:TextFormat = new TextFormat();
= "";
= "_blank";
(formatter);
Open Email:
= "Link text";
var formatter:TextFormat = new TextFormat();
= "mailto:y_boy@";
(formatter);
Use the event protocol to open the ActionScript method:
= "<a href='event:'></a>";
(, onClickHyperlink);
private function onClickHyperlink(event:TextEvent):void {
trace(); //Output:
}
【Exactly obtain the index value of a certain character】
Return the character index value starting from zero at the position specified by the x and y parameters
(x:Number, y:Number):int
example:
(mouseX, mouseY); //Return the index value of the character at the mouse position
【Exactly obtain the border area of a certain character】
(charIndex:int):Rectangle
Determine the border area range of the character by reading the x, y, width and height properties of the Rectangle.
【Exactly obtain the index value of a row】
Return the row index value starting from zero at the position specified by the x and y parameters
(x:Number, y:Number):int
(mouseX, mouseY); //Return the index value of the row at the mouse position
[Get the line index value of the line where the character at the specified index value is located]
(charIndex:int):int
【Get the number of characters in the specified line】
(lineIndex:int):int
【Get the characters contained in the line at the specified index value】
(lineIndex:int):String
[Get the index value of the first character of the line where the specified row index is located]
(lineIndex:int):int
【Get the measurement information of the specified text line】
(lineIndex:int):TextLineMetrics
The TextLineMetrics class contains information about the text position and measurement value of a line of text in the text field, and provides in-depth illustrations in the document.
[Get the first character index value in the paragraph where the character at the specified index is located]
If a character index is given, the index of the first character in the same paragraph is returned
(charIndex:int):int
[Get the number of characters in the paragraph where the characters at the specified index are located]
(charIndex:int):int
【Replace selected text】
(value:String):void
This method can be used to insert and delete text without destroying the character and paragraph format of the rest of the text.
【Replace text in the specified range】
(beginIndex:int, endIndex:int, newText:String):void
Note: If a stylesheet is applied to the text field, this method does not work
【Using Timer Timer】
var timer:Timer = new Timer(delay:Number, repeatCount:int = 0);
(, onTimer);
();
private function onTimer(event:TimerEvent):void {
//code
}
【Move components in specified direction and speed】
speed: Target speed
radians: The angle between speed and x-axis, radians
vx = (radians) * speed;
vy = (radians) * speed;
【Easy animation algorithm】
targetX, targetY: target coordinate;
easingSpeed: Each move score, range 0 to 1;
var vx:Number = (targetX - ) * easingSpeed;
var vy:Number = (targetY - ) * easingSpeed;
+= vx;
+= vy;
Note: When approaching the target point very much, you often find the distance to the target point. If its value is less than a specific value, the easing should be stopped.
【Simulated spring reciprocating motion algorithm】
Five variables: targetX (x coordinates of the target point), (x coordinates of the object), vx (horizontal velocity), ax (horizontal acceleration), k (spring intensity)
Known: targetX (x coordinates of target point), k (spring strength)
var ax:Number = (targetX - ) * k;
vx += ax;
+= vx;
Note: This is just a horizontal direction, and it can also be extended to vertical direction and horizontal vertical situations.
【Simulated single pendulum motion】
Four variables: (the x coordinate of the object), centerX (the x coordinate of the center position), angle (radian), radius (the amplitude of the swing, that is, the range of the swing)
= centerX + (angle) * radius;
angle += 0.05;
【Simulated damping motion】
Six variables: targetX (x coordinates of the target point), (x coordinates of the object), vx (horizontal velocity), ax (horizontal acceleration), k (spring strength), and damping (damp coefficient)
Known: targetX (x coordinates of target point), k (spring strength)
var ax:Number = (targetX - ) * k;
vx += ax;
+= vx;
vx *= damp; //This sentence is missing from the reciprocating movement of the spring without resistance
Note: This is just a horizontal direction, and it can also be extended to the vertical direction and horizontal vertical situation.
This principle can be generalized to other attributes, such as scaleX:
scaleVel += (targetScale - ) * k;
+= scaleVel;
scaleVel *= damp;
【Calculate the angle between two points】
Math.atan2(y:Number, x:Number):Number
y: The vertical distance between two points
x: The horizontal distance between two points
Returns an angle in radians, with the return value between positive PI and negative PI.
【Let the object point to the mouse (follow the eyes)】
var dx:Number = mouseX - ;
var dy:Number = mouseY - ;
var radians:Number = Math.atan2(dy, dx);
= radians * 180 / ;
【What should be paid attention to when connecting strings】
var result:String = 2 + 6 + "a";
trace(result); //Output: 8a
var result:String = "a" + 2 + 6;
trace(result); //Output: a26
var result:String = "a" + (2 + 6);
trace(result); //Output: a8
【Find matching string】
Use (val:String, startIndex:Number = 0):int
Returns the index of the first match of the specified substring, and returns -1 if there is no match.
Find all matching strings:
var index:int = -1;
while((index = ("val", index + 1)) != -1) {
trace("Result:" + index);
}
Use (val:String, startIndex:Number = 0x7FFFFFFF):int
Returns the position of the last match of the specified substring, and returns -1 if there is no match.
var index:int = ;
while((index = ("val", index - 1)) != -1) {
trace("Result:" + index);
}
Note: both indexOf() and lastIndexOf() methods are case sensitive. If you want to find both upper and lowercase, you can combine the() and () methods to convert the string to lowercase or uppercase before searching.
【Extract substring】
(startIndex:Number = 0, len:Number = 0x7fffffff):String
(startIndex:Number = 0, endIndex:Number = 0x7fffffff):String
(startIndex:Number = 0, endIndex:Number = 0x7fffffff):String
The first parameter of substr() can be a negative value, indicating that the index value is calculated from the end of the string, and -1 is the last character;
The difference between substring() and slice() is that substring() only accepts positive index values and treats negative values as 0. In addition, if endIndex is less than startIndex, the substring() method will automatically swap it before execution, and always use the smaller value of the two parameters as the starting index.
The slice() method can accept startIndex and endIndex as negative values, and express the negative values as calculated from the end of the string. If the endIndex is less than startIndex, the slice() method will return an empty string.
【Split string in specified mode】
(delimiter:*, limit:Number = 0x7fffffff):Array
【Replace the specified string】
Use (pattern:*, repl:Object):String
You can replace all specified strings like this: ("a").join("b");
【Reverse string by word or single letter】
1. Use the () method to cut strings into an array: for words, spaces are used as delimiters, and for letters, empty characters ("") are used as delimiters;
2. Use the () method to reverse the array;
3. Use the () method to recombine into a string: for words, use spaces (" ") as concatenation characters, and for letters, use empty characters ("") as concatenation characters;
【Use SharedObject and catch exceptions】
var so:SharedObject = ("so");
= "Youthoy";
try {
var flushResult:String=(500 * 1024);//Request 500KB capacity
//If flush() is not executed successfully, add an event handler function to netStatus to confirm whether the user agrees or refuses, otherwise only the result is checked.
if(flushResult == ) {
//Add an event handler function for netStatus, so that we can check whether the user agrees to store shared objects with sufficient disk capacity. When the netStatus event occurs, execute the onStatus function.
(NetStatusEvent.NET_STATUS, onStatus);
}else if(flushResult == ) {
//Save successfully. Put the program you want to execute after the data is successfully stored here.
}
}catch(e:Error) {
//This means that the user sets local storage to "Reject". If storing data is important, you will want to warn the user here.
//In addition, if you want the user to modify its setting values, you can use the following statement to open the local storage page of the Player's "Settings" dialog box.
(SecurityPanel.LOCAL_STORAGE);
}
//Define the onStatus function to handle the state events of the shared object. When flush() returns "pending", a prompt is raised, and the event triggered after the user makes a selection.
function onStatus(event:NetStatusEvent):void {
if( == ""){
//If the content is "", it means the user's consent. Put the program you want to execute after the user agrees here.
}else if( == ""){
//If the content is "", it means the user's consent. Put the program you want to execute after the user refuses.
}
//Now, remove the event listener because we only need to listen once.
(NetStatusEvent.NET_STATUS, onStatus);
}
[Need to pay attention when storing objects to LSO]
LSO uses binary AMF (Action Message Format) to encode an object. When you store a class object in LSO, the object will be encoded into a normal object containing content attributes. Therefore, when you try to read the object from a shared object, it cannot be read as an object of a certain class because there is no type data. To overcome this limitation, use the() method.
In any video, as long as the custom object data is read from the shared object, the custom class must register an alias, and the registerClassAlias() method must be called before the() method is called.
[Two videos in the same domain name can access the same LSO]
First of all, LSO is stored in the following directory:
C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\#SharedObjects
For example, if a video from /youthoy is written to an LSO named example, the data will be stored in the following location:
C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\#SharedObjects\[random directory name]\\youthoy\\
If the code in this is like this: var example:SharedObject = ("example", "/"); That is, the selected local path parameter is used, then the LSO will be stored in the following path. It is noted that "youthoy\" is missing from the above path:
C:\Documents and Settings\[user name]\Application Data\Macromedia\Flash Player\#SharedObjects\[random directory name]\\
The LSO created in this way can be read by other Flash videos of the same domain using the following program:
var example:SharedObject = ("example", "/");
Let's take a look at another example:
There are two Flash videos and they are both located in the same domain(), but, located in /youthoy/firstGroup and located in /youthoy/secondGroup. In this case, LSO can be created and read using any of the following local paths:
/
/youthoy
/youthoy/firstGroup
Instead, LSO can be created and read using any of the following local paths:
/
/youthoy
/youthoy/secondGroup
Therefore, if you want both movies to read the same LSO, when you call the getLocal() method, you must specify one of the two local paths (/or /youthoy) that the movie shares.
【Three types】
The optional values are:
When dataFormat is BINARY, the attribute is the type;
When dataFormat is TEXT, the property is the String type;
When dataFormat is VARIABLES, the attribute is the Object type.
【Use dynamic data in XML】
Put variables in braces ({and}):
var userName:String = "Youthoy";
var myXML:XML = <person><userName>{userName}</userName></person>;
【A new node element】
//Example 1:
var example:XML = <person/>;
=<father/>; //The content attribute and element name do not have to be the same, it can also be: = <father/>, but the element name will be the basis, that is, a <father/> element will be created
trace(example);
/*
Output:
<person>
<father/>
</person>
*/
//Example 2:
var example:XML = <person/>;
= ""; //Create a <father/> element
trace(());
/*
Output:
<person>
<father/>
</person>
*/
//Example Three:
var example:XML = <person/>;
var id:int = 10;
example["user" + id] = "";
trace(example);
/*
Output:
<person>
<user10/>
</person>
Note: Some cases must use the third case. For example, it is completely legal to have a hyphen ("-") inside the element node name, but you will get a compiler error in ActionScript:
-element = """; //Creating compiler error
The correct way to write it is:
example["some-element"] = "";
*/
【New Attributes】
var example:XML = <person/>;
= "";
.@name = "Youthoy";
.@["bad-variable-name"] = "yes";
.@other = ["", undefined, true, 44, null];
var id:int = 44;
.@["user" + id] = "love";
trace(example);
/*
Output:
<person>
<element name="Youthoy" bad-variable-name="yes" other=",,true,44," user44="love"/>
</person>
*/
【Insert text elements and node elements into XML】
//Insert before and after the specified element, insert at the front and tail ends
var example:XML = <person/>;
= "";
(, <one/>); //Insert one node in front of two nodes
(, “Insert text in front of two nodes”);
(, <three/>); //Insert three nodes after two nodes
(<start/>); //Insert the start node at the top end
(<end/>); //Insert end node at the end end
= "start content"; //Add content to the node
trace(example);
/*
Output:
<person>
<start>start content</start>
<one/>
Insert text in front of two nodes
<two/>
<three/>
<end/>
</person>
*/
【Convert the content attributes of the XML instance to any content that can be converted into a string】
//First convert the right side of the equal sign into a convertible string
var example:XML = <person/>;
= "one";
= new URLLoader();
= new Sprite();
= new Boolean; //The value of the Boolean object is initialized to false
= new Number();
= new String();
= {a:"Youthoy", b:true, c:undefined};
= ["three", undefined, true, 44, null];
/*
Output:
<person>
<one>one</one>
<two>[object URLLoader]</two>
<three>[object Sprite]</three>
<four>false</four>
<five>0</five>
<six/>
<seven>[object Object]</seven>
<eight>three,,true,44,</eight>
</person>
*/
[Algorithm for recursion of XML attributes layer by layer]
var example:XML = <menu>
<menuitem label="File">
<menuitem label="New"/>
<menuitem label="Save"/>
</menuitem>
<menuitem label="Help">
<menuitem label="About"/>
</menuitem>
</menu>;
walk(example);
function walk(node:XML):void {
for each(var element:XML in ()) {
trace(element.@label);
walk(element);
}
[Read text nodes and their values]
var example:XML = <menu>
My name is Youthoy.
<myName>Youthoy</myName>
</menu>;
(); //Output: My name is Youthoy.
(); //Output: <myName>Youthoy</myName>
//The following three lines are output: Youthoy
();
();
;
[Read the attribute name and its value when recursive element attribute]
var example:XML = <person name="Youthoy" age="21"/>;
for each(var i:XML in ()) {
trace(() + "=" + i);
}
/*
Output:
name=Youthoy
age=21
*/
Alternative method:
var example:XML = <person name="Youthoy" age="21"/>;
trace(example.@*[1]); //Output: 21
【Accessing specified nodes or attributes in the entire XML hierarchy regardless of hierarchy】
Use descendant accessor operators..
Example 1 (for node elements):
var example:XML = <a>
<z>I am z.</z>
<b>
<c></c>
<z>I am z.</z>
</b>
</a>;
trace(example..()); //Output: I am am z.
Example 2 (for attributes):
var example:XML = <person>
<a>
<item name="Youthoy" age="21"/>
</a>
<item name="Jimpa" age="21"/>
</person>
trace(example..@name); //Output: YouthoyJimpa
[Delete node elements, text nodes and attributes]
Use delete to delete a single item. To delete the entire XMLList, you can use a for loop to delete it in reverse order to avoid the need to change the array index when iterating.
【Search for XML Advanced Applications】
Use the term filtering. (condition), which can be used in combination with regular expressions.
example:
var example:XML = <foodgroup>
<fruits>
<fruit color="red">Apple</fruit>
<fruit color="orange">Orange</fruit>
<fruit color="green">Pear</fruit>
<fruit color="red">Watermelon</fruit>
</fruits>
<vegetables>
<vegetable color="red">Tomato</vegetable>
<vegetable color="brown">Potato</vegetable>
<vegetable color="green">Broccoli</vegetable>
</vegetables>
</foodgroup>;
trace(example..*.(hasOwnProperty("@color") && @color == "red"));
/*
The detection done by hasOwnProperty is to ensure that the element has a color attribute, and then, if so, test whether the value of the color attribute is red. Only when the calculation result of the condition is true, the element will add the XMLList returned by the EX4 equation.
Output
<fruit color="red">Apple</fruit>
<fruit color="red">Watermelon</fruit>
<vegetable color="red">Tomato</vegetable>
*/
【CDATA(Character Data) tag】
<![CDATA[]]>, must be capitalized. If <![CDATA[[]]]> does this, it will tell you that the syntax is wrong.
【Some nouns】
RPC:
Full name: Remote Procedure Call, friends in * call it: Remote program call
SOAP:
Full name: Simple Object Access Protocol, friends in * call it: Simple Object Access Agreement
WSDL:
Full name: Web Service Description Language, friends in * call it: web service description language
Previous page12Read the full text