necesito ayuda para flash as3
tengo un prollecto enpezado es el jugo del espace invaders pero tengo algunos problemas kiero sustituir las acciones k se realizan con el teclado por botones
las acciones k kiero cambiar son la de disparar y moverse las kiero poner ke funcionen al tocar botones
aki el codigo
Código PHP:
stop();
// ------- Game Settings -------
var shipSpeed:Number = 7;//speed of ship
var enemyNum:Number = 8;//number of enemies per row !important!
var enemySpace:Number = 40;// space between enemies
var maxLives:int = 3;// max number of lives !based on mLives!
var yIncrement:int = 30;//amount of y (vertical) to increase per edge detect
var bulletSpeed:int = 15;// speed of bullet
var enemySpeed:int = 10;// speed of the enemy moving horizontally
// ------- variables -----------
var enemyArray:Array = new Array();
var trackerArray:Array = new Array();
var timerVal:Number;
var score:Number;
var enemyTimer:Timer;
var spaceShipTimer:Timer;
var spaceShipFlying:Boolean;
var spaceShipObject:Spaceship;
// ---------- initial set up ------------
score = 0;
setLevel();
function setLevel() {
//hide the text field
tWin.text = "";
// hide level up button
bLevelUp.removeEventListener(MouseEvent.CLICK, restartGame);
bLevelUp.visible = false;
bLevelUp.enabled = false;
// hide try again button
bAlive.removeEventListener(MouseEvent.CLICK, restartGame);
bAlive.visible = false;
bAlive.enabled = false;
//-- set up the lives display --
mLives.alpha = 1;
mLives.gotoAndStop(maxLives);
// -- red space ship stuff --
spaceShipTimer = new Timer(12000);
spaceShipTimer.start();
spaceShipTimer.addEventListener(TimerEvent.TIMER, createSpaceShip);
spaceShipObject = new Spaceship();
spaceShipObject.x = -40;
// -- init enemies --
insertEnemyRow(1,(enemyNum*0),0,30);
insertEnemyRow(2,(enemyNum*1),(enemyNum*enemyNum)*-5,60);
insertEnemyRow(3,(enemyNum*2),(enemyNum*enemyNum)*-10,90);
insertEnemyRow(4,(enemyNum*3),(enemyNum*enemyNum)*-15,120);
// -- init invisble tracker --
for (var i:int = 0; i < enemyNum; i++) {
var mTracker:Tracker = new Tracker();
addChild(mTracker);
mTracker.x = i * enemySpace;
mTracker.y += 20;
mTracker.name = "mTracker" + i;
mTracker.visible = false;
trackerArray[i] = mTracker;
}
// -- level check --
if (level == 1) {
tScore.text = score.toString();//needed for first level init
timerVal = 300;
enemyTimer = new Timer(timerVal);
enemyTimer.start();
} else {//if its not level 1
tScore.text = score.toString();
if (timerVal == 100) {// if already at maximum timer
timerVal = 100;
} else {
timerVal -= 50;//timer decreases by 50ms every level up
}
// insert new row of enemy
for (var j:Number = 4; j < (level +3); j++) {
insertEnemyRow((j+1),(enemyNum*j),(enemyNum*enemyNum)*-(j*5),((j+1)*30));
}
}
// -- set up the stage to listen for the keyboard and start moving enemies --
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyRight);
tLevel.text = level.toString();
}
// ------------- initial setup end --------------------
// ---------------- custom functions --------------------
// create red space ship
function createSpaceShip(event:TimerEvent):void {
if (spaceShipFlying == false) {
addChild(spaceShipObject);
spaceShipObject.x = -40;
spaceShipObject.y = 15;
spaceShipFlying = true;
spaceShipObject.addEventListener(Event.ENTER_FRAME, moveSpaceShip);
}
}
// red space ship animate in
function moveSpaceShip(evt:Event):void {
if (spaceShipObject.x >= stage.stageWidth + 20) {
spaceShipFlying = false;
spaceShipObject.removeEventListener(Event.ENTER_FRAME, moveSpaceShip);
} else {
spaceShipObject.x += 5;
}
}
//insert enemy row
function insertEnemyRow(rowNum:int,startVal:int,xPos:int,yPos:int) {
for (var i:int = startVal; i < (enemyNum * rowNum); i++) {
var mEnemy:Enemy = new Enemy();
addChild(mEnemy);
mEnemy.x = xPos + (i * enemySpace);
mEnemy.y += yPos;
mEnemy.name = "mEnemy" + i;
enemyArray[i] = mEnemy;
}
}
//move ship left
function moveLeft(evt:Event):void {
if (mShip.x >= 20) {
mShip.x -= shipSpeed;
}
}
//move ship right
function moveRight(evt:Event):void {
if (mShip.x <= stage.stageWidth - 20 ) {
mShip.x += shipSpeed;
}
}
//handles key downs
function keyDownHandler(evt:KeyboardEvent):void {
if (evt.keyCode == 37) {//left
mShip.addEventListener(Event.ENTER_FRAME, moveLeft);
} else if (evt.keyCode == 39) {//right
mShip.addEventListener(Event.ENTER_FRAME, moveRight);
}
if (evt.keyCode == 32) {//spacebar
var mBullet:Bullet = new Bullet();
addChild(mBullet);
mBullet.x = mShip.x;
mBullet.y = mShip.y;
mBullet.addEventListener(Event.ENTER_FRAME, shoot);
}
function shoot(evt:Event):void {
//remove bullet if over stage
if (mBullet.y >= 0) {
mBullet.y -= bulletSpeed;
} else {
mBullet.removeEventListener(Event.ENTER_FRAME, shoot);
removeChild(mBullet);
}
if (mBullet.hitTestObject(spaceShipObject)) {
score += 100;
tScore.text = score.toString();
mBullet.removeEventListener(Event.ENTER_FRAME,shoot);
spaceShipObject.removeEventListener(Event.ENTER_FRAME, moveSpaceShip);
spaceShipFlying = false;
removeChild(mBullet);
spaceShipObject.x = -40;
}
//for every enemy
for (var i:int = 0; i < enemyArray.length; i++) {
if (mBullet.hitTestObject(enemyArray[i])) {
score += 10;
tScore.text = score.toString();
mBullet.removeEventListener(Event.ENTER_FRAME,shoot);
removeChild(mBullet);
enemyArray[i].parent.removeChild(enemyArray[i]);
enemyArray.splice(i,1);
if (enemyArray.length == 0) {
endGame();
tWin.text = "LEVEL " + level + " COMPLETE!";
level++;
bLevelUp.addEventListener(MouseEvent.CLICK, restartGame);
bLevelUp.enabled = true;
bLevelUp.visible = true;
}
break;
}
}
}
}
function endGame() {
bAlive.addEventListener(MouseEvent.CLICK, restartGame);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.removeEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
mShip.removeEventListener(Event.ENTER_FRAME, moveLeft);
mShip.removeEventListener(Event.ENTER_FRAME, moveRight);
enemyTimer.removeEventListener(TimerEvent.TIMER, enemyRight);
enemyTimer.removeEventListener(TimerEvent.TIMER, enemyLeft);
spaceShipTimer.removeEventListener(TimerEvent.TIMER, createSpaceShip);
spaceShipObject.removeEventListener(Event.ENTER_FRAME, moveSpaceShip);
// if the red space ship is in the air when the game ends remove it
if (spaceShipFlying == true) {
removeChild(spaceShipObject);
spaceShipFlying = false;
}
//clear trackers
for (var q:int = 0; q < enemyNum; q++) {
removeChild(trackerArray[q]);
trackerArray[q].pop;
}
// clear enemies
for (var w:int = 0; w < enemyArray.length; w++) {
removeChild(enemyArray[w]);
enemyArray[w].pop;
}
}
function enemyLeft(event:TimerEvent):void {
for (var i:int = 0; i < trackerArray.length; i++) {
trackerArray[i].x -= enemySpeed;
}
for (var j:int = 0; j < enemyArray.length; j++) {
enemyArray[j].x -= enemySpeed;
if (mLimit.hitTestObject(enemyArray[j])) {
endGame();
maxLives--;
bAlive.enabled = true;
bAlive.visible = true;
trace(maxLives);
if (maxLives == 0) {
mLives.alpha = 0;
gotoAndStop("lose");
} else {
mLives.gotoAndStop(maxLives);
}
break;
}
if (trackerArray[0].x <= 10) {
enemyArray[j].y += yIncrement;
enemyTimer.removeEventListener(TimerEvent.TIMER, enemyLeft);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyRight);
}
}
}
function enemyRight(event:TimerEvent):void {
for (var i:int = 0; i < trackerArray.length; i++) {
trackerArray[i].x += enemySpeed;
}
for (var j:int = 0; j < enemyArray.length; j++) {
enemyArray[j].x += enemySpeed;
if (mLimit.hitTestObject(enemyArray[j])) {
endGame();
maxLives--;
bAlive.enabled = true;
bAlive.visible = true;
trace(maxLives);
if (maxLives == 0) {
mLives.alpha = 0;
gotoAndStop("lose");
} else {
mLives.gotoAndStop(maxLives);
}
break;
}
// if tracker reach end of stage
if (trackerArray[7].x >= (stage.stageWidth - 10)) {
enemyArray[j].y += yIncrement;
enemyTimer.removeEventListener(TimerEvent.TIMER, enemyRight);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyLeft);
}
}
}
function keyUpHandler(evt:KeyboardEvent):void {
if (evt.keyCode == 37) {//left
mShip.removeEventListener(Event.ENTER_FRAME, moveLeft);
} else if (evt.keyCode == 39) {//right
mShip.removeEventListener(Event.ENTER_FRAME, moveRight);
}
if (evt.keyCode == 32) {
//fireTimer.removeEventListener(TimerEvent.TIMER, fireBullet);
}
}
function restartGame(evt:Event):void {
if (maxLives == 0) {
gotoAndStop("start");
} else {
setLevel();
}
}
porfavor lo necesito