As3 Summary (7)

Source: Internet
Author: User

Or in the programming of this game, we can summarize it.

1. external variables

Suppose you have a game that is changed based on some options. For example, you need to add
Loads different images, or an arcade game runs at different speeds. You can view the HTML of the Flash video
Obtain some parameters on the page. Multiple methods can be implemented here. If you use HTML as the template by default in the release settings
You can pass parameters through the flashvars attribute in the ac_fl_runcontent function.
The following is a short ac_fl_runcontent function. After exporting a video from Flash CS3, you can go to the HTML folder.
. Here I added the flashvars Parameter
<Script language = "JavaScript">
Ac_fl_runcontent (
'Codebase ',
'Http: // download.macromedia.com/P... flash. Cab # version = 9, 0, 0, 0 ',

'Width', '123 ',
'Height', '123 ',
'Src', 'externalvariables ',
'Quality', 'high ',
'Flashvars', 'puzzlefile=myfilename.jpg & difficultylevel = 7 ');
</SCRIPT>
This flashvars attribute includes a pair of values, which are separated by an & symbol. Here, puzzlefile is set
Myfilename.jpg and difficultylevel are set to 7.
When a Flash video starts running, the loaderinfo method is used to call the information. The following code Retrieves
Some parameters are assigned to the declared paramobj variable.
VaR paramobj: Object = loaderinfo (this. Root. loaderinfo). parameters;
To access a single attribute value, use the following code:
VaR difflevel: String = paramobj ["difficultylevel"]
In addition, you can also pass other constants of the game, such as the name, start level, speed, and location of the Instance piece.
 
Read the source file:
// Load External Parameter Information
VaR paramobj: Object = loaderinfo (this. Root. loaderinfo). parameters;
// Read the external difflevel value and assign it to the messagestrng variable
VaR difflevel: String = paramobj ["difficultylevel"];
VaR messagestring = "difficultylevel:" + paramobj ["difficultylevel"];
Messagestring + = "/N ";
// Read the puzzlefile value and assign the value to the text after adding the value to difflevel.
Messagestring + = "puzzlefile:" + paramobj ["puzzlefile"];
Messagetext. Text = messagestring;
When you run this video externalvariables. fla, it loads the externalvariables.html file.

3. load data

In as3.0, loading external text files is quite easy, especially in XML format. For example
Load a trivial problem file, so the content of this XML file can be written as follows:
<Loadingdata>
<Question>
<Text> This is a test </text>
<Answers>
<Answer type = "correct"> correct answer </answer>
<Answer type = "wrong"> incorrect answer </answer>
</Answers>
</Question>
</Loadingdata>
To load the data, you need to use the URLRequest and urlloader functions.
After loading, you also need to add a listener function and call the relevant data. The Code is as follows:
VaR xmlurl: URLRequest = new URLRequest ("loadingdata. xml ");
VaR xmlloader: urlloader = new urlloader (xmlurl );
Xmlloader. addeventlistener (event. Complete, xmlloaded );

The xmlloaded function contains some trace statements that display the passed data on the output panel.
Function xmlloaded (Event: Event ){
VaR dataxml = xml(event.tar get. data );
Trace (dataxml. Question. Text );
Trace (dataxml. Question. Answers. Answer [0]);
Trace (dataxml. Question. Answers. Answer [0]. @ type );
}

4. Save local data

We will use a local shared object, which is similar to a browser cookie. We need to call
Sharedobject. getlocal () is used to create shared objects in an application, such as a calculator with memory function.

Note: This object can only be used on the current client. The following code shows how to return
Shared object reference value to the variable:
VaR mylocaldata: Export dobject = export dobject. getlocal ("mygamedata ");
If you store some data, you can access it through the following code:
Trace ("found data:" + mylocaldata. Data. gameinfo );
Next, set the value of the gameinfo attribute.
Mylocaldata. Data. gameinfo = "store this .";

5. Customize the cursor Style

First, drag and drop a button on the stage and place it on the first layer. Then, draw a direction key image and convert it
Video editing, which is placed on the second layer and the Instance name is Arrow.
Set the mouse cursor to invisible and use the mouse. Hide () command
Mouse. Hide ()
Then, make sure that the custom video is placed on all the displayed objects. If you create an object using a script and add it to the screen, you must use the setchildindex command to place the custom video on the top of all display objects.
Enter_frame listeners are also required for this custom video to follow the mouse.
Addeventlistener (event. enter_frame, movecursor );
Function movecursor (Event: Event ){
Arrow. x = mousex;
Arrow. Y = Mousey;
}
In addition, you need to set the mouseenabled attribute of the custom optical mark to false, indicating that the object does not receive
Mouse events. The default value is true, indicating that the object accepts mouse events.
Arrow. mouseenabled = false;

6. Film Loading

Set the video to stop in the first frame,
Stop ()
Then, add an enter_frame listener and call the loadprogress function at each frame.
Addeventlistener (event. enter_frame, loadprogress );
This function uses this. Root. loaderinfo to obtain the video loading status. It has two attributes: bytesloaded and bytestotal.
Then, we divide the two property values by 1024 and convert them into kilobytes. The Code is as follows:
Function loadprogress (Event: Event ){
// Obtain the total and downloaded bytes.
VaR moviebytesloaded: Int = This. Root. loaderinfo. bytesloaded;
VaR moviebytestotal: Int = This. Root. loaderinfo. bytestotal;
// Convert to kilobytes
VaR moviekloaded: Int = moviebytesloaded/1024;
VaR moviektotal: Int = moviebytestotal/1024;
To enable gamers to see the download progress, we also need to draw a text field on the stage. The display format is:
Loading: 5 k/32 K
// Display the download progress
Progresstext. Text = "loading:" + moviekloaded + "K/" + moviektotal + "K ";
When the downloaded bytes are equal to the total bytes of the video, we will remove the frame rate listener and redirect the video to the second frame.
// After loading, the listener is removed.
If (moviebytesloaded> = moviebytestotal ){
Removeeventlistener (event. enter_frame, loadprogress );
Gotoandstop (2 );
}}

7. Random Numbers

In as3.0, use the math. Random () function to create a random number. The returned value is a number greater than or equal to 0 and less than 1. Example
For example:
VaR random1: Number = math. Random ()
Generally, we use a specific random number range. The following code randomly generates a number between 0 and 10.
VaR random2: Number = math. Random () * 10
If you want to get a random integer, you can use the math. Floor method. The return value of floor is less than or equal to the specified value.
The nearest integer to a number or expression. The following code generates 0 ~ Random integer of 9
VaR random3: Number = math. Floor (math. Random () * 10)
If the defined range does not contain 0, you can add a specified number to the returned result. The following code generates 1 ~ 10
Random INTEGER
VaR random4: Number = math. Floor (math. Random () * 10) + 1

8. Shuffling Arrays

In games, random numbers are most commonly used in card game shuffling. For example, you have 52 cards in your hand,
What should I do if I want to randomly disrupt it?
First, you need to create a new array, which is simply arranged in order. The following code displays ordered numbers from 0 to 51
// Create an ordered array
VaR startdeck: array = new array ();
For (VAR cardnum: Int = 0; cardnum <52; cardnum ++ ){
Startdeck. Push (cardnum );
}
Trace ("unshuffled:", startdeck );
The output result of the test video is as follows:
Unshuffled:
, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 45, 50, 51
 
To disrupt the order of the array elements, we first select an element at the random position of the array and save it
Put it in a new array, and then delete the element from the original array until the elements of the original array are cleared.
// Shuffle into new array
VaR shuffleddeck: array = new array ();
While (startdeck. length> 0 ){
VaR R: Int = math. Floor (math. Random () * startdeck. Length );
Shuffleddeck. Push (startdeck

);
Startdeck. splice (R, 1 );
}
Trace ("shuffled:", shuffleddeck );

9. System Data

You can use the capabilities object to obtain multiple information about the computer where the flash video is located. Here we will list several common genus
Sex:
Capabilities. playertype: Specifies the player type. "Standalone", used for independent Flash Player;
"External", used for External Flash Player or in test mode; "plugin", used for Flash Player browsing
Is used for the Flash Player ActiveX Control used by Microsoft Internet Explorer.
Capabilities. Language: Specifies the language code of the system that runs the player. In the English system, this value returns the double letter generation.
(EN ).
Capabilities. OS: return the type and version of the operating system. For example, Windows XP
Capabilities. screenresolutionx, capabilities. screenresolutiony: return screen resolution 1280*800
Capabilities. Version: The Flash Player version, for example, Win 9, 0, 45, and 0. You can obtain
Type and Flash Player version.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.