1 Reply Latest reply on Jun 21, 2011 6:50 AM by kglad

    Understanding this piece of code

    azzic7

      Hi all,

       

      I have got some code, but don't quite understand all the lines, any chance of helping me out?

      Code

      onClipEvent (load) {
      var grav:Number = 0;

      var speed:Number = 5;

      var jumpHeight:Number = 13;

      }

       

      //initalise variables to be used to move and jump


      onClipEvent (enterFrame) {
      grav++;

      _y += grav;

      while (_root.ground.hitTest(_x, _y, true)) {
      _y--;

      grav = 0;
      }

      // don't quite understand what this does



      if (Key.isDown(Key.RIGHT)) {
      _x += speed; //move to the right

       

      } else if (Key.isDown(Key.LEFT)) {
      _x -= speed; //move to the left

      }
      if (Key.isDown(Key.UP) && _root.ground.hitTest(_x, _y+3, true)) {  // what does y+3 do?

      grav = -jumpHeight;
      _y -= 4;  // if y-=4 is removed it makes no effect

      }
      }

        • 1. Re: Understanding this piece of code
          kglad Adobe Community Professional & MVP

          Code

          onClipEvent (load) {
          var grav:Number = 0;

          var speed:Number = 5;

          var jumpHeight:Number = 13;

          }

           

          //initalise variables to be used to move and jump


          onClipEvent (enterFrame) {
          grav++;

          _y += grav; // with above line accerlates this movieclip towards stage bottom

          while (_root.ground.hitTest(_x, _y, true)) {  // if this movieclip "hits" _root.ground
          _y--; // move up.  this repeats until this movieclip no longer "hits" _root.ground

          grav = 0;
          }

          // in total this stops your movieclip's acceleration towards stage-bottom and prevents it from being below ground



          if (Key.isDown(Key.RIGHT)) {
          _x += speed; //move to the right

           

          } else if (Key.isDown(Key.LEFT)) {
          _x -= speed; //move to the left

          }

          //this hittest checks if this movieclip is within 3px above the ground.  ie, almost hitting
          if (Key.isDown(Key.UP) && _root.ground.hitTest(_x, _y+3, true)) {  // what does y+3 do?

          grav = -jumpHeight;

          // if you don't see the effect of the below, it's because other code continues moving this movieclip up
          _y -= 4;  // if y-=4 is removed it makes no effect

          }
          }

          1 person found this helpful