13 Replies Latest reply on Jun 11, 2011 12:09 PM by LukeElChul

    HELP! with this script

    LukeElChul Level 1

      Im making a strategy like game and ive got this script for moving my people

       

      how could i make it so i dont need a script for unit1 and unit just 1 script for them both and how could i make it so u can click unit1 and then unit and then u can click and thay both move to where youve clicked.

       

       

       

       

      import mx.transitions.Tween;

      import mx.transitions.easing.*;

       

      unit.onRelease = function(){

      unit.onMouseUp = function(){

      var t1:Tween = new Tween(unit, "_x", None.easeNone, unit._x, _xmouse, 5, true);

      var t2:Tween = new Tween(unit, "_y", None.easeNone, unit._y, _ymouse, 5, true);

      dy = _root._ymouse-this._y;
      dx = _root._xmouse-this._x;
      radianrotation = Math.atan2(dy, dx);
      radianrotation = radianrotation*180/Math.PI;
      this._rotation = radianrotation;

      delete unit.onMouseUp;
      }
      }

       

       

       

       

       

      unit1.onRelease = function(){

      unit1.onMouseUp = function(){

      var t1:Tween = new Tween(unit1, "_x", None.easeNone, unit1._x, _xmouse, 5, true);

      var t2:Tween = new Tween(unit1, "_y", None.easeNone, unit1._y, _ymouse, 5, true);

      dy = _root._ymouse-this._y;
      dx = _root._xmouse-this._x;
      radianrotation = Math.atan2(dy, dx);
      radianrotation = radianrotation*180/Math.PI;
      this._rotation = radianrotation;

      delete unit1.onMouseUp;
      }
      }

        • 1. Re: HELP! with this script
          kglad Adobe Community Professional & MVP

          normally, if you click a unit (eg, unit1,unit2 etc) the next click would cause unit to move to the clicked position.  is that what you want?

          1 person found this helpful
          • 2. Re: HELP! with this script
            LukeElChul Level 1

            no what i need it to do is so when you click a unit (example,unit,unit1) then click another unit thay will both move to where you click

             

            so realy what i whant is so you can click on a unit then another unit it will select them both and thay will move to where you clicked

             

             

            I was thinking of making it like this

             

            but i dont realy have much knolage about "".

             

             

             

            import mx.transitions.Tween;

            import mx.transitions.easing.*;

             

            _root.unit = "";

             

            "".onRelease = function(){

            "".onMouseUp = function(){

            var t1:Tween = new Tween(unit"", "_x", None.easeNone, unit""._x, _xmouse, 5, true);

            var t2:Tween = new Tween(unit"", "_y", None.easeNone, unit""._y, _ymouse, 5, true);

            dy = _root._ymouse-this._y;
            dx = _root._xmouse-this._x;
            radianrotation = Math.atan2(dy, dx);
            radianrotation = radianrotation*180/Math.PI;
            this._rotation = radianrotation;

            delete unit"".onMouseUp;
            }
            }

             

            Then the code that goes on unit1 and unit

             

            on (press) {

            _root.unit = "unit1"

            }

             

            on (press) {

            _root.unit = "unit"

            }

             

            well i think this is how you do it could you please correct me in the right way

            • 3. Re: HELP! with this script
              kglad Adobe Community Professional & MVP

              what if you click the same unit twice and then click elsewhere?

              1 person found this helpful
              • 4. Re: HELP! with this script
                LukeElChul Level 1

                no i whant it so you can click one unit then click another unit and then when you click you move both them units

                 

                instead of moving the units 1 at a time

                 

                 

                so for example  .........

                 

                you click a unit and then you can move that unit to where you click.

                but i whant you to click a unit then click another unit and then you can move them both.

                 

                Its like selecting lots of units when you drag your mouse over them.

                 

                so basicly i whant to select and then move multiple units at a time.

                • 5. Re: HELP! with this script
                  kglad Adobe Community Professional & MVP

                  so, you'll never click the same unit twice in a row and you'll never want to click one unit and move the one and you'll never want to click 3 units and move all three.

                  • 6. Re: HELP! with this script
                    LukeElChul Level 1

                    no bacicly what i whant is so you can drag the mouse and select multiple units

                     

                    like the game Age of the Empires

                    • 7. Re: HELP! with this script
                      kglad Adobe Community Professional & MVP

                      that's not what you've been describing.  are you changing what you want?

                      • 8. Re: HELP! with this script
                        LukeElChul Level 1

                        Thank you for your patience

                         

                        I whant help on a selecting system like nano wars

                         

                        click to select your units or drag your mouse and select them

                        • 9. Re: HELP! with this script
                          kglad Adobe Community Professional & MVP

                          assign your units to the array, unitA.  that (and the tween parameters) are the only things you should change in the below code.

                           

                          when you want to select units call startSelect

                           

                          import mx.transitions.Tween;
                          import mx.transitions.easing.None;

                           

                          var tl:MovieClip = this;

                           

                          var unitA:Array = [];
                          var selectedUnitA:Array;

                           

                          function startSelectF():Void {
                              tl.onMouseDown = startF;
                              tl.onMouseUp = stopF;
                          }
                          function stopSelectF():Void {
                              delete tl.onMouseDown;
                              delete tl.onMouseUp;
                          }
                          function startF():Void {
                              start_x = tl._xmouse;
                              start_y = tl._ymouse;
                          }
                          function stopF():Void {
                              stopSelectF();
                              stop_x = tl._xmouse;
                              stop_y = tl._ymouse;
                              selectedF();
                              tl.onMouseUp = moveF;
                          }
                          function moveF() {
                              delete tl.onMouseUp;
                              for (var i:Number = 0; i<selectedUnitA.length; i++) {
                                  new Tween(selectedUnitA[i], "_x", None.easeNone, selectedUnitA[i]._x, tl._xmouse, 5, true);
                                  new Tween(selectedUnitA[i], "_y", None.easeNone, selectedUnitA[i]._y, tl._ymouse, 5, true);
                              }
                          }
                          function selectedF():Void {
                              var begin_x:Number = Math.min(start_x, stop_x);
                              var end_x:Number = Math.max(start_x, stop_x);
                              var begin_y:Number = Math.min(start_y, stop_y);
                              var end_y:Number = Math.max(start_y, stop_y);
                              selectedUnitA = [];
                              if (begin_x == end_x && begin_y == end_y) {
                                  for (var i:Number = 0; i<unitA.length; i++) {
                                      if(unitA[i].hitTest(begin_x,begin_y)){
                                          selectedUnitA.push(unitA[i]);
                                      }
                                  }
                              } else {
                                  for (var i:Number = 0; i<unitA.length; i++) {
                                      if (unitA[i]._x>=begin_x && unitA[i]._x<=end_x && unitA[i]._y>=begin_y && unitA[i]._y<=end_y) {
                                          selectedUnitA.push(unitA[i]);
                                      }
                                  }
                              }
                          }

                          • 10. Re: HELP! with this script
                            LukeElChul Level 1

                            Thank you for your time

                             

                            sorry for taking so long to explian my problam

                             

                            1 more Question can you make any surgestions where i can put this

                             

                            startSelectF();

                            • 11. Re: HELP! with this script
                              kglad Adobe Community Professional & MVP

                              when and how to call startSelectF() is a design decision.  there must be some criteria you use to determine when a user wants to select units to move.  whenever that is (eg, clicking a "move units" button), call startSelectF().

                              • 12. Re: HELP! with this script
                                LukeElChul Level 1

                                ok thanks

                                • 13. Re: HELP! with this script
                                  kglad Adobe Community Professional & MVP

                                  you're welcome.