Sunday, 9 May 2010

Mailto link from external text file in AS3

I was emailed the following question below about how to load an email address from an external text file from the mailto in Actionscript 3 tutorial.

I have been searching around trying to find a tutorial that will show how to make a mailto link work in an external text file that is being loaded into flash. Do you have any suggestions?

For my solution I have used the following two tutorials: Load external text in Actionscript 3 and mailto in Actionscript 3. I have basically defined a variable to hold the externally load text which then gets passed into URLRequest() method in the mailto function.


Mailto link from external text file in AS3

Step 1

Open up a Flash AS3 file save it.
Then create or import an image you wish to use as the mailto button. I have used an image of an envelope, but you can use whatever image you wish. Convert your button in a symbol (F8) and give it the instance name: ‘mailto_btn’.




Step 2

Open up a text editing program of your choice and write the email address that you want to be loaded externally. For example: somebody1234567@example.com. Save the text file with the name “external_address” in the same directory of your fla file.


Step 3

On the timeline insert a new layer called ‘Actions’ then open up the Actions panel and enter the following code:

//Variable to hold the external email address.
var external_address:String;

//Creates a new instances of the loader class.
var my_loader:URLLoader = new URLLoader();

//Create a new instance of the URLRequest object with the path
//to the external text file.
var my_req:URLRequest=new URLRequest("external_address.txt");

//Event listener to listen to when the file has finished loading.
my_loader.addEventListener(Event.COMPLETE, loadExternalText);

//This loads the text file into the URLloader.
my_loader.load(my_req);

//This assigns the load text file to the external_address variable.
function loadExternalText(event:Event):void {
external_address=my_loader.data;
}

//This adds a mouse click event to the mailto button.
mailto_btn.addEventListener(MouseEvent.CLICK, mailto);

//The mailto function open up email application to send email.
function mailto(event:MouseEvent):void {
var email:URLRequest=new URLRequest("mailto:"+external_address);
navigateToURL(email, "_blank");
}

Step 4

Finally test your movie clip Ctrl + Enter and you should now be able to load an email address from external text files.

2 comments:

Kenneth 10 May 2010 at 15:38  

Hello, I meant to say that the link is text, not an image. The link is copy in the external text file. Is this still possible in AS3?

Thx!

iliketo 12 May 2010 at 13:44  

@Kenneth

Not sure what you mean.

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP