actionscript para animar una nave (juego flash)

Creado a las // comentar

/*
The Ship class is linked to a ship movie clip on stage.
The Ship class is the largest class, because we will use the ship class in this game to drive most of the game logic.
*/

class Ship extends MovieClip
{
//We want to create several varibles to keep track of important game information.
//We must define them here

//This will keep track of the ship's speed
var velocity;
//This will create a buffer between shooting missiles.
var shootLimiter;
//This variable will keep track of how often to create enemies
var enemyTimer;
//This variable will keep track of how oftern to create a miniboss
var miniBossTimer;
//This variable will keep track of how often to create a powerup
var powerUpTimer;
//This variable will keep track of how many minibosses before the boss
var bossCountdown;
//This variable will be an array and will contain a list of all enemies on stage.
var enemies;
//This will keep track of the ship's current score.
var score;
//This will store the final score of a game after bonuses have been tallied.
var finalScore;
//This variable keeps track of the ship's current health
var health;

//This will keep track of how many kills the ship has made
var kills;
//This will keep track of how many times a ship missile fails to hit a target before flying off screen.
var misses;
//This will keep track of whether the ship is currently shaking from taking damage
var shaking;
//this will store how many frames the ship should shake for when it takes damage
var shakeDuration;
//this variable is actually a reference to the shield movie clip that is nested inside the ship movie clip.
var shield:MovieClip;


//This onLoad function is a built-in function of every movie clip.
//When the ship is first loaded onto stage (which will be when the swf is launched because the ship movie clip is sitting on stage)
//we want to do several things:
function onLoad()
{
//this is how you connect to the the kongregate servers to use the stats and highscore api!
_root.kongregateServices.connect();

//Let's create a sound object on the root timeline.
_root.soundFX = new Sound();

//Let's play a big explosion sound when the game first launches. The "very_big_explosion.wav" is the linakge identifier of that sound in the fla library
_root.soundFX.attachSound("very_big_explosion.wav");
_root.soundFX.start();

//Hide the ship graphic.
_visible = false;

//Hide the game over menu.
_root.gameOverMenu._visible = false;
//Hide the ship's health meter.
_root.healthMeter._visible = false;
//Hide the enemy health meter
_root.enemyHealthMeter._visible = false;

//Attach some logic to the PLAY button that is nested in the play menu.
_root.playMenu.playButton.onPress = function()
{
//When the PLAY button is pressed, call the ship's newGame() function
_root.ship.newGame();
}

}

//When ever we want to start a new game, we need to call this function which will do several things
function newGame()
{
//When a new game is started, hide the play menu, the game over menu, and the enemy health meter
//The enemy health meter will only be diplayd during miniboss and boss battles
_root.playMenu._visible = false;
_root.gameOverMenu._visible = false;
_root.enemyHealthMeter._visible = false;

//reset the values of all these varibles to zero for the new game.
kills = 0;
misses = 0;
velocity = 10;
shootLimiter = 0;
powerUpTimer = 0;
enemyTimer = 0;
miniBossTimer = 0;

//reset the Boss Countdown variable to whatever you want it to be.
//Right now it's set to trigger te boss after 2 minibosses have been defeated
bossCountdown = 2;

//Reset the enemies array. this will clear the list of enemies and start a new list which will be empty at first.
enemies = [];

//This will reset the score variable to zero and update the display to reflect this change
score = 0;
_root.scoreText.text = score;

//This will reset the health variable to 100, show the health meter, and update the health meter to reflect this change
health = 100;
_root.healthMeter._visible = true;
_root.healthMeter.bar._xscale = 100;

//Center the ship on stage (our game is 600 pixels wide and 300 tall)
_x = 300;
_y = 150;

//unhide the ship graphic now
_visible = true;
//But hide the ship's shield graphic until a shield powerup is captured.
shield._visible = false;
//Set the shaking variable to false. It will get set to true when the ship gets hit and takes damage.
shaking = false;
//Set a duratin that we want shakes to last. 10 frames is about 1/3 of a second.
shakeDuration = 10;
//Make sure the ship is not stuck in a shaking rotation from the previous game.
_rotation = 0;
}

//This onEnterFrame function is a built-in function of every movie clip.
//All the code that we need to continuously execute at 30 frames a second goes inside this function
function onEnterFrame()
{
//We only want to execute logic at frame rate if the ship is visible (the game is being played right now)
//We don't want to do all this logic if we are on the play menu or the game over menu, etc...
if(_visible == true)
{

/*
--- MOVEMENT LOGIC ---
Check to see if arrow keys are being pressed, and if the ship is not heading off screen, and update it's position
basd on its speed. For example: if the LEFT arrow key is being pressed AND ( && ) the ships _x position is greater than
40, move the ship left 10 pixels (we set our velocity to 10 in the newGame() function).
*/
if( Key.isDown(Key.LEFT) && _x > 40){ _x -= velocity; }
if( Key.isDown(Key.RIGHT) && _x < 560){ _x += velocity; } if( Key.isDown(Key.UP) && _y > 20){ _y -= velocity; }
if( Key.isDown(Key.DOWN) && _y < 280){ _y += velocity; } //--- SHOOTING LOGIC: --- //Increment the shootLimiter (it gets set to zero in the newGame() function) shootLimiter += 1; //Then check to see if the SPACE BAR is being pressed AND ( && ) if the shootLimiter has counted to at least eight //NOTE: you can change the 8 to any number to modify the rate at which the ship can shoot if( Key.isDown(Key.SPACE) && shootLimiter > 8)
{
//If so, attach a Missile to stage (See Missile class).
var missile = _root.attachMovie("Missile","Missile" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
//Set the missile's position to that of this ship's, but offset it a little so it comes from the nose of the ship, or the cannon, etc...
missile._x = _x + 50;
missile._y = _y + 2;

//Lastly, set the shootLimiter back to zero. It will start counting back up at framerate, and in 8 frames, you can fire again
shootLimiter = 0;

}

//--- SHAKING LOGIC: ---
//If the ship is currently shakig from taking damage, call it's shake() function
if(shaking == true){ shake(); }


//--- SHIELD LOGIC: ---
//If the shield is currenlty active (from getting a shield powerup)
if(shield._visible == true)
{
//Slowly fade it out.
shield._alpha-= .5;
//If the shield's alpha reaches zero (if it completely transparent)
if(shield._alpha < 0) { //hide it, tunr it's visibility off shield._visible = false; //but crank it's alpha all the way back up so it'll be ready to fade our again next time we unhide it shield._alpha = 100; } } //--- ENEMY SHIP TIMER LOGIC: --- //Incrment the enemy timer. enemyTimer += 1; //If it reaches 30 (30 frames, or one second) if(enemyTimer > 30)
{
//reset it back to zero so it'll start counting back up to 30 again.
enemyTimer = 0;
//Attach a new enemy ship to the stage (See EnemyShip class)
_root.attachMovie("EnemyShip", "EnemyShip" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
}

//--- POWERUP TIMER LOGIC: ---
//Increment the powerup timer by one
powerUpTimer += 1;
//If it reaches 30 * 10 (30 frames x 10 seconds, 300 frames or 10 seconds)
if(powerUpTimer > 30 * 10)
{
//reset it back to zero so it'll start counting back up to 300 again.
powerUpTimer = 0;
//Attach a new PowerUp to the stage (see PowerUp class)
_root.attachMovie("PowerUp", "PowerUp" + _root.getNextHighestDepth(), _root.getNextHighestDepth());
}

//--- MINIBOSS TIMER LOGIC: ---
//Increment the miniboss timer by one
miniBossTimer += 1;
//If it reaches 30 * 30 (30 frames x 30 seconds, 900 frames or 30 seconds)
if(miniBossTimer == (30 * 30))
{
//if there is not already a miniboss on stage
if(!_root.miniBoss)
{
//if our boss countdown timer has reached zero
if( bossCountdown == 0)
{
//Attach a Boss to stage (see Boss class)
_root.attachMovie("Boss", "boss", _root.getNextHighestDepth());
}
//if our boss countdown timer has not reached zero yet
else
{
//Attach a miniboss to stage (see MiniBoss class)
_root.attachMovie("MiniBoss", "miniBoss", _root.getNextHighestDepth());
//then count down the Boss timer one miniboss closer to the boss
bossCountdown -= 1;
}
}
}
}

//we are now done with all of our onEnterFrame() logic
}

// Here are a few functions we made to do extra stuff.

//We can call this function from any other class like this: _root.ship.updateScore(10); which will add 10 to the ship's score.
function updateScore(points)
{
//Add "points" which is som value passed into this function to the ship's score variable
score += points;
//update the score display on stage to match the score variable
_root.scoreText.text = score;
}

//We can call this function from any other class like this: _root.ship.updateHealth(-10); which would subtract 10 from the ship's health.
function updateHealth(points)
{
//Only update the ship's health if it is till alive (if it's visible).
if(_visible == true)
{
//Update the health by the value "points" which gets passed into this function. It cn be a negative value (hit by missile)
//or it could be a positive value (collected a health powerup)
health += points;
//Adjust the helath meter's scale to represent the percent health of the ship.
_root.healthMeter.bar._xscale = health;

//Add an explosion sound fx. we create the _root.soundFX sound object in the Ship class
_root.soundFX.attachSound("small_explosion.wav");
_root.soundFX.start();

//IF the ship's health is now zero or less
if(health <= 0)
{
//force it to exactly zero
health = 0;
//update the health bar
_root.healthMeter.bar._xscale = health;
//call the ship's explode function
explode();
}
}
}

//We call this function from the updateHealth() function when the ship's health reaches zero
function explode()
{
//Attach an explosion movie clip to the stage
var explosion = _root.attachMovie("Explosion","Explosion" + _root.getNextHighestDepth(),_root.getNextHighestDepth());
//Position the explosion to the position of the ship.
explosion._x = _x;
explosion._y = _y;
//turn the ship's visiblity off, hide the ship. It'll look like it exploded.
this._visible = false;

//Add an explosion sound fx. we create the _root.soundFX sound object in the Ship class
_root.soundFX.attachSound("very_big_explosion.wav");
_root.soundFX.start();

//Once the ship is dead, get rid of all enemies on stage
destroyAllEnemies();
//Then call this game over function
gameOver();
}

//This function will iterate through every enemy in the enemies array and destroy it!
//We call this at the end of the game to clear the screen of enemies.
function destroyAllEnemies()
{
//cycle through every index of the enemies array
for(var i = 0; i< enemies.length; i++)
{
//destroy the enemy at this index
enemies[i].explode();
}
}

//This function will iterate through every enemy in the enemies array and damage it!
//We call this when a nuke poweup is collected. Regulare enemies will just explode, but minibosses and bosses will
//just take damage.
function damageAllEnemies()
{
//Cycle through every index of the enemies array.
for(var i = 0; i< enemies.length; i++)
{
//Call the takeDamage() function of the enemy at this index in the list.
enemies[i].takeDamage();
}
}

//This function gets called from the Boss class when the Boss is destroyed. Kill the boss to win the game.
function winGame()
{

//this is how you submit a stat to the Kongregate api to flag that the game has been WON!
_root.kongregateStats.submit("GameWon", 1); // The user collected a coin

//Play the "MISSION COMPLETED" banner. It will move across the screen.
_root.missionCompletedBanner.gotoAndPlay(2);
//Turn the visibility of the ship off.
this._visible = false;
//Remove all enemies from the stage.
destroyAllEnemies();
//Then do the normal game over logic by calling the gamOver() function.
gameOver();
}

//This function gets called from th explode() function and also form the winGame() function.
function gameOver()
{
//unhide the game over menu
_root.gameOverMenu._visible=true;
//attach some logic to the PLAY AGAIN button
_root.gameOverMenu.playAgainButton.onPress = function()
{
//when the PLAY AGAIN button is pressed, call the ship's newGame() function.
_root.ship.newGame();
}

//display the kills bonus
_root.gameOverMenu.killBonus.text = kills + " x " + 100;
//display the hit percent bonus
_root.gameOverMenu.hitBonus.text = Math.floor((kills/(misses + kills))*100) + " x " + 100;

//now calculate the final score:
//score + (kills x 100) + (hit percent x 100)
finalScore = score + (kills * 100) + (Math.floor((kills/(misses + kills))*100)*100);
//then display the final score
_root.gameOverMenu.finalScore.text = finalScore;

//this is how you submit a score to the kongregate api when the game is over!
_root.kongregateScores.submit(finalScore);

//this is how you submit a stat to the Kongregate api
_root.kongregateStats.submit("Kills", kills); // The user collected a coin

}

//This function is called form the EnemyMissile class when a missile hits the ship
function initShake()
{
//we set the shaking varible to true, so that the onEnterFrame() function knows to fire the shake() function
shaking = true;
//we want the ship to shake for ten frames
shakeDuration = 10;
//we want the ship to shake 5 rotational degrees each way
_rotation = 5;
}

//This function is called from the onEnterFrame() function whenever the shaking varible is set to true.
function shake()
{
//Reverse the direction of the shake rotation.
_rotation *= -1;
//Countdown the duration of the shake from 10 to zero.
shakeDuration -= 1;
//If the duration reaches zero
if(shakeDuration == 0)
{
//set the shaking variable to false so the ship no longer calls the shake() function in the onEnterFrame() function
shaking = false;
//Make sure the ship's rotation get's set back to it's default, zero
_rotation = 0;
}
}
}

0 comentarios: