Code snippets for the future

19 Dec

For the future of my project (i.e, in a week or so’s time when I try to put a basic level together)

For my own amusement and so I dont have to sit through the intro animation a bajillion times but yet not take it out (it’s integral structure dammit) I’ll put in a skip code. This is a short sequence of button presses that will allow me to skip to the fun stuff and not have to do the setup screen every time.

Code!

var keyCodeArray:Array = new Array( 39, 66, 65);
var currentKeyArray:Array = new Array();
function codeListener(event:KeyboardEvent):void {
if (keyCodeArray.toString().indexOf(event.keyCode.toString()) != -1) {
currentKeyArray.push(event.keyCode);
if (currentKeyArray.toString().indexOf(keyCodeArray.toString()) != -1) {
// PUT YOUR FUNCTION IN HERE
currentKeyArray = new Array();
}
} else {
currentKeyArray = new Array();
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN,codeListener);

This is a modifcation of the konami key code listener which can be found here. I just changed it so that its the last three keys of the konami code so its quicker for me to enter, yet it cant be accidentally hit.

B,A,ENTER

The other code snippet I want to put in my project is a high score list.

Surprisingly, there isn’t a tutorial that I could find on making a local highscore table in as3. I could find examples of ones written in as2, online highscore tables, everything but what I wanted.

So, I found out what code I needed to use, (the sharedObject class) and read up on different ways to sort an array. Turns out I can sort arrays by different values in each entry. For example, if I had this:

Name 9000

Name 4050

Name 5999

in an array, I could sort the array by the score alone rather than everything. Pretty handy, and exactly what I need. Then I thought about how I would structure this code. I would shove all the scores into an array, and then before showing the table, sort the array and then save that array to the local system. There by having a persistant local high score table. I’ll look at the as2 examples to see how they display the data in a table, but I have a feeling that you just assign a text box with the different entries of the array.

So that the first text box at the top would have arrayName[0] and the second one would have arrayName[1] and so on. This should arrange the data in the right order and also update itself every time its called.

I’ll be looking at this post to help me understand shared objects, and handily, adobe have posted up all the chapters of AS3 cookbook for free on their site, so I’ll be using that as a reference for the array sorting.

Job done.

Leave a comment