Comments
Richard Davies wrote: The UK has a good crop of technology pioneers in cloud computing - for example ElasticHosts, FlexiScale, Flexiant, OnApp - and also some strong government initiatives such as G-Cloud. We will have to see whether this kind of technical leadership converts into swift mass-market adoption or not.
Cloud Expo on Google News


2008 West
DIAMOND SPONSOR:
Data Direct
SOA, WOA and Cloud Computing: The New Frontier for Data Services
PLATINUM SPONSORS:
Red Hat
The Opening of Virtualization
GOLD SPONSORS:
Appsense
User Environment Management – The Third Layer of the Desktop
Cordys
Cloud Computing for Business Agility
EMC
CMIS: A Multi-Vendor Proposal for a Service-Based Content Management Interoperability Standard
Freedom OSS
Practical SOA” Max Yankelevich
Intel
Architecting an Enterprise Service Router (ESR) – A Cost-Effective Way to Scale SOA Across the Enterprise
Sensedia
Return on Assests: Bringing Visibility to your SOA Strategy
Symantec
Managing Hybrid Endpoint Environments
VMWare
Game-Changing Technology for Enterprise Clouds and Applications
Click For 2008 West
Event Webcasts

2008 West
PLATINUM SPONSORS:
Appcelerator
Get ‘Rich’ Quick: Rapid Prototyping for RIA with ZERO Server Code
Keynote Systems
Designing for and Managing Performance in the New Frontier of Rich Internet Applications
GOLD SPONSORS:
ICEsoft
How Can AJAX Improve Homeland Security?
Isomorphic
Beyond Widgets: What a RIA Platform Should Offer
Oracle
REAs: Rich Enterprise Applications
Click For 2008 Event Webcasts
SYS-CON.TV
Top Links You Must Click On


Harnessing Shared Objects
How to Make Use of Draggable Flash Elements

With our tunes playing and the volume knob to about the 3:00PM position (I prefer it loud) lets get started. First you need to create some content to drag. I just made some nice looking mockup boxes for now.

With the content boxes created they need instance names. Anything will work just give them appropriate and easy to remember names. In my case I gave them the instance names news and images accordingly.

Ok with the 'design' section out of the way now it's time to get dirty with some ActionScript. I like to keep everything on a frame in the main timeline. It allows all my code to be easily accessible and even allows me to shorten code! We'll get to that later but for now we need to figure out how we're going to set this up. See Figure 1.

To remember where the objects are dragged to we're going to have to use the shared object. Shared object is an extremely useful tool when designing Flash applications. It's exactly like a cookie but specific to Flash. It is a special beast though. The special object doesn't allow data to be directly written to it like so:

sharedObj.data.myName = "Sam";

All data that is to be written to the shared object has to be by reference. For example:

var name:String = "Sam";
sharedObj.data.myName = name;

This isn't a problem however as we only have to slightly change how we design the code portion of this project. With that sorted out let's define the three variables we'll need for this project - positions array, instances array, and the shared object itself.

//Insert your own instance names into the array
var instances:Array = [_root.news, _root.images];
var so:SharedObject = SharedObject.getLocal
( "pps.com/tutorial/userPositions");
var positions:Array = [[],[]];

I chose to have the positions of the movie clips in the instances array stored in a 2d array. I could have used objects with the properties x and y in a 1d array but I just like arrays. The positions will also be stored in the shared object as a 2d array named posArr. Now onto writing the functions we need.

For each movie clip that we want to be draggable on stage we have to write a handler. You can imagine that this would be a pain because for each instance we'd have to write these handlers.

mc.onPress = function(Void):Void{
  this.startDrag();
}
mc.onRelease = function(Void):Void {
  this.stopDrag();
}

But (bum bum bummm!) thanks to some clever coding we can use a for loop to loop through an array we take in as a parameter to set all our handlers. Lets begin function header and the for loop. (Note: I personally prefer putting all the functions etc I write above where I define my vars. It helps keep code more organized.)

function setHandlers(mcs:Array):Void {
   for( var a:Number = 0; a <= mcs.length-1; a++){

Now we need to setup the on press and on release handlers of our movie clips then just close the for loop and function.

mcs[a].onPress = function(Void):Void{
  if(this.onEnterFrame){
   delete this.onEnterFrame;
  }
  this.startDrag();
}
mcs[a].onRelease = function(Void):Void {
  this.stopDrag();
  setPositions(); //inserts object's x/y coords into SO
  }
}//end for a
}//end setHandlers

Q. Why are we checking for a nonexistent on enter frame handler?
A. Don't worry for now it'll be clear later on why we need this if test.

Q. What's this setPositions function about?
A. The setPositions function is the function we're about to write that will actually update the shared object with the positions of our elements on stage.

The set positions function is extremely simple. First we have to use a for loop to go through the instances array we defined earlier.

function setPositions(Void):Void {
  //Cycle through all the instances
  for( var a:Number = 0; a <= instances.length-1; a++){

Now, we could just reset every instance's x/y coordinates but that'd take extraneous processing power if they hadn't been moved. Because of that we just need to have a simple if test to check if the x or y coordinates of the movie clip in question are different then those in the array.

  if( instances[a]._x != positions[a][0] || instances[a]._y != positions[a][1]){
   positions[a][0] = instances[a]._x;
   positions[a][1] = instances[a]._y;
   }
  }//end for a

With our if test done and for loop closed we just have to set the shared object's 2d array posArr to positions.

so.data.posArr = positions;
so.flush(); //force so to write
}//end setPositions

About Sam Coles
Sam Coles is a writer for MXDJ Journal.

In order to post a comment you need to be registered and logged in.

Register | Sign-in

Reader Feedback: Page 1 of 1

Enterprise Open Source Magazine Latest Stories . . .
Apache Deltacloud, the Red Hat-contributed ReSTful API that abstracts differences between clouds so services on any cloud can be managed – provided of course there’s a driver – has graduated from the Apache Foundation’s incubator and is now a full-fledged Top-Level Project (TLP). The...
With Cloud Expo 2012 New York (10th Cloud Expo) just four months away, what better time to start introducing you in greater detail to the distinguished individuals in our incredible Speaker Faculty for the technical and strategy sessions at the conference... We have technical and st...
AMD said late Tuesday that its chief sales officer Emilio Ghilardi had left the company and that CEO and president Rory Read is going to do his job while a replacement is sought. AMD didn’t say why Ghilardi left but it’s assumed Read wants his own people. Read is relatively new to th...
During the lifespan of M3 (Monitis Monitor Manager) there has always been something lacking – timers. M3 execution procedure was outlined in this previous article. The execution mentioned in the latter was a one-time-execution, whereas server monitoring requires periodic invocati...
Red Hat is putting its bought-in Gluster scale-out NAS storage technology, acquired in October, on the Amazon cloud. It’s styled Red Hat Virtual Storage Appliance for Amazon Web Services and other clouds are supposed to follow in short order.
A new episode of the screencast series is now available at the OpenNebula YouTube Channel. This screencast demonstrates the new easily-customizable self-service portal for cloud consumers. Its aim is to offer a simplified access to shared infrastructure for non-IT end users. The scree...
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
Click to Add our RSS Feeds to the Service of Your Choice:
Google Reader or Homepage Add to My Yahoo! Subscribe with Bloglines Subscribe in NewsGator Online
myFeedster Add to My AOL Subscribe in Rojo Add 'Hugg' to Newsburst from CNET News.com Kinja Digest View Additional SYS-CON Feeds
Publish Your Article! Please send it to editorial(at)sys-con.com!

Advertise on this site! Contact advertising(at)sys-con.com! 201 802-3021


SYS-CON Featured Whitepapers
ADS BY GOOGLE