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
Imágen de perfil de Alejandro

Solución para el problema de disparo en un videojuego de dos jugadores

Publicado por Alejandro (369 intervenciones) el 27/06/2023 20:29:02
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
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 las naves
            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;
            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;
            ship.flame.visible = false;
            ship2.flame.visible = false;
            addEventListener(Event.ENTER_FRAME, update);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler2);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler2);
        }
 
        private function keyDownHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                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(ship);
                    break;
 
                default:
                    break;
            }
        }
 
        private function keyUpHandler(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.LEFT:
                case Keyboard.RIGHT:
                    vr = 0;
                    break;
 
                case Keyboard.UP:
                    thrust = 0;
                    ship.flame.visible = false;
                    break;
 
                default:
                    break;
            }
        }
 
        private function keyDownHandler2(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.A
 
:
                    vr2 = -5;
                    break;
 
                case Keyboard.D:
                    vr2 = 5;
                    break;
 
                case Keyboard.W:
                    thrust2 = 0.2;
                    ship2.flame.visible = true;
                    break;
 
                case Keyboard.SHIFT:
                    newMissile(ship2);
                    break;
 
                default:
                    break;
            }
        }
 
        private function keyUpHandler2(event:KeyboardEvent):void
        {
            switch (event.keyCode)
            {
                case Keyboard.A:
                case Keyboard.D:
                    vr2 = 0;
                    break;
 
                case Keyboard.W:
                    thrust2 = 0;
                    ship2.flame.visible = false;
                    break;
 
                default:
                    break;
            }
        }
 
        private function update(event:Event):void
        {
            moveShip(ship, vr, thrust);
            moveShip(ship2, vr2, thrust2);
            moveMissiles();
        }
 
        private function moveShip(ship:Sprite, vr:Number, thrust:Number):void
        {
            ship.rotation += vr;
            var angle:Number = ship.rotation * Math.PI / 180;
            var ax:Number = Math.cos(angle) * thrust;
            var ay:Number = Math.sin(angle) * thrust;
            vx += ax;
            vy += ay;
            vx *= friction;
            vy *= friction;
            ship.x += vx;
            ship.y += vy;
            checkBounds(ship);
        }
 
        private function checkBounds(ship:Sprite):void
        {
            var left:Number = 0;
            var right:Number = stage.stageWidth;
            var top:Number = 0;
            var bottom: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;
            }
        }
 
        public function newMissile(ship:Sprite):void
        {
            var newMissile:Missile = new Missile();
            newMissile.rotation = ship.rotation;
            var angle:Number = newMissile.rotation * Math.PI / 180;
            newMissile.dx = Math.cos(angle) * missileSpeed;
            newMissile.dy = Math.sin(angle) * missileSpeed;
            newMissile.x = ship.x + Math.cos(angle) * shipRadius;
            newMissile.y = ship.y + Math.sin(angle) * shipRadius;
            gameObjects.addChild(newMissile);
            missiles.push(newMissile);
        }
 
        public function moveMissiles():void
        {
            for (var i:int = missiles.length - 1; i >= 0; i--)
            {
                var missile:Missile = missiles[i];
                missile.x += missile.dx;
                missile.y += missile.dy;
 
                if (missile.x < 0 || missile.x > stage.stageWidth || missile.y < 0 || missile.y > stage.stageHeight)
                {
                    gameObjects.removeChild(missile);
                    missiles.splice(i, 1);
                }
            }
        }
    }
}

Ten en cuenta que en el código anterior se asume que tienes una clase `Missile` para representar los misiles en el juego. Asegúrate de tener definida esa clase en tu código o modifica el código según tus necesidades.

Espero que esto solucione tu problema de disparo en tu videojuego de dos jugadores en Flash. ¡Buena suerte con tu juego!
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar