ActionScript - No puedo crear un ciclo for en actionscript

   
Vista:

No puedo crear un ciclo for en actionscript

Publicado por Cecilio Lanz Vallejo (1 intervención) el 12/11/2011 03:45:26
Hola!
Estoy creando un videojuego en Flash, que consiste en un videojuego de dos jugadores donde se matan el uno al otro, donde salen dos naves y se disparan entre si, el problema es, que no puede disparar bien, según yo, el problema es un ciclo for del programa, porque si aparece la bala, pero esta no se mueve, solo aparece.¿ Alguien que sepa me podria ayudar? Aqui les va el codigo completo de mi juego:


package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.utils.getTimer;
import flash.utils.Timer;
import flash.geom.Point;

public class ShipSim extends Sprite
{
private var ship:nave;
private var ship2:nave2;
private var vr:Number = 0;
private var thrust:Number = 0;
private var vr2:Number = 0;
private var thrust2:Number = 0;
private var vx:Number = 0;
private var vy:Number = 0;
private var vx2:Number = 0;
private var vy2:Number = 0;
private var friction:Number = 0.97;
private var friction2:Number = 0.97;
private var missileSpeed:Number = .2;
private var shipRadius:Number = 20;
private var missiles:Array;
private var delayTimer:Timer;
private var shieldTimer:Timer;
private var gameMode:String;
private var shieldOn:Boolean;
private var shipsLeft:uint;
private var shieldsLeft:uint;
private var shipIcons:Array;
private var shieldIcons:Array;
private var gameObjects:Sprite;
private var scoreObjects:Sprite;
private var lastTime:uint;


public function ShipSim()
{
init();
gameObjects = new Sprite();
addChild(gameObjects);
missiles=new Array();
}

private function init():void
{
//declarar posición de la nave
ship = new nave();
ship2 = new nave2();
addChild(ship);
addChild(ship2);
ship.x = stage.stageWidth / 4;
ship.y = stage.stageHeight / 2;
ship2.x = stage.stageWidth / 1.5;
ship2.y = stage.stageHeight / 2;
addEventListener(Event.ENTER_FRAME, MoverNave);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyDown2);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp2);

}


private function KeyDown(event:KeyboardEvent):void
{
switch(event.keyCode)
{
//quitar flama y frenar
case Keyboard.LEFT :
vr = -5;
break;

case Keyboard.RIGHT :
vr = 5;
break;

case Keyboard.UP :
thrust = 0.2;
ship.flame.visible=true;
break;

case Keyboard.SPACE :
newMissile();
break;

default :
break;
}
}
private function KeyDown2(event:KeyboardEvent):void
{
switch(event.keyCode)
{
//quitar flama y frenar

case Keyboard.A :
vr2 = -5;
break;

case Keyboard.D :
vr2 = 5;
break;

case Keyboard.W :
thrust2 = 0.2;
ship2.flame.visible=true;
break;

default :
break;
}
}

private function KeyUp(event:KeyboardEvent):void
{
//mostrar flama cuando se pulsa arriba
vr = 0;
thrust = 0;
ship.flame.visible=false;
}

private function KeyUp2(event:KeyboardEvent):void
{
//mostrar flama cuando se pulsa arriba
vr2 = 0;
thrust2 = 0;
ship2.flame.visible=false;
}


private function MoverNave(event:Event):void
{
//movimiento de la nave
ship.rotation += vr;
ship2.rotation += vr2;
var angle:Number = ship.rotation * Math.PI / 180;
var angle2:Number = ship2.rotation * Math.PI / 180;
var ax:Number = Math.cos(angle) * thrust;
var ay:Number = Math.sin(angle) * thrust;
var ax2:Number = Math.cos(angle2) * thrust2;
var ay2:Number = Math.sin(angle2) * thrust2;
vx += ax;
vx2 -= ax2
vy += ay;
vy2 -= ay2;
vx *= friction;
vy *= friction;
vx2 *= friction2;
vy2 *= friction2;
ship.x += vx;
ship.y += vy;
ship2.x += vx2;
ship2.y += vy2;
var left:Number = 0;
var left2:Number = 0;
var right:Number = stage.stageWidth;
var right2:Number = stage.stageWidth;
var top:Number = 0;
var top2:Number = 0;
var bottom:Number = stage.stageHeight;
var bottom2:Number = stage.stageHeight;
if (ship.x - ship.width / 2 > right)
{
ship.x = left - ship.width / 2;
}
else if (ship.x + ship.width / 2 < left)
{
ship.x = right + ship.width / 2;
}
if (ship.y - ship.height / 2 > bottom)
{
ship.y = top - ship.height / 2;
}
else if (ship.y < top - ship.height / 2)
{
ship.y = bottom + ship.height / 2;
}
//segundo
if (ship2.x - ship2.width / 2 > right2)
{
ship2.x = left2 - ship2.width / 2;
}
else if (ship2.x + ship2.width / 2 < left2)
{
ship2.x = right2 + ship2.width / 2;
}
if (ship2.y - ship2.height / 2 > bottom2)
{
ship2.y = top2 - ship2.height / 2;
}
else if (ship2.y < top2 - ship2.height / 2)
{
ship2.y = bottom2 + ship2.height / 2;
}
}

public function newMissile() {
// crear misiles
var newMissile:Missile = new Missile();

// dirigir misiles
newMissile.dx = Math.cos(Math.PI*ship.rotation/180);
newMissile.dy = Math.sin(Math.PI*ship.rotation/180);

// Posicion
newMissile.x = ship.x + newMissile.dx*shipRadius;
newMissile.y = ship.y + newMissile.dy*shipRadius;

// añadir al juego
gameObjects.addChild(newMissile);
missiles.push(newMissile);
}
// animar misiles
public function moveMissiles(timeDiff:uint) {
for(var i:int=missiles.length+1;i>=0;i++) {
// mover
missiles[i].x += missiles[i].dx*missileSpeed*timeDiff;
missiles[i].y += missiles[i].dy*missileSpeed*timeDiff;
// si misiles llegan al final de la pantalla
if ((missiles[i].x < 0) || (missiles[i].x > 550) || (missiles[i].y < 0) || (missiles[i].y > 400)) {
gameObjects.removeChild(missiles[i]);
delete missiles[i];
missiles.splice(i,1);
}
}
}

public function missileHit(missileNum:uint) {
gameObjects.removeChild(missiles[missileNum]);
missiles.splice(missileNum,1);
}
}
}
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder