Solution to resource version update in web games

Source: Internet
Author: User
Tags addchild

Because the cache of many browsers is very stubborn, such as the IE browser, there is often a problem that resources cannot be updated in a timely manner in browser games.

For example, if you update a backpack resource, it is a SwF resource and you upload it to the server. However, because of the cache, the web game may still run the old backpack SWF resources during its operation, which leads to the problem that it cannot be updated in a timely manner.

A common solution in the industry is to use the version number:

1. First, you need to create a resource XM, which contains the version number of each resource (such as the backpack SWF resource), such as 1.0.

<? XML version = "1.0" encoding = "UTF-8"?>
<Info>
<Assets name = "backpack">
<Version> 1.0 </version>
</Assets>

<Assets name = "RES2">
<Version> 1.0 </version>
</Assets>
</INFO>

2. Load the above XML in the primary SWF to obtain the latest resource version number. Because you need to ensure that the resource XML you load is up-to-date (XML data is updated frequently. Because the material resources are updated frequently after the project is launched, the version number must be updated.) when loading the XML file, you need to write it like this: Private var xmlurl: string = "assets/version. XML "+ "? "+ Math. Random (); the red part ensures that the XML you load is up-to-date and prevents cache problems.

4. When loading resources, note: url2 = "assets/pack.swf" + "? V "+ version; (the value of version is obtained from the preceding XML)
VaR request: URLRequest = new URLRequest (url2 );

3. When you need to update the resource, first modify the version number of the corresponding resource in the XML above (such as v2.0 and V3.0), and then modify the latest resource SWF, upload the XML file to the server together, which can solve the problem that the cache prevents you from updating resources in time.


Okay. Now I have made an example. Unfortunately, csdn cannot upload project attachments here. The following is the main code:

Package {
Import flash. display. loader;
Import flash. display. Sprite;
Import flash. Events .*;
Import flash.net. urlloader;
Import flash.net. URLRequest;
Import flash. Text. textfield;

Public class testloader extends sprite {
Private var STR: String = "? V ";
Private var url2: String = "assets/1.swf ";
Private var xmlurl: String = "assets/version. xml" + "? "+ Math. Random ();
Private var xmlurl2: String = "assets/version. xml ";
Private var urlloader: urlloader;
Private var curresversion: string; // resource version
Private var userversion: string;
Private var userxmlurl: string;
Private var isusexmlversion: Boolean = true; // whether to use XML version control
Public Function testloader (){

Urlloader = new urlloader;
If (isusexmlversion)
{
Userxmlurl = xmlurl;
}
Else
{
Userxmlurl = xmlurl2;
}

VaR xmlurlrequest: URLRequest = new URLRequest (userxmlurl );
Urlloader. Load (xmlurlrequest );
Urlloader. addeventlistener (event. Complete, xmlloader );
}

Private function xmlloader (Event: Event): void
{
VaR myxml: xml = New XML (urlloader. data );
Curresversion = myxml. Child ("assets") [0]. version; // display our version value

VaR Loader: loader = new loader ();
Configurelisteners (loader. contentloaderinfo );

Url2 = url2 + STR + curresversion;

VaR request: URLRequest = new URLRequest (url2 );
Loader. Load (request );

Addchild (loader );

VaR text: textfield = new textfield;
Text. width = 300;
VaR TXT: String = "";
If (userxmlurl = xmlurl)
{
TXT = "using XML version control ";
}
Else
{
TXT = "No XML version control is used ";
}
Text. Text = TXT + ", resource version:" + url2;
This. addchild (text );
}

Private function configurelisteners (Dispatcher: ieventdispatcher): void {
Dispatcher. addeventlistener (event. Complete, completehandler );
}

Private function completehandler (Event: Event): void {
Trace ("completehandler:" + Event );
}
}
}



Solution to resource version update in web games

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.