Processing external information (XML, HTML, JS, jsonobject)

Source: Internet
Author: User
Accessing external data
Sometimes it is necessary to access the external information of the game. You can load external parameters from webpages or text fields, or save or download local information,
External variables
Suppose you have a game that varies according to some options.
You can obtain some parameters from the HTML page where the flash video is located. There are several different methods to achieve this, but if you use the publish setting settings to publish the generated HTML template, you can set the flashvars parameter in the ac_fl_runcontent function to implement transfer.

Take a look at this short function. Here we have 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>

Flashvars is a pair of formats with the attribute name equal to the value. For example, we set puzzlefileto myfilename.jpg and diffcultylevel to 7.

During flash playback, flash can obtain these parameters through the loaderinfo object, the following lineCodeIs obtained and the parameter is loaded into an object.

VaR paramobj: Object = loaderinfo (this. Root. loaderinfo). parameters;

To access a single parameter value, you can use this code.

VaR difflevel: String = paramobj ["difficultylevel"];

You can use this method to pass any number, Instance name, start level, speed, location, and so on.

Loading data
It is easy to call external text files. It is very convenient to call XML files.
For example, our XML is like this.

<Loadingdata>
<Question>
<Text> This is a test </text>
<Answers>
<Answer type = "correct"> correct answer </answer>
<Answer type = "wrong"> incorrect answer </answer>
</Answers>
</Question>
</Loadingdata>

If you want to call the data in XML, we need the help of the URLRequest and urlloader objects. Then you can monitor whether the loading of XML files is complete. If so, you can trigger the implementation of a function!

VaR xmlurl: URLRequest = new URLRequest ("loadingdata. xml ");
VaR xmlloader: urlloader = new urlloader (xmlurl );
Xmlloader. addeventlistener (event. Complete, xmlloaded );

We can write this function to read the information in XML and trace it out.
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 );
}

We need to convert the content of the XML file into an XML object, and then we can easily access it.

Read the path to the palace of the master of the Black Feather and you can see that XML can be directly written in flash, so we can directly create XML objects, but it is not an external call.

VaR dataxml: xml =

<Loadingdata>
<Question>
<Text> This is a test </text>
<Answers>
<Answer type = "correct"> correct answer </answer>
<Answer type = "wrong"> incorrect answer </answer>
</Answers>
</Question>
</Loadingdata>

Trace (dataxml. Question. Text );
Trace (dataxml. Question. Answers. Answer [0]);
Trace (dataxml. Question. Answers. Answer [0]. @ type );

Saving local data

In a game, we usually need to store data locally. For example, you can store the previous scores of the game or some game options to store data locally. We need to upload the dobject object.
The following code generates a sol file named mygamedata in the corresponding local directory when accessing the mongodobject object. This file is used to store local data and set mylocaldata. if data is gameinfo, gameinfo = "store this" will be generated in the mygamedata file. When we run the following code for the first time, we cannot access local data, for the first time, the following code completes local creation and storage, but the store this will be read in 2nd running times, that is, the previous data is read.
VaR mylocaldata: Export dobject = export dobject. getlocal ("mygamedata ");
Trace ("found data:" + mylocaldata. Data. gameinfo );
Mylocaldata. Data. gameinfo = "store this .";

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.