3 Replies Latest reply on Jun 15, 2009 6:43 AM by kglad

    Applying physics to movieclip created with createEmptyMovieClip...

    Mulefish92

      Hey all,

       

      Im new to the forums and actionscript so be generous...

       

      As the title suggests i want to apply physics (gravity mainly) to a line drawn with the curser using the createEmptyMovieClip method.

      Below is the code in the main frame (sorry its a bit long), what I'm trying to do is make something like crayon physics (i know it will be difficult).

      So far i can draw lines and make certain objects ( a ball for example) be affected by gravity, but when I try to make the drawn lines be affected by gravity it stuffs up.

       

      The first line drawn is produced in the wrong position, (it should be at mouse point but isn't) and you can't draw any subsequent lines. This problem doesnt exist when i remove the gravity, any help is much appreciated.

       

      Basically what i want is that each line that is drawn can have actions assigned to it... (so i can add gravity and unique properties for different line types)

       

      Hope that makes sense and you can help .

      //force settings
      //Drawing tools
      _global.general = 0x0000FF;
      //blue
      _global.rubber = 0x9400D3;
      //purule
      _global.spring = 0x32CD32;
      //green
      _global.rope = 0x8B4513;
      //brown
      _global.explosion = 0xFFFF00;
      //yellow
      _global.explosion2 = 0xFFA500;
      //orange
      //tool setings
      var thickness = 2;
      _global.tool = general;
      var alpha = 100;
      //Scene colours
      var land = 0x000000;
      //black
      var prop = 0x1D1D1D;
      //charcoal      (non-gravity affected prop)
      var gprop = 0x696969;
      //dim gray      (gravity affected prop)
      var backg = 0xD3D3D3;
      //light gray
      //Active objects
      var main = 0xFF0000;
      //red
      var orange = 0xFF4500;
      //orange
      var a = 1;
      var b = gen;
      var menu = 0;
      var d = 0;
      LineA = new Array();
      
      this.onLoad = function() {
      
      for (a in LineA) {
      LineA[a].power = 0.3;
      LineA[a].yspeed = 0;
      LineA[a].xspeed = 0;
      LineA[a].friction = 0.95;
      LineA[a].gravity = 0.1;
      LineA[a].thrust = 0.75;
      }
      };
      
      this.onEnterFrame = function() {
      d++
      for (a in LineA) {
      LineA[a].xspeed *= friction;
      LineA[a].yspeed += gravity;
      LineA[a]._y += yspeed;
      LineA[a]._x += xspeed;
      }
      };
      
      
      onMouseDown = function () {
      createEmptyMovieClip("Line", 1);
      Line.lineStyle(thickness, tool, alpha);
      
      // The function onMouseDown is activated when you click the mouse 
      button.
      Line.moveTo(_xmouse, _ymouse);
      // This moves the line to the starting point, which will be directly 
      where you
      // clicked._xmouse and _ymouse are the x and y position of the mouse.
      onMouseMove = function () { Line.lineTo(_xmouse, _ymouse);};
      // This creates the line to the point where ever your mouse is dragged.
      };
      onMouseUp = function () {
      // The function onMouseUp is activated when you release the mouse 
      button.
      onMouseMove = null;
      // This function turns off the onMouseMove() function so that the 
      line will
      // not be drawn again until the user clicks the mouse button again.
      a++;
      // d = a
      // a = b+a
      duplicateMovieClip(LineA, "LineA"+a, a);
      // a = d
      c = a;
      };
      
      

        • 1. Re: Applying physics to movieclip created with createEmptyMovieClip...
          kglad Adobe Community Professional & MVP

          try:

           

           

           

          //force settings
          //Drawing tools
          _global.general = 0x0000FF;
          //blue
          _global.rubber = 0x9400D3;
          //purule
          _global.spring = 0x32CD32;
          //green
          _global.rope = 0x8B4513;
          //brown
          _global.explosion = 0xFFFF00;
          //yellow
          _global.explosion2 = 0xFFA500;
          //orange
          //tool setings
          var thickness = 2;
          _global.tool = general;
          var alpha = 100;
          //Scene colours
          var land = 0x000000;
          //black
          var prop = 0x1D1D1D;
          //charcoal      (non-gravity affected prop)
          var gprop = 0x696969;
          //dim gray      (gravity affected prop)
          var backg = 0xD3D3D3;
          //light gray
          //Active objects
          var main = 0xFF0000;
          //red
          var orange = 0xFF4500;
          //orange
          var a = 1;
          var b = gen;
          var menu = 0;
          var d = 0;
          LineA = new Array();

          this.onLoad = function() {

          for (a in LineA) {
          LineA[a].power = 0.3;
          LineA[a].yspeed = 0;
          LineA[a].xspeed = 0;
          LineA[a].friction = 0.95;
          LineA[a].gravity = 0.1;
          LineA[a].thrust = 0.75;
          }
          };

          this.onEnterFrame = function() {
          d++
          for (a in LineA) {
          LineA[a].xspeed *= friction;
          LineA[a].yspeed += gravity;
          LineA[a]._y += yspeed;
          LineA[a]._x += xspeed;
          }
          };


          onMouseDown = function () {
          createEmptyMovieClip("Line", 1);
          Line.lineStyle(thickness, tool, alpha);

          // The function onMouseDown is activated when you click the mouse
          button.
          Line.moveTo(_xmouse, _ymouse);
          // This moves the line to the starting point, which will be directly
          where you
          // clicked._xmouse and _ymouse are the x and y position of the mouse.
          onMouseMove = function () { Line.lineTo(_xmouse, _ymouse);};
          // This creates the line to the point where ever your mouse is dragged.
          };
          onMouseUp = function () {
          onMouseMove = null;
          lineA.push(Line);
          };




          1 person found this helpful
          • 2. Re: Applying physics to movieclip created with createEmptyMovieClip...
            Mulefish92 Level 1

            Thanks Kglad, but now it only draws one line and has no gravity, ill spend some time on it tomorrow but can you see the problem?

             

            Thanks in advance

            • 3. Re: Applying physics to movieclip created with createEmptyMovieClip...
              kglad Adobe Community Professional & MVP

              try:

               

              //force settings
              //Drawing tools
              _global.general = 0x0000FF;
              _global.rubber = 0x9400D3;
              _global.spring = 0x32CD32;
              _global.rope = 0x8B4513;
              _global.explosion = 0xFFFF00;
              _global.explosion2 = 0xFFA500;

               

              power = 0.3;
              friction = 0.95;
              gravity = 0.1;
              thrust = 0.75;

               

              //tool setings
              var thickness = 2;
              _global.tool = general;
              var alpha = 100;
              //Scene colours
              var land = 0x000000;
              //black
              var prop = 0x1D1D1D;
              //charcoal      (non-gravity affected prop)
              var gprop = 0x696969;
              //dim gray      (gravity affected prop)
              var backg = 0xD3D3D3;
              //light gray
              //Active objects
              var main = 0xFF0000;
              //red
              var orange = 0xFF4500;
              //orange
              var a = 1;
              var b = gen;
              var menu = 0;
              var dep = 0;
              LineA = new Array();

               

              this.onEnterFrame = function() {

               

                  for (var i = 0; i<LineA.length; i++) {

               

                      LineA[i].xspeed *= friction;
                      LineA[i].yspeed += gravity;
                      LineA[i]._y += LineA[i].yspeed;
                      LineA[i]._x += LineA[i].xspeed;
                 
                  }
              };

               


              onMouseDown = function () {
                  Line = createEmptyMovieClip("Line"+dep, dep++);
                  Line.lineStyle(thickness,tool,alpha);
                  Line.moveTo(_xmouse,_ymouse);
                  Line.xspeed = 0;
                  Line.yspeed = 0;
                  LineA.push(Line);
                  onMouseMove = function () {
                      Line.lineTo(_xmouse,_ymouse);
                  };
              };
              onMouseUp = function () {
                  onMouseMove = null;
              };