7 Replies Latest reply on Jan 16, 2013 7:05 AM by kglad

    as2 predicted trajectory

    pa-pavel Level 1

      Hi,

       

      I found and managed to adapt this code:

       

      shoot(); function shoot() {     bulletSpeed = 6;     bullet.gravity = 0.3;     xd = trgt._x-gun._x;     yd = trgt._y-gun._y;     dist = Math.sqrt(xd*xd+yd*yd);     flight = Math.ceil(dist/bulletSpeed);     bullet._x = gun._x;     bullet._y = gun._y;     bullet.xStep = xd/flight;     bullet.yStep = yd/flight;     bullet.yStep -= flight*bullet.gravity/2;     bullet._visible = true; } this.createEmptyMovieClip("lineobj", getNextHighestDepth()); lineobj.lineStyle(5,0xCD1B83,100); lineobj.moveTo(gun._x,gun._y); bullet.onEnterFrame = function() {           lineobj.lineTo(bullet._x,bullet._y);     with (this) {         _x += xStep;         _y += yStep;         yStep += gravity;         if (_root.trgt.hitTest(_x, _y, true)) {             xStep = 0;             yStep = 0;             gravity = 0;             _visible = false;         }     } }; 

       

      Now I am trying to get the trigectory line to be drawn befor the 'bullet' shoots, showing where its goiing to land and its arch.

      Any ideas?

       

      Thx pavel

        • 1. Re: as2 predicted trajectory
          kglad Adobe Community Professional & MVP

          paste formatted code into the forums so it's readable:

           

          this.createEmptyMovieClip("lineobj",getNextHighestDepth());

          lineobj.lineStyle(5,0xCD1B83,100);

          var trajectoryI:Number;

           

          paramsF();

          initTrajectoryF();

           

          function paramsF() {

              bulletSpeed = 6;

              bullet.gravity = 0.3;

              xd = trgt._x-gun._x;

              yd = trgt._y-gun._y;

              dist = Math.sqrt(xd*xd+yd*yd);

              flight = Math.ceil(dist/bulletSpeed);

              bullet._x = gun._x;

              bullet._y = gun._y;

              bullet.xStep = xd/flight;

              bullet.yStep = yd/flight;

              bullet.yStep -= flight*bullet.gravity/2;

              bullet._visible = true;

          }

           

          function initTrajectoryF():Void {

              lineobj.moveTo(gun._x,gun._y);

              updateTrajectoryF();

          }

           

          function updateTrajectoryF():Void {

              lineobj.lineTo(bullet._x,bullet._y);

              bullet._x += bullet.xStep;

              bullet._y += bullet.yStep;

              bullet.yStep += bullet.gravity;

              if (_root.trgt.hitTest(bullet._x, bullet._y, true)) {

                  bullet.xStep = 0;

                  bullet.yStep = 0;

                  bullet.gravity = 0;

                  bullet._visible = false;

                  clearInterval(trajectoryI);

              } else {

                  updateTrajectoryF();

              }

          }

          • 2. Re: as2 predicted trajectory
            pa-pavel Level 1

            Thank you so so so much! You are a gunius!

             

            If I still need help with something else but relating to this should I open a new discussion or ask here?

             

            -------------------------------------

            gun.onMouseDown = function()

            {

                      if(_ymouse < gun._y)

                      {

                                trgt._x = 400

                                trgt._y = 280

                      }

                      else if(_ymouse > gun._y)

                      {

                                trgt._x = 380

                                trgt._y = 350

                      }

                      //shoot();

            };

             

            ----------------------------

            If I add the code above how to I ge the trigectory line to adjust on the movement and draw a new line?

            • 3. Re: as2 predicted trajectory
              kglad Adobe Community Professional & MVP

              call paramsF() and initTrajectoryF():

               

              gun.onMouseDown = function()

              {

                        if(_ymouse < gun._y)

                        {

                                  trgt._x = 400

                                  trgt._y = 280

                        }

                        else if(_ymouse > gun._y)

                        {

                                  trgt._x = 380

                                  trgt._y = 350

                        }

              paramsF();

              initTrajectoryF();

              };

              1 person found this helpful
              • 4. Re: as2 predicted trajectory
                pa-pavel Level 1

                Gives me this error:

                 

                256 levels of recursion were exceeded in one action list.

                This is probably an infinite loop.

                Further execution of actions has been disabled in this movie.

                • 5. Re: as2 predicted trajectory
                  kglad Adobe Community Professional & MVP

                  use:

                   

                   

                  gun.onMouseDown = function() {

                      if (_ymouse<gun._y) {

                          trgt._x = 400;

                          trgt._y = 280;

                      } else if (_ymouse>gun._y) {

                          trgt._x = 380;

                          trgt._y = 350;

                      }

                      paramsF();

                      initTrajectoryF();

                  };

                  this.createEmptyMovieClip("lineobj",getNextHighestDepth());

                   

                  paramsF();

                  initTrajectoryF();

                   

                  function paramsF() {

                      bulletSpeed = 6;

                      bullet.gravity = 0.3;

                      xd = trgt._x-gun._x;

                      yd = trgt._y-gun._y;

                      dist = Math.sqrt(xd*xd+yd*yd);

                      flight = Math.ceil(dist/bulletSpeed);

                      bullet._x = gun._x;

                      bullet._y = gun._y;

                      bullet.xStep = xd/flight;

                      bullet.yStep = yd/flight;

                      bullet.yStep -= flight*bullet.gravity/2;

                      bullet._visible = true;

                  }

                   

                  function initTrajectoryF():Void {

                      lineobj.clear();

                      lineobj.lineStyle(5,0xCD1B83,100);

                      lineobj.moveTo(gun._x,gun._y);

                      updateTrajectoryF();

                  }

                   

                  function updateTrajectoryF():Void {

                      lineobj.lineTo(bullet._x,bullet._y);

                      bullet._x += bullet.xStep;

                      bullet._y += bullet.yStep;

                      bullet.yStep += bullet.gravity;

                      if (_root.trgt.hitTest(bullet)) {

                          bullet.xStep = 0;

                          bullet.yStep = 0;

                          bullet.gravity = 0;

                          bullet._visible = false;

                      } else {

                          updateTrajectoryF();

                      }

                  }

                  1 person found this helpful
                  • 6. Re: as2 predicted trajectory
                    pa-pavel Level 1

                    Ignore this msg...got it sorted ty so so so much!!

                     

                     

                    Hey thx again so this is how my code is looking right now:

                     

                    var Speed = 30;

                    var Gravity = 2.5;

                     

                     

                    gun.onPress = function()

                    {

                              gun.onEnterFrame = function()

                              {

                                        if (topbox.hitTest(_xmouse, _ymouse, true))

                                        {

                                                  trace("topbox");

                                                  trgt._x = 380;

                                                  trgt._y = 350;

                                                  if (Speed >= 10)

                                                  {

                                                            //Speed-=.2

                                                  }

                                        }

                                        else if (bottombox.hitTest(_xmouse, _ymouse, true))

                                        {

                                                  trace("bottombox");

                                                  trgt._x = 400;

                                                  trgt._y = 280;

                     

                     

                                        }

                              };

                              paramsF();

                              initTrajectoryF();

                     

                     

                    };

                     

                     

                    gun.onRelease = function()

                    {

                              delete gun.onEnterFrame;

                              lineobj.clear();

                              bulletSpeed = Speed;

                              bullet.gravity = Gravity;

                              xd = trgt._x - gun._x;

                              yd = trgt._y - gun._y;

                              dist = Math.sqrt(xd * xd + yd * yd);

                              flight = Math.ceil(dist / bulletSpeed);

                              bullet._x = gun._x;

                              bullet._y = gun._y;

                              bullet.xStep = xd / flight;

                              bullet.yStep = yd / flight;

                              bullet.yStep -= flight * bullet.gravity / 2;

                              bullet._visible = true;

                    };

                     

                     

                    bullet.onEnterFrame = function()

                    {

                              with (this)

                              {

                                        _x += xStep;

                                        _y += yStep;

                                        yStep += gravity;

                                        if (_root.trgt.hitTest(_x, _y, true))

                                        {

                                                  xStep = 0;

                                                  yStep = 0;

                                                  gravity = 0;

                     

                     

                                                  _visible = false;

                     

                     

                                        }

                              }

                    };

                     

                     

                    this.createEmptyMovieClip("lineobj",getNextHighestDepth());

                    lineobj.lineStyle(5,0xCD1B83,100);

                    var trajectoryI:Number;

                     

                     

                    //paramsF();

                    //initTrajectoryF();

                     

                     

                    this.createEmptyMovieClip("lineobj",getNextHighestDepth());

                     

                     

                    paramsF();

                    initTrajectoryF();

                     

                     

                    function paramsF()

                    {

                              bulletSpeed = Speed;

                              bullet.gravity = Gravity;

                              xd = trgt._x - gun._x;

                              yd = trgt._y - gun._y;

                              dist = Math.sqrt(xd * xd + yd * yd);

                              flight = Math.ceil(dist / bulletSpeed);

                              bullet._x = gun._x;

                              bullet._y = gun._y;

                              bullet.xStep = xd / flight;

                              bullet.yStep = yd / flight;

                              bullet.yStep -= flight * bullet.gravity / 2;

                              bullet._visible = true;

                    }

                     

                     

                    function initTrajectoryF():Void

                    {

                              lineobj.clear();

                              lineobj.lineStyle(5,0xCD1B83,100);

                              lineobj.moveTo(gun._x,gun._y);

                              updateTrajectoryF();

                    }

                     

                     

                    function updateTrajectoryF():Void

                    {

                              lineobj.lineTo(bullet._x,bullet._y);

                              bullet._x += bullet.xStep;

                              bullet._y += bullet.yStep;

                              bullet.yStep += bullet.gravity;

                              if (_root.trgt.hitTest(bullet))

                              {

                                        bullet.xStep = 0;

                                        bullet.yStep = 0;

                                        bullet.gravity = 0;

                                        bullet._visible = false;

                              }

                              else

                              {

                                        updateTrajectoryF();

                              }

                    }

                     

                     

                    But the line still doesnt adjust itself according to the target.

                    Any ideas? Sorry to keep bothering you with this, really appriciate your help!

                     

                    Thx Pavel

                    • 7. Re: as2 predicted trajectory
                      kglad Adobe Community Professional & MVP

                      you're welcome.

                      1 person found this helpful