Help Converting simple game from AS2 to AS3
I need help updating a previous game project from AS2 to AS3.
I have never actually used AS3 (have not used flash in many years). If someone could convert this code and explain a little I would be very grateful.
Even if someone could convert the could Im sure I will understand the changes by looking.
// Params
var bulletSpeed = 25;
var bulletSpread = 10;
var playerHealth = 25;
var enemyGeneration = 30;
var enemyMaxSpeed = 9;
var enemyMinSpeed = 1;
var enemyHealth = 10;
// Vars
var shots = [];
var enemies = [];
var kills = 0;
var RADIANS = Math.PI / 180;
var DEGREES = 180 / Math.PI;
// Init
player.health = playerHealth;
updateScore();
// Start game
onEnterFrame = game;
// Main game loop
function game(){
updatePlayer();
updateShots();
updateEnemies();
}
// Update the player
function updatePlayer(){
// Player mouse aim
player._rotation = Math.atan2(_ymouse - player._y, _xmouse - player._x) * DEGREES;
// Player shooting
if(shooting){
shoot(player, player._rotation - (bulletSpread / 2) + random(bulletSpread), bulletSpeed);
}
// Update health bar with easing
var percent = player.health / playerHealth;
healthbar._xscale += ((percent * 100) - healthbar._xscale) * .5;
var frame = 1 + int((healthbar._totalframes - 1) * percent);
healthbar.gotoAndStop(frame);
// Check if player died
if(player.health <= 0){
healthbar._width = 0;
onEnterFrame = null;
// Game Over!
this.createTextField("diedText", getNextHighestDepth(), 0, Stage.height / 2 - 25, Stage.width, 76);
diedText.text = "GAME OVER.";
var tf = new TextFormat();
tf.color = 0xff0000;
tf.size = 50;
tf.font = "Arial";
tf.bold = true;
tf.align = "center";
diedText.setTextFormat(tf);
diedText.selectable = false;
}
}
// Player mouse click tracking
function onMouseDown(){
shooting = true;
}
function onMouseUp(){
shooting = false;
}
// Create a new bullet
function shoot(origin, angle, velocity){
var init = {
_x:origin._x,
_y:origin._y,
_rotation:angle,
xSpeed:velocity * Math.cos(angle * RADIANS),
ySpeed:velocity * Math.sin(angle * RADIANS)
}
var d = getNextHighestDepth();
var shot = attachMovie("bullet", "bullet" + d, d, init);
shots.push(shot);
}
// Update all the shots
function updateShots(){
var i = shots.length, shot;
while(i--){
shot = shots;
// Move the shot
shot._x += shot.xSpeed;
shot._y += shot.ySpeed;
// Check if shot went out of bounds
if(shot._x < 0 || shot._x > Stage.width
|| shot._y < 0 || shot._y > Stage.height){
shots.splice(i, 1);
shot.removeMovieClip();
continue;
}
// Check if shot hit an enemy
var e = enemies.length, enemy;
while(e--){
enemy = enemies
if(enemy.hitTest(shot)){
enemy.health--;
shots.splice(i, 1);
shot.gotoAndPlay("die");
break;
}
}
}
}
// Update and generate enemies
function updateEnemies(){
// Randomly generate new enemies along edge of screen
if(random(enemyGeneration) == 0){
var d = getNextHighestDepth();
var enemy = attachMovie("enemy", "enemy" + d, d);
if(Math.random() < .5){
enemy._x = random(Stage.width);
enemy._y = Stage.height * random(2);
}else{
enemy._x = Stage.width * random(2);
enemy._y = random(Stage.height);
}
var angle = Math.atan2(player._y - enemy._y, player._x - enemy._x);
var speed = enemyMinSpeed + random(enemyMaxSpeed - enemyMinSpeed);
enemy.xSpeed = Math.cos(angle) * speed;
enemy.ySpeed = Math.sin(angle) * speed;
enemy.health = enemyHealth;
enemies.push(enemy);
}
// Move all enemies
var i = enemies.length, enemy;
while(i--){
enemy = enemies;
enemy._x += enemy.xSpeed;
enemy._y += enemy.ySpeed;
// Check if enemy has been killed
if(enemy.health <= 0){
enemies.splice(i, 1);
enemy.gotoAndPlay("die");
kills++;
updateScore();
continue;
}
// Check if enemy reached player
if(enemy.hitTest(player)){
enemies.splice(i, 1);
enemy.removeMovieClip();
player.health--;
}
}
}
// Update the scoreboard
function updateScore(){
score_txt.text = "Kills: " + kills;
updateHighscore();
}
// Update the high score
function updateHighscore(){
var so = SharedObject.getLocal("player");
if(!so.data.highscore)
so.data.highscore = 0;
if(kills > so.data.highscore)
so.data.highscore = kills;
highscore_txt.text = "High Score: " + so.data.highscore;
}