• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Shoot in different directions

New Here ,
Sep 15, 2018 Sep 15, 2018

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;

     }

  }

}

TOPICS
ActionScript

Views

450

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2018 Sep 15, 2018

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]

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

Thank you for your answer, but i can't figure out what should go in that IF statement

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

how did you define destra and sinistra?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2018 Sep 15, 2018

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;

     }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

use:

if(destra){ 

bullo.dir=destra

} else {

bullo.dir=sinistra

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

Works like before. The problem is that each bullo follows the character direction

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

show your mainLoop function

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2018 Sep 15, 2018

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;

          }

      }

}

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Sep 15, 2018 Sep 15, 2018

Copy link to clipboard

Copied

Nevermind, I worked that out. Thank you for your help!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Sep 16, 2018 Sep 16, 2018

Copy link to clipboard

Copied

LATEST

you're welcome.

(p.s when using the adobe forums, please mark helpful/correct responses, if there are any.)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines