11 Replies Latest reply on Nov 8, 2012 8:08 PM by CJ C

    ActionScript 2 Collision Problem

    CJ C

      I am trying to make a simple platform game, so I can expand on it later. It all works fine, except for the collision. The character hits the ground, but he hits is lower than I want him too. Here is a screenshot of it:

      Screenshot.png

      I am trying to make him hit the black line, instead of underneath it. I used a tutorial on how to make the platform game, and I downloaded the source. Theirs worked fine, and it was the same code. I have no idea what to do! Here is my code for the character if you need it:

       


      onClipEvent (load) {
       var grav:Number = 0;
       // gravity
       var speed:Number = 7;
       // how fast you walk
       var jumpHeight:Number = 15;
       // how high you jump
       var slow:Number = .5;
       // sets water falling speed
       var slowspd:Number = speed / 1.5;
       // sets water walking speed
       var setspeed:Number = speed;
       var scale:Number = _xscale;
       var ex:Number = 5;
       // makes hitTests better, change for a closer hitTest (warning, more buggy if smalle, less real if further)
       this.gotoAndStop(2);
      }
      onClipEvent (enterFrame) {
       grav++;
       _y += grav;
       while (_root.ground.hitTest(_x, _y, true))
       {
       _y--;
       grav = 0;
       }
       if (_root.water.hitTest(_x, _y, true))
       {
       if (grav > 0)
       {
       grav *= slow;
       }
       speed = slowspd;
       }
       else
       {
       speed = setspeed;
       }
       if (Key.isDown(68))
       {
       _x += speed;
       _xscale = scale;
       if (_root.ground.hitTest(_x, _y + 3, true))
       {
       this.gotoAndStop(1);
       }
       else
       {
       this.gotoAndStop(2);
       }
       }
       else if (Key.isDown(65))
       {
       _x -= speed;
       _xscale = -scale;
       if (_root.ground.hitTest(_x, _y + 3, true))
       {
       this.gotoAndStop(1);
       }
       else
       {
       this.gotoAndStop(2);
       }
       }
       else
       {
       if (_root.ground.hitTest(_x, _y + 3, true) && !Key.isDown(79) && !Key.isDown(73))
       {
       this.gotoAndStop(3);
       }
       }
       if (Key.isDown(79) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(73))
       {
       this.gotoAndStop(5);
       }
       if (Key.isDown(73) && !Key.isDown(87) && !Key.isDown(65) && !Key.isDown(68) && !Key.isDown(79))
       {
       this.gotoAndStop(4);
       }
       if (Key.isDown(87) && _root.ground.hitTest(_x, _y + 3, true))
       {
       grav = -jumpHeight;
       _y -= 4;
       this.gotoAndStop(2);
       }
       if (_root.ground.hitTest(_x + (_width / 2) + ex, _y - (_height / 2), true) || _root.ground.hitTest(_x + (_width / 2) + ex, _y - (_height / 6), true) || _root.ground.hitTest(_x + (_width / 2) + ex, _y - _height, true))
       {
       _x -= speed;
       }
       if (_root.ground.hitTest(_x - (_width / 2) - ex, _y - (_height / 2), true) || _root.ground.hitTest(_x - (_width / 2) - ex, _y - (_height / 6), true) || _root.ground.hitTest(_x - (_width / 2) - ex, _y - _height, true))
       {
       _x += speed;
       }
       if (_root.ground.hitTest(_x, _y - _height + 15, true))
       {
       grav = 1;
       }
      }
      

       

      Sorry if it's big, I don't know how to use spoilers on here. I understand how most of it works, just not the long collision lines. If someone could tell me how to fix it, and even better explain how it works, I would really appreciate it. Thanks!