« Return
Action Script 2 code for the 'Monkey Gun' Flash game:
//Main timeline:
stop();
var bullets:Array = new Array();
var i:Number = 0;
var hits:Number = 0;
var gold_hits:Number = 0;
var red_hits:Number = 0;
var time_left:Number = 60;
var myInterval = setInterval(this,"callMe",1000);
function callMe() {
time_left--;
if (time_left <= 0) {
clearInterval(myInterval);
nextScene();
}
}
gun_btn.onRelease = function()
{
i++;
attachMovie("bullet","b"+i,getNextHighestDepth(),{_x:188, _y:141});
// the bullet array contains a simple sequence of numbers
bullets[bullets.length] = i;
trace("length of bullets array before removing an element: " + bullets.length);
// to keep the bullets array from getting huge and hogging memory, only let there be
// the most recent 10 bullets in there. Unload the movie clip for the oldest bullet,
// and use the shift method to remove the first array element:
if (i > 10) {
firstElement = bullets[0];
trace ("firstElement: " + firstElement);
removeMovieClip("b"+firstElement);
bullets.shift();
trace("length of bullets array after removing an element: " + bullets.length);
}
}
onEnterFrame = function ()
{
red_timeout_remaining--;
for (j=0; j<bullets.length; j++)
{
n = bullets[j];
_root["b"+n]._x += 80;
if (_root["b"+n].hitTest(_root.red_balloon_rising_mc))
{
/* When a shot is timed well, the same bullet will overlap the balloon
for two frames. The following use of the "prev_successful_bullet" flag
makes a successful shot count as one hit instead of two.
Also, to prevent a bullet's collision with an already
popped balloon from counting, I use a "timeout_remaining" flag so that 10 frames
need to pass after a collision before another collision will be credited:
*/
if ((n != red_prev_successful_bullet) && (red_timeout_remaining <= 0))
{
_root.red_balloon_rising_mc.red_balloon_mc.gotoAndPlay("popped");
red_prev_successful_bullet = n;
red_timeout_remaining = 10;
}
}
}
gold_timeout_remaining--;
for (j=0; j<bullets.length; j++)
{
n = bullets[j];
_root["b"+n]._x += 80;
if (_root["b"+n].hitTest(_root.gold_balloon_rising_mc))
{
if ((n != gold_prev_successful_bullet) && (gold_timeout_remaining <= 0))
{
_root.gold_balloon_rising_mc.gold_balloon_mc.gotoAndPlay("popped");
gold_prev_successful_bullet = n;
gold_timeout_remaining = 10;
}
}
}
score = ( ((gold_hits*100)+(red_hits*50)) - (i*10));
if (score < 0) { score = 0; }
hits = gold_hits + red_hits;
}