Fragmenti

Some notes on building the Fragmenti Picture Puzzle application by Andrew J. Brown


Why Fragmenti?


Why Silverlight 1.0?


What no source code?


Schematics 4 U


Fragmenti Puzzle Game Functional Schematic

Code notes

// Download manager...Globals...

var LoadManagerTimer = new Object()
var LoadManagerCompletedToken = 0
var LoadManagerFailedToken = 0
var LoadManager = new Object()
var LoadManagerMsg = ''
var ImportCallbackOkay = null
var ImportCallbackFail = null

// fn : LoadManagerDownload : Start async download to client...
//
//  targetUri - for example: 'http://silverlight.net/throws.aspx?id=bigexception
//  onOkay - the name of the function to call when the download is complete
//  onFail - the name of the function to call if the download fails
//  [ timeoutSec ] - a watchdog timeout period in seconds

function LoadManagerDownload(targetUri,onOkay,onFail,timeoutSec){ try {

 LoadManagerMsg = ''
 LoadManagerCallbackOkay = onOkay
 LoadManagerCallbackFail = onFail

 LoadManager = Plugin.createObject('downloader')

 LoadManagerCompletedToken = LoadManager.addEventListener('Completed',LoadManagerCompleted)

 LoadManagerFailedToken = LoadManager.addEventListener('DownloadFailed',LoadManagerFaulted)

 LoadManager.open('GET',targetUri) 
 LoadManager.send()                   // Go Ajax baby, go.

 if ( typeof(timeoutSec) == 'number'){ LoadManagerTimer = setTimeout('LoadManagerTimeout', (1000 * timeoutSec )) }

} catch(e) {}

// fn : LoadManagerFaulted : Handle failed download callback...

function LoadManagerFaulted(sender){ try {

 clearTimeout(LoadManagerTimer)
 LoadManager.removeEventListener('completed',LoadManagerCompletedToken)
 LoadManagerMsg = 'Could not complete download. Network reported ' + LoadManager.StatusText
 LoadManager.abort()
 LoadManagerCallbackFail(null)

} catch(e) {}

// fn : LoadManagerTimeout : Handle download timeout event...

function LoadManagerTimeout(){ try {

 LoadManager.removeEventListener('Completed',LoadManagerCompletedToken)
 LoadManager.removeEventListener('DownloadFailed',LoadManagerFailedToken)
 LoadManager.abort()
 LoadManagerMsg = 'Download time exceeded'
 LoadManagerCallbackFail(null)

} catch(e) {}

// fn : LoadManagerCompleted : Handle download callback...

function LoadManagerCompleted(sender){ try {

 if ( sender.Status != '200' ) {

  LoadManagerMsg = 'No content received'
  LoadManagerCallbackFail(null)

 } else { LoadManagerCallbackOkay(sender) } // Action the next function

} catch(e) {}

// Example usage: Here, the package for a puzzle is to be loaded. On success LoadPuzzleCallback is called and
// the puzzle created. LoadPuzzleFailed provides a soft landing if the package is 404 or, download times out.

LoadManagerDownload(ContentPackUri, LoadPuzzleCallback, LoadPuzzleFailed, ContentTimeout)
// With global variable Root pointing to the plugin,

String.prototype.setX =
  function(i,j,k){Root.findName((typeof(k)=='string')?k+this.toString():this.toString()).setValue(i,j)}

// means code can be shorthanded to, for example,

'pnlPlayGhostImage'.setX('Visibility','Visible')

// And likewise this prototype returns a value from an x:name,

String.prototype.getX =
  function(i,k){return Root.findName((typeof(k)=='string')?k+this.toString():this.toString()).getValue(i)}

z = 'pnlPlayGhostImage'.getX('Visibility')


What next?


Any questions?


Andrew J. Brown - June 2008