Monday, 7 April 2014

Working Processes: Creating the Game - Adding a username feature

Without giving too much of the storyline away, I'm going to tell you why I believe a username feature will be so important for my artifact.

When the users enter their name, their name will be stored in the Flash as a string.

By doing so, I will be able to later refer back to their name on the computer and possibly other items too. This will therefore make the game more interactive as though it is them who is trapped in the room.

I created the following code to ask for the username:

var usersName:String;
stage.addEventListener(KeyboardEvent.KEY_DOWN,usersNameListener);
function usersNameListener(event:KeyboardEvent) {

    // if the key is ENTER
    if (event.charCode==13) {
        usersName=usersNameTxt.text;
        trace("You entered a name with " + usersName.length + " characters");
        if (usersName.length>=3) {
            stage.removeEventListener(KeyboardEvent.KEY_DOWN,usersNameListener);
            trace("Hello " + usersName + ", thank you for playing my game");
            gotoAndStop("viewMain");
        } else if (usersName.length<3){
            trace("Name needs more characters");
            nameCharacters_MC.play();
        }
    }
}

Since I wanted to be sure that the user actually enters a name rather than just skipping ahead, I added a character count by using an AS3 script. I also added a character limit, which didn't need to be scripted but merely changed in the settings of the input text field.

The character count was not something I had done before, I tried several methods including:
maxchars<=3
but in the end, I managed to find a forum post which touched upon this subject.

The code I had in fact been looking for was length

So I applied it accordingly, as seen above.

I used this Adobe page on Textfields to familiarise myself with them:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/text/TextField.html
and then for character count, this was something I hadn't done before
http://www.kirupa.com/forum/showthread.php?179466-is-it-possible-to-do-a-character-count-in-flash

No comments:

Post a Comment

Note: only a member of this blog may post a comment.