Action Script 3: Component and SharedObject Example

Here is a simple tutorial on using prebuilt AS3 components and SharedObject.

SharedObject Example

Class soExample:

package {
import fl.controls.Button;
import fl.controls.Label;
import fl.controls.TextInput;

import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.SharedObject;

public class soExample extends Sprite {
private var getData_btn:Button;
private var setData_btn:Button;
private var delData_btn:Button;
private var titleLbl:Label;
private var statLbl:Label;
private var soText:TextInput;
private var _mc:Sprite;
private var mySO:SharedObject;

public function soExample(_mc:Sprite) {
this._mc = _mc;
updateUI();
updateDisplay();
getSO();
}
private function updateUI():void {
getData_btn = createButton(getData_btn, "Get Shared Object", 150, 10, 100, "get_btn");
setData_btn = createButton(getData_btn, "Store Shared Object", 150, 10, 130, "set_btn");
delData_btn = createButton(getData_btn, "Delete Shared Object", 150, 10, 160, "del_btn");
titleLbl = createLabel(titleLbl, "<b>AS3 - Shared Object Example</b>", 200, 10, 10);
statLbl = createLabel(statLbl, "", 400, 10, 200);
soText = createTextInput(soText, 200, 10, 50);
}
private function updateDisplay():void {
_mc.addChild(titleLbl);
_mc.addChild(soText);
_mc.addChild(getData_btn);
_mc.addChild(setData_btn);
_mc.addChild(delData_btn);
_mc.addChild(statLbl);
}
private function createButton(btn:Button, lbl:String, w:uint, x:uint, y:uint, nme:String):Button {
btn = new Button();
btn.label = lbl;
btn.width = w;
btn.move(x, y);
btn.name = nme;
btn.addEventListener(MouseEvent.CLICK, clickHandler);
return btn;
}
private function createLabel(lblRef:Label, lbl:String, w:uint, x:uint, y:uint):Label {
lblRef = new Label();
lblRef.htmlText = lbl;
lblRef.width = w;
lblRef.move(x, y);
return lblRef;
}
private function createTextInput(tiRef:TextInput, w:uint, x:uint, y:uint):TextInput {
tiRef = new TextInput();
tiRef.htmlText = “”;
tiRef.width = w;
tiRef.move(x, y);
return tiRef;
}
private function clickHandler(event:MouseEvent):void {
respondToMouseEvent(event.target.name);
}
private function respondToMouseEvent(nme:String):void {
switch (nme) {
case "get_btn":
getSO();
break;
case "set_btn":
setSO();
break;
case "del_btn":
deleteSO();
break;
}
}
private function getSO():void {
soText.text = "";
mySO = SharedObject.getLocal("userData");
if (mySO.size == 0) {
// Shared object doesn’t exist.
statLbl.htmlText = "Status: SharedObject not found.";
} else {
statLbl.htmlText = "Status: SharedObject value is - <b>"+mySO.data.userName+"</b>";
}
}
private function setSO():void {
if (soText.text != "") {
mySO.data.userName = soText.text;
mySO.flush();
}
getSO();
}
private function deleteSO():void {
mySO.clear();
getSO();
}
}
}

Now, create object of soExample and pass movieclip reference (in which all the components will be added at runtime) as parameter:

var mySoExample:soExample = new soExample(this);

Note: Be sure to add “Button”, “Label” & “TextInput” components to the library of that movie.

Publish and test the flash file. Simple isn’t it 🙂

Advertisements

About Tushar

Hi, I am Tushar Wadekar from Pune, India. I am an Adobe Certified Flash Developer. My Interests are: Flash, ActionScript, Flex, AIR, PHP, Ruby on Rails. You can reach me at: tusharwadekar[at]gmail[dot]com Thank you! Tushar
This entry was posted in ActionScript3, AS3, Flash. Bookmark the permalink.

15 Responses to Action Script 3: Component and SharedObject Example

  1. Pingback: ActionScript 3: Serializing Classes using registerClassAlias « Tushar Wadekar

  2. Mike says:

    Nice blog. I have one similiar to it. Yours is a little more advanced though.

  3. Satish says:

    nice blog!!

    Can u help me in writing some actionscript3.0 code?

    rgds
    Satish

  4. Satish says:

    i want AS3 coding for cropping an image by selecting a part of it

    Can u help out?
    rgds
    Satish

  5. Satish says:

    i want AS3 coding for cropping an image by selecting a part of it

    Can u help out?

    Satish

  6. I’ve been surfing online more than 3 hours today, yet I never found any interesting article like yours. It’s pretty worth enough for me.
    In my view, if all webmasters and bloggers made good content as you did,
    the web will be much more useful than ever before.

  7. Teri says:

    I like the valuable information you provide in your articles.
    I’ll bookmark your weblog and check again here frequently. I am quite certain I’ll learn
    lots of new stuff right here! Good luck for the next!

  8. I quite like reading an article that can make men and women think.

    Also, thanks for allowing for me to comment!

  9. I’ll immediately take hold of your rss as I can’t in finding your
    email subscription link or e-newsletter service. Do you have any?
    Please let me realize in order that I could subscribe.
    Thanks.

  10. These are really great ideas in on the topic of blogging.
    You have touched some nice points here. Any way keep up wrinting.

    My site about engineering: http://www.ocala.com/

  11. Hey There. I discovered your weblog the usage of msn.
    That is a very neatly written article. I’ll make sure to bookmark it and come back to read extra of your useful information. Thanks for the post. I will certainly return.

  12. I like reading through an article that will make men and women think.
    Also, thank you for allowing me to comment!

  13. They are found mostly on large tracts of land away from it all.
    Mostly everybody knows what’s healthy and what’s not.
    Generally a number of us weight loss program
    to shed pounds because were too heavy along with be aware that our diet plan can have become with this predicament for
    a start.

  14. We stumbled over here coming from a different website
    and thought I may as well check things out. I like what I see so i am just following you.
    Look forward to looking into your web page again.

  15. I have attempted to your past 35 an extended time to shed and Carry on to maintain OFF my stomach undesirable body fat.
    I have attempted just about every point from diet program regime capsules, personal trainers,
    every food system ever proposed, I at present shell out
    on two gym memberships, at any provided time, and have not had the time
    to suit your needs to employ the benefits of both viewed
    as certainly one of them in over a 12 months. I have to state that only point
    that assisted me diet plans to lose weight in 2 weeks
    :: microfinance.ca :: hoping the ambiance, ease and in addition the availability
    around the amenities currently currently being opened from five:
    00a.m till 10:00pm would inspire me to stay on course, attend several from the
    cardio lessons, aquatics lessons, phase programs etc…

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s