/* This makes heavy use of the Flash 8 Drawing API
there is a good overview of it here:
http://www.adobe.com/devnet/flash/articles/image_api_print.html
*/
//import classes we will be using
import flash.display.BitmapData;
import mx.managers.DepthManager
//http://livedocs.macromedia.com/flash/mx2004/main_7_2/wwhelp/wwhimpl/ common/html/wwhelp.htm?context=Flash_MX_2004&file=00002014.html
import flash.geom.Rectangle
//____________________________________________________________________ ___
// Initial Local Variables & display Tutorial
if(_global.runFlag != 1) { // Hack to see if the loop has run yet. If not, initialize score
_global.p1_Score = 0;
_global.p2_Score = 0;
stop;
}
_global.runFlag = 1;
// Initialize Stage and Load BG image from Library
var stageW:Number = 485;
var stageH:Number = 600;
var bg_mc:MovieClip = this.createEmptyMovieClip ("mc", 1);
var myBmp:BitmapData = new BitmapData (stageW, stageH, true, 0×00000000);
myBmp.draw (BitmapData.loadBitmap ("bg_pic"));
bg_mc.attachBitmap (myBmp, 0, "never", false);
//trace(bg_mc.getDepth());
// Sets the Player Avatars & Effects Depth above that of the Background
Player_1_mc.setDepthTo(DepthManager.kTopmost);
Player_2_mc.setDepthTo(DepthManager.kTopmost);
Asplode_mc.setDepthTo(DepthManager.kTopmost);
P1_mc.setDepthTo(DepthManager.kTopmost);
P2_mc.setDepthTo(DepthManager.kTopmost);
race_mc.setDepthTo(DepthManager.kTopmost);
_root.playAgain_btn.setDepthTo(DepthManager.kTopmost);
// Create a Dynamic Text Object for Score Box
_root.createTextField("UI_txt",2,50,20,400,100);
//("objName",depth,x,y,w,h)
// Must create dynamically to use with the Draw API, to set depth
// Create Text Format Object
var my_fmt:TextFormat = new TextFormat();
// Specify paragraph and character formatting.
//txt_fmt.bold = true;
my_fmt.color = 0×888899;
my_fmt.size = 24;
my_fmt.font = "Haettenschweiler";
updateScore(); // Updates the score to show the initial 0/0
//____________________________________________________________________ ___
// Initialize Sounds
// Player 1
p1_SlowSound = new Sound();
p1_SlowSound.attachSound("p1_Slow");
p1_MedSound = new Sound();
p1_MedSound.attachSound("p1_Med");
p1_FastSound = new Sound();
p1_FastSound.attachSound("p1_Fast");
p1_TurnSound = new Sound();
p1_TurnSound.attachSound("p1_Turn");
p1_DeathSound = new Sound();
p1_DeathSound.attachSound("p1_Death");
p1_WinSound = new Sound();
p1_WinSound.attachSound("p1_Win");
// Player 2______________________________
p2_SlowSound = new Sound();
p2_SlowSound.attachSound("p2_Slow");
p2_MedSound = new Sound();
p2_MedSound.attachSound("p2_Med");
p2_FastSound = new Sound();
p2_FastSound.attachSound("p2_Fast");
p2_TurnSound = new Sound();
p2_TurnSound.attachSound("p2_Turn");
p2_DeathSound = new Sound();
p2_DeathSound.attachSound("p2_Death");
p2_WinSound = new Sound();
p2_WinSound.attachSound("p2_Win");
//____________________________________________________________________ ____
//Initialize all variables
gameOver = 0;
topSpeed = 7;
lowSpeed = 2;
// for Player 1
p1_X_Dir = 0;
p1_Y_Dir = 1;
p1_Speed = 2;
p1_X = 243;
p1_Y = 10;
p1_lose = 0;
p1_baseScale = 1;
p1_Wins = 0;
// for Player 2
p2_X_Dir = 0;
p2_Y_Dir = -1;
p2_Speed = 2;
p2_X = 243;
p2_Y = 590;
p2_lose = 0;
p2_baseScale = 1;
p2_Wins = 0;
//____________________________________________________________________ ____
// Main Game Loop
this.onEnterFrame = function() {
if (gameOver == 0) {
// Record Deltas
p1_Old_X_Dir = p1_X_Dir;
p1_Old_X = p1_X;
p1_Old_Y = p1_Y;
p2_Old_X_Dir = p2_X_Dir;
p2_Old_X = p2_X;
p2_Old_Y = p2_Y;
//__________________________________________________________
// Player_1 Keyboard Input
if (Key.isDown(Key.RIGHT)) {
if(p1_X_Dir != -1){
p1_X_Dir = 1;
p1_Y_Dir = 0;
if(p1_Speed < topSpeed){
p1_Speed += .5;
}
}
else {
if(p1_Speed > lowSpeed){
p1_Speed -= .5;
}
}
}
if (Key.isDown(Key.LEFT)) {
if(p1_X_Dir != 1){
p1_X_Dir = -1;
p1_Y_Dir = 0;
if(p1_Speed < topSpeed){
p1_Speed += .5;
}
}
else {
if(p1_Speed > lowSpeed){
p1_Speed -= .5;
}
}
}
if (Key.isDown(Key.UP)) {
if(p1_Y_Dir != 1) {
p1_X_Dir = 0;
p1_Y_Dir = -1;
if(p1_Speed < topSpeed){
p1_Speed += .5
}
}
else {
if(p1_Speed > lowSpeed){
p1_Speed -= .5
}
}
}
if (Key.isDown(Key.DOWN)) {
if(p1_Y_Dir != -1) {
p1_X_Dir = 0;
p1_Y_Dir = 1;
if(p1_Speed < topSpeed){
p1_Speed += .5;
}
}
else {
if(p1_Speed > lowSpeed){
p1_Speed -= .5;
}
}
}
//__________________________________________________________
// Player_2 Keyboard Input
if (Key.isDown(68)) { //D in Ascii
if(p2_X_Dir != -1){
p2_X_Dir = 1;
p2_Y_Dir = 0;
if(p2_Speed < topSpeed){
p2_Speed += .5;
}
}
else {
if(p2_Speed > lowSpeed){
p2_Speed -= .5;
}
}
}
if (Key.isDown(65)) { //A
if(p2_X_Dir != 1){
p2_X_Dir = -1;
p2_Y_Dir = 0;
if(p2_Speed < topSpeed){
p2_Speed += .5;
}
}
else {
if(p2_Speed > lowSpeed){
p2_Speed -= .5;
}
}
}
if (Key.isDown(87)) { //W
if(p2_Y_Dir != 1) {
p2_X_Dir = 0;
p2_Y_Dir = -1;
if(p2_Speed < topSpeed){
p2_Speed += .5
}
}
else {
if(p2_Speed > lowSpeed){
p2_Speed -= .5
}
}
}
if (Key.isDown(83)) { //S
if(p2_Y_Dir != -1) {
p2_X_Dir = 0;
p2_Y_Dir = 1;
if(p2_Speed < topSpeed){
p2_Speed += .5;
}
}
else {
if(p2_Speed > lowSpeed){
p2_Speed -= .5;
}
}
}
//__________________________________________________________
// Update Player 1 Sounds
//Speed Sounds
//trace("Speed="+p1_Speed);
if(p1_Speed <= 3) {
if(p1Slow != 1) {
_root.p1_MedSound.stop();
_root.p1_FastSound.stop();
_root.p1_SlowSound.start(0,9999);
}
p1Slow = 1;
p1Med = 0;
p1Fast = 0;
}
if(p1_Speed > 3 && p1_Speed <= 6) {
if(p1Med != 1) {
_root.p1_SlowSound.stop();
_root.p1_FastSound.stop();
_root.p1_MedSound.start(0,9999);
}
p1Slow = 0;
p1Med = 1;
p1Fast = 0;
}
if(p1_Speed > 6) {
if(p1Fast != 1) {
_root.p1_SlowSound.stop();
_root.p1_MedSound.stop();
_root.p1_FastSound.start(0,9999);
}
p1Slow = 0;
p1Med = 0;
p1Fast = 1;
}
// Turn Sounds
if (p1_Old_X_Dir != p1_X_Dir) {
_root.p1_TurnSound.start(0,1);
//trace("here");
}
//__________________________________________________________
// Update Player 1 Sounds
//Speed Sounds
//trace("Speed="+p1_Speed);
if(p2_Speed <= 3) {
if(p2Slow != 1) {
_root.p2_MedSound.stop();
_root.p2_FastSound.stop();
_root.p2_SlowSound.start(0,9999);
}
p2Slow = 1;
p2Med = 0;
p2Fast = 0;
}
if(p2_Speed > 3 && p2_Speed <= 6) {
if(p2Med != 1) {
_root.p2_SlowSound.stop();
_root.p2_FastSound.stop();
_root.p2_MedSound.start(0,9999);
}
p2Slow = 0;
p2Med = 1;
p2Fast = 0;
}
if(p2_Speed > 6) {
if(p2Fast != 1) {
_root.p2_SlowSound.stop();
_root.p2_MedSound.stop();
_root.p2_FastSound.start(0,9999);
}
p2Slow = 0;
p2Med = 0;
p2Fast = 1;
}
// Turn Sounds
if (p2_Old_X_Dir != p2_X_Dir) {
_root.p2_TurnSound.start(0,1);
//trace("here");
}
//__________________________________________________________
// Update Player_1 Location
p1_X += (p1_Speed * p1_X_Dir);
p1_Y += (p1_Speed * p1_Y_Dir);
//__________________________________________________________
// Update Player_2 Location
p2_X += (p2_Speed * p2_X_Dir);
p2_Y += (p2_Speed * p2_Y_Dir);
//__________________________________________________________
// Place and Scale Player 1
_root.Player_1_mc._x = p1_X;
_root.Player_1_mc._y = p1_Y;
_root.Player_1_mc._xscale = p1_baseScale + (p1_Speed * 23);
_root.Player_1_mc._yscale = p1_baseScale + (p1_Speed * 23);
//__________________________________________________________
// Place and Scale Player 2
_root.Player_2_mc._x = p2_X;
_root.Player_2_mc._y = p2_Y;
_root.Player_2_mc._xscale = p2_baseScale + (p2_Speed * 23);
_root.Player_2_mc._yscale = p2_baseScale + (p2_Speed * 23);
//__________________________________________________________
// Draw the Line for Player 1
if (p1_Y_Dir == -1) {
p1Width = 7;
p1Height = p1_Old_Y - p1_Y + 1 + 3;
topleft_X = p1_X - 3;
topleft_Y = p1_Y;
myRect = new Rectangle(topleft_X,topleft_Y,p1Width,p1Height);
//The first two parameters are the x/y coordinates of the top left-hand corner of the rectangle.
//The last two parameters specify the dimensions of the rectangle (width and height).
myBmp.fillRect(myRect,0xFF00FFFF);
}
if (p1_Y_Dir == 1) {
p1Width = 7;
p1Height = p1_Y - p1_Old_Y + 1 + 3;
topleft_X = p1_X - 3;
topleft_Y = p1_Old_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p1Width,p1Height);
//The first two parameters are the x/y coordinates of the top left-hand corner of the rectangle.
//The last two parameters specify the dimensions of the rectangle (width and height).
myBmp.fillRect(myRect,0xFF00FFFF);
}
if (p1_X_Dir == -1) {
p1Height = 7;
p1Width = p1_Old_X - p1_X + 1 + 3;
topleft_X = p1_X;
topleft_Y = p1_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p1Width,p1Height);
myBmp.fillRect(myRect,0xFF00FFFF);
}
if (p1_X_Dir == 1) {
p1Height = 7;
p1Width = p1_X - p1_Old_X + 1 + 3;
topleft_X = p1_Old_X -3;
topleft_Y = p1_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p1Width,p1Height);
myBmp.fillRect(myRect,0xFF00FFFF)
}
//__________________________________________________________
// Draw the Line for Player 2
if (p2_Y_Dir == -1) {
p2Width = 7;
p2Height = p2_Old_Y - p2_Y + 1 + 3;
topleft_X = p2_X - 3;
topleft_Y = p2_Y;
myRect = new Rectangle(topleft_X,topleft_Y,p2Width,p2Height);
//The first two parameters are the x/y coordinates of the top left-hand corner of the rectangle.
//The last two parameters specify the dimensions of the rectangle (width and height).
myBmp.fillRect(myRect,0xFFFF0000);
}
if (p2_Y_Dir == 1) {
p2Width = 7;
p2Height = p2_Y - p2_Old_Y + 1 + 3;
topleft_X = p2_X - 3;
topleft_Y = p2_Old_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p2Width,p2Height);
//The first two parameters are the x/y coordinates of the top left-hand corner of the rectangle.
//The last two parameters specify the dimensions of the rectangle (width and height).
myBmp.fillRect(myRect,0xFFFF0000);
}
if (p2_X_Dir == -1) {
p2Height = 7;
p2Width = p2_Old_X - p2_X + 1 + 3;
topleft_X = p2_X;
topleft_Y = p2_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p2Width,p2Height);
myBmp.fillRect(myRect,0xFFFF0000);
}
if (p2_X_Dir == 1) {
p2Height = 7;
p2Width = p2_X - p2_Old_X + 1 + 3;
topleft_X = p2_Old_X - 3;
topleft_Y = p2_Y - 3;
myRect = new Rectangle(topleft_X,topleft_Y,p2Width,p2Height);
myBmp.fillRect(myRect,0xFFFF0000)
}
//____________________________________________________________________ ____
//__________________________________________________________
// Player 1 Collision Detection
/*
Ok, a bit of explanation. Through a series of screen grabs, I was
able to determine that at low speed (2), the player avatar = 10px.
At high speed (7), the avatar = 35px. Which means at any given speed,
the size of the avatar = speed*5.
So, the loop devides (the player speed * 5) in half, and then counts
from (-half) the result to (+ half) the result, by twos.
Then, depending which direction the avatar is moving, checks the
appropriate number of pixels to the - and + side of the line.
Well, every other pixel. For performance.
*/
for (loop = -(Math.round((p1_Speed * 5) / 2)); loop <= (Math.round((p1_Speed * 5) / 2)); loop= loop + 2) {
//trace("COL: "+loop);
if(p1_X_Dir != 0){ // Moving along X
yTest = p1_Y + loop;
xTest = p1_X + ((Math.round((p1_Speed * 5) / 2)) * p1_X_Dir); //This offset makes sure we are testing at the front edge of the ball
if (collisionTest (xTest, yTest)) {
gameOver = 1;
_root.Asplode_mc._x = p1_X - 50;
_root.Asplode_mc._y = p1_Y -50;
_root.p1_SlowSound.stop();
_root.p1_FastSound.stop();
_root.p1_MedSound.stop();
_root.p1_DeathSound.start(0,1);
p1_Wins = 0;
}
} else { // Must be moving along Y
xTest = p1_X + loop;
yTest = p1_Y + ((Math.round((p1_Speed * 5) / 2)) * p1_Y_Dir); //This offset makes sure we are testing at the front edge of the ball
if (collisionTest (xTest, yTest)) {
gameOver = 1;
_root.Asplode_mc._x = p1_X - 50;
_root.Asplode_mc._y = p1_Y -50;
_root.p1_SlowSound.stop();
_root.p1_FastSound.stop();
_root.p1_MedSound.stop();
_root.p1_DeathSound.start(0,1);
p1_Wins = 0;
}
}
}
//__________________________________________________________
// Player 2 Collision Detection
for (loop = -(Math.round((p2_Speed * 5) / 2)); loop <= (Math.round((p2_Speed * 5) / 2)); loop= loop + 2) {
//trace("COL: "+loop);
if(p2_X_Dir != 0){ // Moving along X
yTest = p2_Y + loop;
xTest = p2_X + ((Math.round((p2_Speed * 5) / 2)) * p2_X_Dir); //This offset makes sure we are testing at the front edge of the ball
if (collisionTest (xTest, yTest)) {
gameOver = 1;
_root.Asplode_mc._x = p2_X - 50;
_root.Asplode_mc._y = p2_Y -50;
_root.p2_SlowSound.stop();
_root.p2_FastSound.stop();
_root.p2_MedSound.stop();
_root.p2_DeathSound.start(0,1);
p1_Wins = 1;
}
} else { // Must be moving along Y
xTest = p2_X + loop;
yTest = p2_Y + ((Math.round((p2_Speed * 5) / 2)) * p2_Y_Dir); //This offset makes sure we are testing at the front edge of the ball
if (collisionTest (xTest, yTest)) {
gameOver = 1;
_root.Asplode_mc._x = p2_X - 50;
_root.Asplode_mc._y = p2_Y -50;
_root.p2_SlowSound.stop();
_root.p2_FastSound.stop();
_root.p2_MedSound.stop();
_root.p2_DeathSound.start(0,1);
p1_Wins = 1;
}
}
}
// Evaluate the results of the round and update score
if (gameOver == 1) {
_root.playAgain_btn._x = 193;
_root.playAgain_btn._y = 335;
if (p1_Wins == 0) {
_global.p2_Score += 1;
updateScore();
for(i=1;i<=300000;++i){
// delay for sound to finish
}
_root.p2_WinSound.start(0,1);
stop;
} else {
_global.p1_Score += 1;
updateScore();
for(i=1;i<=300000;++i){
// delay for sound to finish
}
_root.p1_WinSound.start(0,1);
}
}
//____________________________________________________________________ ____
} // Closes "if(gameOver == 0)"
} // Closes "OnEnterFrame"
//____________________________________________________________________ ____
function collisionTest (pX, pY) {
pixelColor = myBmp.getPixel(pX, pY);
//trace("0x"+pixelColor.toString(16)); //outputs : 0×00000000
//trace(pixelColor);
if (pixelColor == 65535 || pixelColor == 0) {
return true;
}
if (pixelColor == 16711680) {
return true;
}
return false;
}
function zeroPad (flatNum){
padNum = flatNum;
if (flatNum < 10) padNum = "00"+flatNum;
if (flatNum >= 10 && flatNum < 100) padNum = "0"+flatNum;
return padNum;
}
function updateScore () {
// Update Score
temp1 = zeroPad (_global.p1_Score);
temp2 = zeroPad (_global.p2_Score);
displayScore = "Player 1: " + temp1 + " " + "Player 2: " + temp2;
UI_txt.setNewTextFormat(my_fmt);
UI_txt.text = displayScore;
return;
}
stop();
Pages: 1 2




















