Tracing to a textfield on stage with AS3

You can download the example here: tracer

You can see trace statements in the output window within Flash. You can also use a debugger for this however what if you can’t install them or have no access to get them?

Here’s an option… tracing on stage. Create a text field and trace to the stage. I hope this is a good alternative to people who wouldn’t want to install anything else.

I may place this on the stage in an area that doesn’t interfere with the movie just to make sure the functions are being called.

I called the movieClip traceField.

within my function along with my trace statement:

trace("Tracing init function)";

I also added

traceField.text = "Tracing init function to stage";

That’s it. Seems to make my workflow easier.

stop();
init();
function init():void{
        //Call trace statements
        trace ("Init function has been called");
        traceField.text = "Init function has been called";

        var cbtn:MovieClip = new clickbtn();
        cbtn.x = 140;
        cbtn.y = 140;
        addChild(cbtn);
        cbtn.buttonMode = true;
        cbtn.addEventListener(MouseEvent.CLICK, btnClick);
        cbtn.addEventListener(MouseEvent.ROLL_OVER, btnRollOver);
        cbtn.addEventListener(MouseEvent.ROLL_OUT, btnRollOut);
}
function btnClick(e:MouseEvent):void{

        //Call trace statements
        trace("Button has been Clicked");
        traceField.text = "Button has been Clicked";

}

function btnRollOver(e:MouseEvent):void{
       //Call trace statements
        trace("Button rolled over");
        traceField.text = "Button rolled over";

}

function btnRollOut(e:MouseEvent):void{
       //Call trace statements
        trace("Button rolled out");
        traceField.text = "Button rolled out";

}

How to create a email form with AS3 & PHP

Download the emailer here it was made with lots of recycled code so really no credit is necessary.

I made two movieclips that I used as buttons: sendbtn and resetbtn.

I created input fields called: yourName, fromEmail, yourSubject and YourMsg.

They post the variables: name, from, subject and msg to the PHP.

/*************************************
Buttons
**************************************/
sendbtn.buttonMode = true;
sendbtn.addEventListener(MouseEvent.CLICK, submit);
resetbtn.buttonMode = true;
resetbtn.addEventListener(MouseEvent.CLICK, reset);
init();

/*************************************
Variables needed
**************************************/
var timer:Timer;
var varLoad:URLLoader = new URLLoader;
var urlRequest:URLRequest = new URLRequest( "mail.php" );
urlRequest.method = URLRequestMethod.POST;

/*************************************
Functions
**************************************/
function init():void{
//Set all fields to empty
yourName.text = "";
fromEmail.text = "";
yourSubject.text = "";
YourMsg.text = "";
}

function submit(e:MouseEvent):void{
//Check to see if any of the fields are empty
if( yourName.text == "" || fromEmail.text == "" ||
yourSubject.text == "" || YourMsg.text == "" )
{
valid.text = "All fields need to be filled.";
}

//Check if you're using a valid email address
else if( !checkEmail(fromEmail.text) )
{
valid.text = "Enter a valid email address";
}
else
{
valid.text = "Sending over the internet...";
var emailData:String = "name=" + yourName.text+ "&from="
+ fromEmail.text+ "&subject="
+ yourSubject.text+ "&msg="
+ YourMsg.text;

var urlVars:URLVariables = new URLVariables(emailData);
urlVars.dataFormat = URLLoaderDataFormat.TEXT;
urlRequest.data = urlVars;
varLoad.load( urlRequest );
varLoad.addEventListener(Event.COMPLETE, thankYou );
}
}

function reset(e:MouseEvent):void{
init(); //call the initial clear function
}

function checkEmail(s:String):Boolean{
//This tests for correct email address
var p:RegExp = /(\w|[_.\-])+@((\w|-)+\.)+\w{2,4}+/;
var r:Object = p.exec(s);
if( r == null )
{
return false;
}
return true;
}

function thankYou(e:Event):void{

var loader:URLLoader = URLLoader(e.target);
var sent = new URLVariables(loader.data).sentStatus;
if( sent == "yes" )
{
valid.text = "Thanks for your email!";
timer = new Timer(500);
timer.addEventListener(TimerEvent.TIMER, msgSent);
timer.start();
}

else{
valid.text = "Oh no! Something is wrong! Try again...";
}
}

function msgSent(te:TimerEvent):void
{
if( timer.currentCount >= 10 )
{
init();
timer.removeEventListener(TimerEvent.TIMER, msgSent);
}
}

————-

Then I created a PHP file called mail.php with the code:

<?php
$yourName = $_POST['name'];
// the variable needs to match your Actionscript
$fromEmail = $_POST['from'];
// the variable needs to match your Actionscript
$yourSubject = $_POST['subject'];
// the variable needs to match your Actionscript
$YourMsg = $_POST['msg'];
// the variable needs to match your Actionscript
if( $yourName == true )
{
$sender = $fromEmail;
$yourEmail = "yourEmailAddress@yourdomain.com";
// This will be your email address so please change this
$ipAddress = $_SERVER['REMOTE_ADDR']; // This gets the user's ip Address
$emailMsg = "Name: $yourName  sent this from IP: $ipAddress \n\n
Return Email: $sender \n\nSubject: $yourSubject \n\nMessage: \n\n
$YourMsg \n\nThis email was sent using a form on your site";
$return = "From: $sender\r\n" . "Reply-To: $sender \r\n" .
"X-Mailer: PHP/" . phpversion();
if( mail( $yourEmail, "$yourSubject", $emailMsg, $return ) )
{echo "sentStatus=yes";}
else
{echo "sentStatus=no";}}
?>