Copy link to clipboard
Copied
I don't know how to shoot the bullets in different directions. Every time I shoot the bullet follow the direction of the player, but if it turns the bullet stop and turn itself, following the character direction, and when it is still, the bullet just appear but does not move.
Sinistra is the boolean to know if the character is going left, Destra is the identifier for the right.
var Bulli:Array = new Array();
var ShootTimer:Timer = new Timer(0);
stage.addEventListener(MouseEvent.MOUSE_DOWN, startShootTimer);
stage.addEventListener(MouseEvent.MOUSE_UP, stopShootTimer);
ShootTimer.addEventListener(TimerEvent.TIMER, shootBullo);
stage.addEventListener(Event.ENTER_FRAME, mainLoop);
function startShootTimer(e:MouseEvent):void
{
ShootTimer.start();
}
function stopShootTimer(e:MouseEvent):void
{
ShootTimer.stop();
}
function shootBullo(e:TimerEvent):void
{
var bullo:Bullo = new Bullo();
bullo.x = Jumbo.x;
bullo.y = Jumbo.y - 50;
addChild(bullo);
Bulli.push(bullo);
}
function mainLoop (e:Event):void
{
for (var b:int = 0; b < Bulli.length; b++)
{
if (destra == true)
{
Bulli.x += 10;
}
if (sinistra == true)
{
Bulli.x -= 10;
}
}
}
Copy link to clipboard
Copied
add a direction property to each bullo:
function shootBullo(e:TimerEvent):void {
var bullo:Bullo = new Bullo();
bullo.x = Jumbo.x;
bullo.y = Jumbo.y - 50;
if(something){ // use the appropriate criteria
bullo.dir=destra
} else {
bullo.dir=sinistra
}
addChild(bullo);
Bulli.push(bullo);
}
function mainLoop (e:Event):void {
for (var b:int = 0; b < Bulli.length; b++) {
if (Bulli.dir==destra) {
Bulli.x += 10;
} else {
Bulli.x -= 10;
}
}
}
p.s. you should also check when your bullo's are off-stage
and can be removed from the display list and Bulli.
and you should reverse-loop through Bulli
[moved from Adobe Animate CC to ActionScript 3]
Copy link to clipboard
Copied
Thank you for your answer, but i can't figure out what should go in that IF statement
Copy link to clipboard
Copied
how did you define destra and sinistra?
Copy link to clipboard
Copied
var destra: Boolean = false;
var sinistra: Boolean = false;
var v = 5;
function muovi(event:Event)
{
if(destra == true)
{
Jumbo.x += v;
Jumbo.gotoAndStop(3);
}
if (sinistra == true)
{
Jumbo.x -= v;
Jumbo.gotoAndStop(4);
}
}
function premi(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
destra = true;
break;
}
case Keyboard.LEFT:
{
sinistra = true;
break;
}
}
function rilascia(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.RIGHT:
{
destra = false;
break;
}
case Keyboard.LEFT:
{
sinistra = false;
break;
}
}
Copy link to clipboard
Copied
use:
if(destra){
bullo.dir=destra
} else {
bullo.dir=sinistra
}
Copy link to clipboard
Copied
Works like before. The problem is that each bullo follows the character direction
Copy link to clipboard
Copied
show your mainLoop function
Copy link to clipboard
Copied
Now it shoots correctly, but if the character goes left and then right all the bullo's change direction.
function mainLoop (e:Event):void
{
for (var b:int = 0; b < Bulli.length; b++)
{
if (Bulli.dir == destra)
{
Bulli.x += 10;
}
else
{
Bulli.x -= 10;
}
}
}
Copy link to clipboard
Copied
Nevermind, I worked that out. Thank you for your help!
Copy link to clipboard
Copied
you're welcome.
(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)