Full Actionscript 3.0 Code for Horse Racing Game - Here is ...

anthropologistbarrenΛογισμικό & κατασκευή λογ/κού

4 Ιουλ 2012 (πριν από 5 χρόνια και 3 μήνες)

286 εμφανίσεις

Full Actionscript 3.0 Code for Horse Racing Game
 
 
Passy’s World of ICT – Horse Race Game 2011  Page 1 
 
 
Here is the Frame 1 Introduction Screen Actionscript:

// Horse Racing Game that uses Flash Collision Detection to End
// AS 3.0 Code for Frame 1 Start Game Screen
// The button on the screen has an instance name of
// "startBTN" which links it into this Actionscript Code.
// Always have a stop at the beginning of the Frame.
stop();
// When the start button is clicked in Frame 1, make Flash
// go to Frame 2 and start the race there.
startBTN.addEventListener(MouseEvent.CLICK, startRACE)
function startRACE(event:MouseEvent) {
gotoAndStop(2);
}


Here is the final Actionscript Code for Frame 2 of the Horse Race Game.

// Frame 2 AS 3 Code to control the Horse Race
stop();
// Make the winner message and Restart button invisible
text_mc.visible = false;
restartBTN.visible = false;
// Define and initialize arrow variables for Horse 2 movement.
// Boolean variable are Yes/No or True/False type indicators.
var leftArrow:Boolean = false;
var rightArrow:Boolean = false;
// set event listeners for user pressing any keys on keyboard.
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
// set arrow variables to True when arrow key pressed
// Flash has a number code for every key on the keyboard.
// For arrow keys, codes are: Left=37, Right=39, Down=40, Up=38.
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = true;
} else if (event.keyCode == 39) {
rightArrow = true;
}
}

Full Actionscript 3.0 Code for Horse Racing Game
 
 
Passy’s World of ICT – Horse Race Game 2011  Page 2 
 
// set arrow variables to false when arrow key released
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
leftArrow = false;
} else if (event.keyCode == 39) {
rightArrow = false;
}
}
// set up an event listener so that Horse2 can be moved as soon as we
// enter into this frame of the animation.
stage.addEventListener(Event.ENTER_FRAME, movehorse2);
// Define the function which will Move Horse 2 horizontally
// whenever the arrows are pressed. We have also set up Horse 2
// to have a speed value in the variable definition below.
function movehorse2(event:Event) {
var speed:Number = 12; /* sets the speed horse moves at */
if (leftArrow) {
horse2_mc.x -= speed;
}
else if (rightArrow) {
horse2_mc.x += speed;
}
}
// Move horse 6 along automatically till he hits the rectangle on finish post.
// The enter frame makes horse6 start straight away.
// We have a little red rectangle on the finish post for
// the horse to hit and we will detect this collision.
// Horse 6 then stops moving because the hit event has happened.
//
// We also have the same type of collision set up for Horse2, but on each
// finish we need to remove the event listener from Horse2 to stop it from
// being moved any further with the arrow key.
horse6_mc.addEventListener(Event.ENTER_FRAME, horse6Hit);
function horse6Hit(event:Event):void {
if (horse6_mc.hitTestObject(rectangle_mc)) {
text_mc.visible = true;
stage.removeEventListener(Event.ENTER_FRAME, movehorse2);
/* Give Restart Option */
restartGame();
}
else if (horse2_mc.hitTestObject(rectangle_mc)) {
text_mc.visible = true;
stage.removeEventListener(Event.ENTER_FRAME, movehorse2);
/* Give Restart Option */
restartGame();
}
Full Actionscript 3.0 Code for Horse Racing Game
 
 
Passy’s World of ICT – Horse Race Game 2011  Page 3 
 
else
/* move Horse 6 along 4 pixels */
{
horse6_mc.x+=4;
}
}
// Restart the game is a function inside a function.
// First we make the Restart button appear, then we set up an
// event listener to wait to see if anyone clicks the button.
// If the Restart button is clicked, then we reactivate movement
// buttons for Horse2, and go back to the game's start in Frame 1.
function restartGame() {
restartBTN.visible = true;
/* click the restart button to go to Frame 2 and start race. */
restartBTN.addEventListener(MouseEvent.CLICK, restartRACE)

function restartRACE(event:MouseEvent) {
stage.addEventListener(Event.ENTER_FRAME, movehorse2);
gotoAndStop(1);
/* could not get it to restart at frame 2, and there is */
/* a glitch where Horse6 gets randomly faster on each */
/* restart of the race, and becomes unbeatable. */
/* But after a few races, he slows back down to his */
/* Original Race 1 speed and can be beaten.*/
}
}


End of Actionscript Code