Due to a request over at triquitips.com I’m going to be showing you how to create a button mashing game. It’s fun, it’s pretty easy, and here’s how to do it.
Setting up the visuals

On stage I have 2 arrow movie clips instanced as left and right respectively, a large bar movie clip as tall as your final bar will be, but about twice as wide. Finally I have the shape you want to act as your bar as a mask for the bar movieclip, and I added a title. I like titles. I’m going to go ahead and assume you can follow this fine, if you have any problems at any point throw me an email (alternateaccount[AT]frozenhaddock.co.uk) or hit me up on Skype (tdxiiifrozenhaddock), or of course you could throw yourselves to the mercy of the comments below for public advice. Anyway, on with the tutorial
We’ll look at the code, and I’ll explain later on:
button = 1; power = 0; gain = 5; loss = 2; lossspeed = 1; losscount = 0; keylistener = new Object(); keylistener.onKeyDown = function() { if (Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) { trace("doublepress"); } else { if (Key.getCode() == Key.RIGHT && button == 1) { power += gain; button = 2; } if (Key.getCode() == Key.LEFT && button == 2) { power += gain; button = 1; } } }; Key.addListener(keylistener); onEnterFrame = function () { losscount += 1; if (losscount>=lossspeed || power>loss) { power -= loss; losscount = 0; } bar._yscale = power; if (power>=100) { power = 100; trace("yay"); } if (power<=0) { power = 0; trace("lose!"); } }; Right, here you'll see we're using a key listener to capture the key presses from the user, which we then test against. I'll explain more in-depth.
button = 1;
power = 0; //Your starting power
gain = 5; //How much power you gain each buttonpress
loss = 2; //How much you lose each interval
lossspeed = 1; //How long the interval is
losscount = 0;
These are the variables that control the engine, play around with these to get the feel right for your individual project.
keylistener = new Object(); keylistener.onKeyDown = function() { //function Key.addListener(keylistener);
These lines create the Keylistener, and then open a function that will run every time any key is pressed.
if (Key.isDown(Key.LEFT) && Key.isDown(Key.RIGHT)) { trace("doublepress"); }
These lines check if the user is pressing both keys at the same time, if they aren’t, then it will test the next part, which is the following.
if (Key.getCode() == Key.RIGHT && button == 1) { power += gain; button = 2; } if (Key.getCode() == Key.LEFT && button == 2) { power += gain; button = 1; }
This is what actually controls your bashing, if, when a key is pressed, the right one is down and the button variable is showing it should be the right button pressed, add the gain to current power. Same for the left button.
It’s easier to comment the enterFrame function, so here it is:
onEnterFrame = function () {
losscount += 1; //every frame, add 1 to the losscount variable, which times it
if (losscount>=lossspeed || power>loss) { //if the timer is bigger than the interval, and the power is above the minimum
power -= loss; //take the loss var away from the power
losscount = 0; //reset the timer
}
bar._yscale = power; //this makes sure the bar is the right scale
if (power>=100) {
power = 100;
trace("yay"); //You should replace this with a win function
}
if (power<=0) {
power = 0;
trace("lose!"); //you should replace this with a lose function
}
};
Right, that’s it for this, apologies for the lateness and I shall be adding to this in the future. So request features below in the comments or over in the forum (when it’s up)
![[del.icio.us]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/delicious.png)
![[Digg]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/digg.png)
![[Google]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/google.png)
![[LinkedIn]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/linkedin.png)
![[StumbleUpon]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/stumbleupon.png)
![[Windows Live]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/windowslive.png)
![[Yahoo!]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/yahoo.png)
![[Email]](http://frozenhaddock.co.uk/wp-content/plugins/bookmarkify/email.png)


April 22nd, 2008 at 8:02 am
I can’t get the blue bar to go up when I press left or right? It doesn’t seem to work right.
April 22nd, 2008 at 9:06 am
Thanks for doing this for me,
Don’t worry about how long its taken because I know how busy it gets
thanks again
nutter666
April 22nd, 2008 at 8:13 pm
Abzde: Make sure to check the instance names on the bars, if that doesn’t help, try downloading the source at the bottom of the post and compare what you’ve got :)
Nutter: No problem! I hope to add to this soonish, multiplayer and AI bars or something. If you have any ideas for future additions post below or in the forum (when it arrives). Also, send me a link to your project when you’re done!
April 23rd, 2008 at 10:10 pm
Could you post a finished .swf so we can see the finished results ?
April 24th, 2008 at 12:27 am
The swf’s on the end there, just before the source file download :)