Use mongodobject to cache the demo of the game framework to a local device and version update.

Source: Internet
Author: User

Source code
There are two projects, one is versionloader
. One is mainframe.
Versionloader is responsible for loading
Frame, and for version identification.
Versionloader uses XML files
Obtain the Server File version. Compare with the file stored locally in mongodobject to determine whether to reload mainframe or retrieve it in mongodobject.
A server version marked with XML is used.
I hope to help you.
The following is the source code:

Package
{

Import flash. display. loader;
Import flash. display. Sprite;
Import flash. Events. event;
Import flash. Events. ioerrorevent;
Import flash. Events. netstatusevent;
Import flash. Events. securityerrorevent;
Import flash.net. Export dobject;
Import flash.net. Export dobjectflushstatus;
Import flash.net. urlloader;
Import flash.net. urlloaderdataformat;
Import flash.net. URLRequest;
Import flash. utils. bytearray;

[SWF (width = "1000", Height = "600", framerate = "31")]
Public class versionloader extends Sprite
{

Private var configloader: urlloader;
Private var framelocalloader: loader;
Private var frameremoteloader: urlloader;

// Version ID
Private var version: string;

Private Static const main_frame_url: String = "mainframe.swf ";

/**
* Constructor
*/
Public Function versionloader ()
{
Super ();
Loadconfig ();
}

/**
* Load the configuration file
*/
Private function loadconfig (): void
{
Configloader = new urlloader ();
Configloader. Load (New URLRequest ("version. xml? V = "+ new date (). gettime ())));
Configloader. addeventlistener (event. Complete, configloadcompleted );
Configloader. addeventlistener (ioerrorevent. io_error, configloaderror );
}

/**
* Complete Configuration File Processing
*/
Private function configloadcompleted (EVT: Event): void
{
Trace ("Config Load completed ");
Configloader. removeeventlistener (event. Complete, configloadcompleted );
Configloader. removeeventlistener (ioerrorevent. io_error, configloaderror );
Configloader. Close ();
VaR xmldata: xml = xml(evt.tar get. data );
Version = xmldata. version;
Loadmainframe ();
}

/**
* Failed to load the configuration file.
*/
Private function configloaderror (EVT: Event): void
{
Trace ("Config load error ");
}

/**
* Load the main framework
*/
Private function loadmainframe (): void
{
VaR mainframedata: bytearray = (readlocal ("mainframe", Version) as bytearray );
// Load locally to data
If (mainframedata)
{
Trace ("load shell from local export dobject ");
Loadframefrombytes (mainframedata );
}
// Reload data
Else
{
Trace ("Load new shell ");
Loadframefromremote ();
}
}

/**
* Load the main framework in bytes Mode
*/
Private function loadframefrombytes (bytedata: bytearray): void
{
Framelocalloader = new loader ();
Framelocalloader. contentloaderinfo. addeventlistener (event. init, framelocalloadcompleted );
Framelocalloader. loadbytes (bytedata );
}

/**
* The local master framework is loaded.
*/
Private function framelocalloadcompleted (EVT: Event): void
{
Addchildat (framelocalloader, 0 );
}

/**
* Remote loading of the main framework
*/
Private function loadframefromremote (): void
{
Frameremoteloader = new urlloader ();
Frameremoteloader. dataformat = urlloaderdataformat. Binary;
Frameremoteloader. addeventlistener (event. Complete, frameremoteloadcompleted );
Frameremoteloader. addeventlistener (ioerrorevent. io_error, frameremoteloaderror );
Frameremoteloader. addeventlistener (securityerrorevent. security_error, frameremoteloaderror );
Frameremoteloader. Load (New URLRequest (main_frame_url + "? V = "+ version )));
}

/**
* The master framework is remotely loaded.
*/
Private function frameremoteloadcompleted (EVT: Event): void
{
Trace ("load frame from remote completed ");
VaR Date: bytearray = evt.tar get. Data as bytearray;
Loadframefrombytes (date );
Savelocal ("mainframe", date, Version)
}

/**
* Failed to remotely load the main framework.
*/
Private function frameremoteloaderror (EVT: Event): void
{
Trace ("load frame from remote error ");
}

/**
* Save to local
*/
Private function savelocal (Name: String, data: object, version: string): void
{
Trace ("save data to local name:" + name + ", version:" + version );
VaR shared: * = NULL;
VaR flushresult: * = NULL;
VaR name: * = Name;
VaR data: * = data;
VaR version: * = version;
Try
{
Shared = export dobject. getlocal (name ,"/");
If (shared)
{
Shared. Data ["version"] = version;
Shared. Data ["data"] = data;
Flushresult = shared. Flush ();
If (flushresult = export dobjectflushstatus. Pending)
{
Shared. addeventlistener (netstatusevent. net_status, sharednetstatus );
}
}
}
Catch (ERR: Error)
{
Trace ("Save to local error ");
}
}

/**
* Read from local
*/
Public Function readlocal (Name: String, version: string): Object
{
Trace ("read data from local name:" + name + ", version:" + version );
VaR shared: * = NULL;
VaR name: * = Name;
VaR version: * = version;
Try
{
Shared = export dobject. getlocal (name ,"/");
If (shared)
{
If (shared. Data ["version"]! = Version)
{
Shared. Clear ();
}
Else
{
If (shared. Data. hasownproperty ("data "))
{
Return (shared. Data ["data"]);
}
}
}
}
Catch (ERR: Error)
{
Trace ("read from local error ");
}
Return (null );
}


Private function sharednetstatus (Event: netstatusevent): void
{

}
}
}

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.