Thursday, January 29, 2009

Dynamic buttons in Flash using ActionScript 2.0

I am often needing Flash to create dynamic buttons from an Array (or external TXT (text) document). Here's how I do it:

1. Make your button an actual MovieClip on the stage however you want them all to look/work. Create a dynamic text field (center justified - or whatever) within the button with the instance property of "text_txt" for this example. (You can name it whatever you want, but you'll need to change some of the code below).

2. In the library find your MovieClip. Open the Symbol properties dialog.

3. Select the "export of actionscript" check box. Identifier "myButton"... again, for this example only. Click OK.

4. Now return to the stage and in frame one of a layer, I've named my top-most layer "actions". Copy and paste the following:

for(i=0; i
target = this.attachMovie("myButton", "myButton"+i, i, {_x:10, _y:30*i} );
target.text_txt.text = nav[i];
target.myLink = url[i];
trace(myLink);
target.nr = i;
target.onPress = function(){
//getURL(this.myLink);
trace("Hi, I'm "+this._name+" and my number is "+this.nr+". The link is: " + this.myLink);
}
}

With some extra work we can make this a horizontal menu, getting the width of each text box and moving the rest.... I'll post that another time.

5. I created an Array that I want to use for the labels of my buttons. Eventually I will pull these labels from a database table, but for now, paste this info above the previous code.

var nav:Array = Array();

nav[0] = "nav 1";
nav[1] = "nav 2;
nav[2] = "nav 3";
nav[3] = "nav 4";
nav[4] = "nav 5";
nav[5] = "nav 6";
nav[6] = "nav 7";

6. Now we need these links to do something, right? So let's create an Array for our URLs. Paste this array just below the previous but before the button code "for" stuff.

var url:Array = Array();

url[0] = "http://www.adobe.com";
url[1] = "my link 1";
url[2] = "my link 2";
url[3] = "my link 3";
url[4] = "my link 4";
url[5] = "my link 5";
url[6] = "my link 6";


6. Ta da! That's all that is to it.

7. Publish your file.

Tuesday, January 27, 2009

Simple Javascript email verification

Here is a simple email verification script in Javascript.  Just pass in the email address and it will return true or false depending on if the email is valid.  It will also do an alert popup if it is invalid.

function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
if (str.indexOf(at)==-1){
  alert("Invalid email address of attendee.")
  return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
  alert("Invalid email address of attendee.")
  return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
   alert("Invalid email address of attendee.")
   return false
}

if (str.indexOf(at,(lat+1))!=-1){
   alert("Invalid email address of attendee.")
   return false
}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
   alert("Invalid email address of attendee.")
   return false
}

if (str.indexOf(dot,(lat+2))==-1){
   alert("Invalid email address of attendee.")
   return false
}
if (str.indexOf(" ")!=-1){
   alert("Invalid email address of attendee.")
   return false
}

  return true
}

Monday, January 26, 2009

Autocomplete textfields

In my search for a cool autocomplete script for my text fields, I came across one that I'd actually recommend.  If you aren't familiar with autocomplete scripts, just start typing a url into the latest version of firefox and you'll see what I mean.


The script uses jQuery, which is a javascript library.  It is really simple to use, everything is included in the download.  It comes with a demo page and you can use to make it work the way you want it.

Wednesday, January 21, 2009

Flash Random Number generator

A little actionscript to get a random number (n) is this case.




low = 0;
high = 360;

function randRange(min:Number, max:Number):Number {
var randomNum:Number = Math.floor(Math.random() * (max - min + 1)) + min;
return randomNum;
}
for (var i = 0; i < 1; i++) {
var n:Number = randRange(low, high)
trace(n);
}

Wednesday, January 14, 2009

Why I hate Blogger!

This is just a rant but blogger is terrible.  It has the worst text editor ever created for a blogging software.  The list can go on but I think we're going to switch to something new as soon as we have some free time (if ever!).

Wow, I just need to vent because it is a pain to use blogger.  I don't recommend it!

IE6 Problem with Form Submit Image Buttons

A lot of times we use images as our submit buttons for forms. I'm sure you've seen it a lot. Well, say you wanted to test if the form was submitted and used the standard way, lets say in PHP, of seeing if the submit button was sent, for example like this:

if (isset($_REQUEST['mysubmitbutton']))
{ ...}

With IE6 this wouldn't work if your submit button was an image. So if you had this as your submit button:

<input name="mysubmitbutton" type="image" value="1" src="someimage.png" alt="" />

Then your script wouldn't work. The reason is that IE6 WILL NOT send over the variable mysubmitbutton.

The workaround is to know that IE6 will send over other information (along with all other browsers). It sends the x and y coordinates of where you clicked on the image. So you'll see mysubmitbutton_x and mysubmitbutton_y. So if you modify your code to look for either of these variables, you'll be good to go!

Credit is to be given to this discussion forum: Differences in form handling btw Mozilla and IE?

Thursday, January 8, 2009

Get referrer with PHP

Here is some code to find out the referrer to a page in PHP.  This can be very useful if you want to redirect back after login or something like that.  You can also set it up to make sure a visitor came to the page from within your site or something like that.

<?php
session_start();

if(!isset($_SESSION[’referrer’])){

//get the referrer
if ($_SERVER[’HTTP_REFERER’]){
$referrer = $_SERVER[’HTTP_REFERER’];
}else{
$referrer = “unknown”;

}
//save it in a session
$_SESSION[’referrer’] = $referrer; // store session data
}
?>


Hope this is useful.  The reference is: Get a referral URL from the session using PHP