Flash name generator

A  flash name generator can be useful as a build-in feature  in various flash games and applications. For example could you use it, when you add a new movieclip to the stage.  A Flash name generator could give these movieclips  random names. Or the star in your Flash game can have a new and random name every time the game is loaded or is restarted.

And then of course because name generators are fun to play with and is a simple way for the beginner to get  in to ActionScript 3 programming and Flash in general.

In this  tutorial I will show you how to make a name generator in Flash and ActionScript.

A name generator script in ActionScript 3

First let’s have a look at the complete ActionScript for the  Flash name generator:

var myName:String;
var random1:Number;
var random2:Number;
var metal:Array = new Array("Gold","Silver","Iron","Steel","Copper","Bronze");
var bird:Array = new Array("Eagle","Hawk","Sparrow","Owl","Kite","Albatross");

generateBtn.addEventListener(MouseEvent.CLICK, makeName);

function makeName(evt:MouseEvent):void {
    random1 = Math.floor(Math.random() * metal.length);
    random2 = Math.floor(Math.random() * bird.length);
    myName = metal[random1] + " " + bird[random2];
    resultName.text = myName;
}

Now lets walk through this simple script

How to make a name generator in ActionScript. Line by line

In Line 1-3 we declare some variables. myName  shall hold the generated name, and 2 variables to hold the random numbers we need in the process.

In line 4-5 we declare the arrays. These are the heart of a name generator and  the arrays are the part you need to change with your own names to make your name generator if you just dowvnload my .fla example file. The arrays can hold as many names or words as you like, just as long as you obey the actionscript syntax.

In line 7 we add an eventlistener to the button. generateBtn refers to the instance of the button on the stage. To that instance we add the eventlistener, that listen after the mouseevent CLICK. When that event happens (when the user click the button) the “makeName” function is run.

In line 9 we declare our makeName function

In line 10-11 we use Flashes buikd in math library to first get a random number based on the length of the array and the we floor that number to get an integer. These to 2 randoms integers are used in line 12 to choose random names from the arrays or word lists. In this case we have 2 random names we concatenate with a space in the middle. The name is saved to the myName variable.

In line 13 we add the text(name) to a dynamic textfield we have made on the stage with the instance name resultName.

The necessary Flash to make the name generator work

You only need to add a few Flash elements to the stage to make it all work.

  • A layer with the above actions script in frame 1
  • A layer with a button with the instance name: generateBtn
  • A layer with a dynamic text field with the instance name: resultName

If you are new to Flash it may sound a little complicated, but don’t worry. Just download my example text, and together with this simple tutorial, it really is quite simple to understand how you make your own Flash generator. Even for the Flash and ActionScript beginner.

Download Flash Name Generator

Here you can  download my Flash name generator. A complete .fla Flash file ready to open and work on yourself.

And as usual:  If you make a cool name generator based on this ActionScript generator throw a comment and I might link back to you or make a short review of your new Flash application.

The example swf name generator

Here’s my simple example file flash name generator in use. Be aware of that the very simple actionsscript and extremely short arrays only give very few different names:

This entry was posted in Name generator stuff and tagged , , , . Bookmark the permalink.

2 Responses to Flash name generator

  1. Jason says:

    Hi,

    I’ve tried playing with the ActionScript but I need it to randomly pick numbers 1 to 10, it won’t allow me to use the ‘numbers’ 1 to 10 but will allow the ‘letters’ one to ten.

    How do I alter the ActionScript for that please?

    For example, you have

    var bird:Array = new Array(“Eagle”,”Hawk”,”Sparrow”,”Owl”,”Kite”,”Albatross”);

    where I want something like

    var bird:Array = new Array(“1″,”2″,”3″,”4″,”5″,”6″,”7″,”8″,”9″,”10”);

    Many thanks

    Jason

    • Niels says:

      Sorry. It’s been too many years since I last time used actionsscript, so I’m not really capable of helping you out there. Hope you find your answer out there. 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *