Archive for January, 2009:
Flash SWFAddress
SWFAddress is a small, but powerful library that provides deep linking for Flash and Ajax. It’s a developer tool, allowing creation of unique virtual URLs that can point to a website section or an application state. Finally, between SWF Address and SWF Object, flash is becoming more and more usable as a coherent and complete format for large and SEO friendly sites.
import SWFAddressEvent;
var pageTextArray:Array=new Array("Click a Button",
"Page1",
"Page2",
"Page3");
SWFAddress.addEventListener(SWFAddressEvent.CHANGE, handleSWFAddressChange);
function SWFHandler(e:SWFAddressEvent) {
var address = SWFAddress.getValue();
SWFAddress.setTitle("Your title appear here > "+address);
// SWFAddress.setValue(String) is how you set the browser url to change
// In this case "page" is the parameter name and "p" is the value of that parameter
// The parameter name should always be preceded by a "?" then followed
// by an "=" that is then followed by the value of that parameter
};
function handleSWFAddressChange(e:SWFAddressEvent) {
trace("SWF Address Value == "+SWFAddress.getValue());
switch(SWFAddress.getValue()){
case "/":
desc.text=pageTextArray[0];
SWFAddress.setTitle(" SWF Address Example: Home ");
break;
}
//By using getValue() and a switch statement I am able to determine if
//the value is "/" and therefore at the main page
switch(SWFAddress.getParameter("page")){
case "page1":
desc.text=pageTextArray[1];
SWFAddress.setTitle(" SWF Address Example: Home ");
break;
case "page2":
desc.text=pageTextArray[2];
SWFAddress.setTitle(" SWF Address Example: Portfolio ");
break;
case "page3":
desc.text=pageTextArray[3];
SWFAddress.setTitle(" SWF Address Example: Contact ");
break;
}
//Using getParameter(String) allows me to see what the value of parameters I set are
//by doing this I am then able to determine what page the user is trying to navigate towards
}
function handleBack(e:MouseEvent):void{
if(SWFAddress.getValue()!=="/"){
SWFAddress.back();
}
// SWFAddress.back() goes to the last URL in browser's history
// it does not stop when it reaches the home page of your site.
// NOTE: to prevent the user from going to the site they were on
// before they reached your site, use a simple if statement that
// allows back functionality to work only if the SWFAddress.getValue()
// is NOT equal to "/"
}
function handleNext(e:MouseEvent):void{
SWFAddress.forward();
// SWFAddress.forward() unlike SWFAddress.back() will
// NOT cause problems if the user goes to far forward
}
function changePage(p:String):void{
SWFAddress.setValue("?page="+p);
// SWFAddress.setValue(String) is how you set the browser url to change
// In this case "page" is the parameter name and "p" is the value of that parameter
// The parameter name should always be preceded by a "?" then followed
// by an "=" that is then followed by the value of that parameter
}
btn_home.addEventListener(MouseEvent.CLICK,goHome);
function goHome(evt:MouseEvent) {
changePage("page1");
}
btn_portfolio.addEventListener(MouseEvent.CLICK,goPortfolio);
function goPortfolio(evt:MouseEvent) {
changePage("page2");
}
btn_contact.addEventListener(MouseEvent.CLICK,goContact);
function goContact(evt:MouseEvent) {
changePage("page3");
}
*You must upload to server or add files location under flash Global Security Settings to test the source code.
Download the source code | Download the class
Rearrange random letters
Here is a little experience that I made that letters randomize with actionscript 3. It is similar to The Da Vinci Code – Official Site where you have to rearrange the correct word pend on the specify meaning descriptions. But to make it more dynamic, the words was pull from Wordsmith A.Word.A.Day rss, what it creates is randomize the letters and all you have to do is click and drag to re-arrange the letters base on the descriptions given.
Flash ShareObject
This tutorial will teach you how to use the AS3 SharedObject Class to store small amounts of data on the end user’s machine. This stored data could be used to store a game’s highscore, a user’s login data, or any other information that you need to be remembered when the user visits your Flash movie again. To understand better, an easy comparison for the SharedObject would be the browser’s cookies.
btn_store.addEventListener(MouseEvent.CLICK,storeData);
function storeData(evt:MouseEvent) {
// Save data to the SharedObject
myLocalData.data.username = input_area.text;
// The next line writes the object to a local file
myLocalData.flush();
}
btn_show.addEventListener(MouseEvent.CLICK,traceData);
function traceData(evt:MouseEvent) {
trace(myLocalData.data.username)
}
Timer class
Flash timers are used to run pieces of code on a specific time sequence. They come in real handy when you want something to be done repeatedly, but not in every frame. This tutorial teaches you the basics of ActionScript timers and how to use them in your flash movies. An AS2 implementation of the AS3 Timer class, and a simple TimerEvent Object used for dispatching by the Timer Object.
Actionscript 2
function runOnce():Void {
trace("runOnce() called @ " + getTimer() + " ms");
clearInterval(intID)
}
Actionscript 3
myTimer.addEventListener(TimerEvent.TIMER, runOnce);
myTimer.start();
function runOnce(event:TimerEvent):void {
trace("runOnce() called @ " + getTimer() + " ms");
}
Or run continuously…
Actionscript 2
function runMany():Void {
trace("runMany() called @ " + getTimer() + " ms");
}
Actionscript 3
myTimer.addEventListener(TimerEvent.TIMER, runMany);
myTimer.start();
function runMany(event:TimerEvent):void {
trace("runMany() called @ " + getTimer() + " ms");
}
External API for Flash–JavaScript Communication
There are situations when you need to create a web application that is part Flash, part HTML. The Flash application requires communicating with code outside of Flash Player such as JavaScript in an HTML page. In cases like this, we need to get ActionScript to invoke JavaScript in order to enable communication. Below is the example.
Javascript
<script>
function callFlashAS(dat) {
var dat = document.getElementById("txt_to_flash").value;
swfFile("planner_main").callFromJavascript(dat);
}
function fromFlash(val){
//alert("Value from flash:" + val);
document.getElementById("txt_from_flash").value = val;
}
function swfFile(swfmoviename) {
if (window.document[movieName])
{
return window.document[movieName];
}
if (navigator.appName.indexOf("Microsoft Internet")==-1)
{
if (document.embeds && document.embeds[movieName])
return document.embeds[movieName];
}
else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
{
return document.getElementById(movieName);
}
}
</script>
HTML
<div id="htmlcontainer">
<form>
<textarea id="txt_from_flash" rows="5" cols="45" class="input">Receive text from Flash!</textarea><br /><br />
<textarea id="txt_to_flash" rows="5" cols="45" class="input">Send this text to Flash!</textarea><br /><br />
<input type="button" onClick="callFlashAS()" name="btn" value="Send To Flash" class="input"/>
</form>
</div>
<div id="swfholder"></div>
<script type="text/javascript">
var so = new SWFObject("externalinterface.swf", "planner_main", "300", "300", "8", "#E8E8E8");
so.useExpressInstall('expressinstall.swf');
so.addParam("quality", "high");
so.addParam("menu", "false");
so.write("swfholder");
</script>
</div>
Actionscript 3
import flash.external.*;
button.addEventListener(MouseEvent.MOUSE_DOWN,callJS);
function callJS(evt:MouseEvent):void {
sendToJavascript(txt_tojs.text);
}
function sendToJavascript(txt){
String(ExternalInterface.call("fromFlash", txt ));
}
ExternalInterface.addCallback("callFromJavascript", thefunc);
function thefunc(str:String) {
txt_fromjs.text = "here" + str
}
