16 Replies Latest reply: May 18, 2011 2:12 PM by gaminflash RSS

    Collision problem

    gaminflash

      I have my script:

       

      var radians:uint = 2;

      var speed:Number = 2;

       

      function Collision(event:Event):void{

      var localpointx:Point = new Point(hero.playerbottom.x);
          var heropointx:Point = hero.localToGlobal(localpointx);
      var localpointy:Point = new Point (hero.playerbottom.y);
      var heropointy:Point = hero.localToGlobal(localpointy);

       

      if(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
      hero.y += speed;
      }

      if(box1.bottomhit.hitTestPoint(heropointx.x-radians, heropointy.y, true)){
      hero.x += speed;
      }
      if(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y+radians, true)){
      hero.y -= speed;
      }
      if(box1.bottomhit.hitTestPoint(heropointx.x+radians, heropointy.y, true)){
      hero.x -= speed;
      }

      }

       

      Everything seems to be working except that the hit script is too laggy in my opinion, when he touches the surface it pushes him back at the number of speed and if you walk towards the item he is being pushed back and walks to the item at the same time; walking up a slope will look like walking up stairs. I just want him to not being able to go through the item and that it gives a smooth collision.

       

      If you don't understand what i mean with the laggy script, create an own swf file and try it out by yourself.

       

      Thanks in advance

        • 1. Re: Collision problem
          Dinkyfish Community Member

          All I can think of doing is increasing the framerate of your project (you'll need to decrease your speed variable accordingly). I assume your collision function is being called on ENTER_FRAME events?

          • 2. Re: Collision problem
            gaminflash Community Member

            I already have 60 fps, i changed the value to 120 but there was still a minor lag in the movement.

             

            What i am trying to achieve here is normal hittest where one movieclip is the wall the floor or a slope. I searched for a solution for this and all i can seem to find is the hitTestPoint solution, nothing about what happens afterwards. How the script should manage to make the movieclip to act as a wall. Like in a platformer.

            • 3. Re: Collision problem
              gaminflash Community Member

              I forgot to mention that this is done in an Event.ENTER_FRAME function.

              • 4. Re: Collision problem
                gaminflash Community Member

                anyone?

                • 5. Re: Collision problem
                  Dinkyfish Community Member

                  You could try having your collision function called from a Timer running at very small intervals rather than on an enter frame event. That's about all I've got to offer just now...

                  • 6. Re: Collision problem
                    gaminflash Community Member

                    I am thankful that you try to help me but this did not fix the problem, i think the major problem in this is that when the player touches the item he gets pushed back for too many frames, the good thing would be to have hero.x or y to be -- or ++ instead of += speed. But if i do it the amount of the speed that the player is moving is higher than the pushback and he is actually managing to get through it. :/

                    • 7. Re: Collision problem
                      Dinkyfish Community Member

                      So the player is not being pushed back by the correct amount? Sorry if I'm misunderstanding here haha I'm terrible at reading other people's code.

                       

                      If that is the case you could simply set the hero's x/y positions to be where you want him to be after the collision instead of adding/subtracting speed:

                       

                      e.g. if(hero.y + hero.height >  floorOfLevel_mc.y)

                      {

                           hero.y = floorOfLevel_mc.y - hero.height;

                      }

                       

                      if that makes any sense? Sorry again if this isn't what you're meaning.

                      • 8. Re: Collision problem
                        gaminflash Community Member

                        But the thing is, i don't want to change the hittest, just the thing afterwards

                        • 9. Re: Collision problem
                          gaminflash Community Member

                          I was looking around i saw that "while" the loop condition could fix the problem and i changed everything to:

                           

                          function Collision(event:Event):void{
                          var localpointx:Point = new Point(hero.playerbottom.x);
                          var heropointx:Point = hero.localToGlobal(localpointx);
                          var localpointy:Point = new Point (hero.playerbottom.y);
                          var heropointy:Point = hero.localToGlobal(localpointy);

                          while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
                          hero.y += speed;
                          }
                          while(box1.bottomhit.hitTestPoint(heropointx.x-radians, heropointy.y, true)){
                          hero.x += speed;
                          }
                          while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y+radians, true)){
                          hero.y -= speed;
                          }
                          while(box1.bottomhit.hitTestPoint(heropointx.x+radians, heropointy.y, true)){
                          hero.x -= speed;
                          }
                          }

                           

                          It all works fine until it touches it, then everything crashes and after 15 secs the session closes. Isn't it possible to have a "while" in a ENTER_FRAME function? If not, how am i supposed to check if the player touches it?

                          • 10. Re: Collision problem
                            gaminflash Community Member

                            After a little bit of testing i realized that even this code made it crash:

                            var localpointx:Point = new Point(hero.playerbottom.x);
                              var heropointx:Point = hero.localToGlobal(localpointx);
                            var localpointy:Point = new Point (hero.playerbottom.y);

                            var heropointy:Point = hero.localToGlobal(localpointy);

                             

                            while(box1.bottomhit.hitTestPoint(heropointx.x, heropointy.y-radians, true)){
                            hero.y += speed;
                            }

                             

                            It didn't even have to be in a function to crash, was it the nested movieclip or the variablepoints that made it go all crazy and crash? What is the actual problem here? The only thing i know is that it is possible to have a "while" with a hitTestPoint.

                             

                            Thanks in advance

                            • 11. Re: Collision problem
                              kennethkawamoto2 CommunityMVP

                              It will definitely crash because once it is evaluated true your while loop will never exit. The logic is wrong anyway but do not use while to move objects in general if you running this code on every frame then you can use if instead

                              • 12. Re: Collision problem
                                gaminflash Community Member

                                So there is no way i can make the while statement true when it is touching the item and it isn't, so i can't do:


                                while(Boolean == true){

                                hero.y ++;

                                }

                                 

                                ?

                                • 13. Re: Collision problem
                                  kennethkawamoto2 CommunityMVP

                                  Firstly your test condition does not change during the while loop so if it's true when the loop starts it continue to execute until it finally crashes. To fix this you have to revalue the var used in the test on each loop iteration. However... the next point:

                                   

                                  Secondly the screen redraw happens when the playhead enters the new frame, and the playhead enters the new frame only after all the code is executed in the current frame. This means you do not see any visual movements while while loop is running.

                                   

                                  So, if you are testing the condition on every frame you should change while to if, so that the test runs once per frame.

                                  • 14. Re: Collision problem
                                    gaminflash Community Member

                                    But when i choose if in the hittest function it only adds 1 movement on the y axis each frame, i want to add all the movement within one frame wich can be done with "while" that is why i want it, heres an example that it does work: http://www.newgrounds.com/bbs/topic/1033481

                                    My problem is that i have nested movieclips.

                                    • 15. Re: Collision problem
                                      kennethkawamoto2 CommunityMVP

                                      Your code before the loop is not right either Not sure exactly what you're after but purely from the code point of view here's the corrected:

                                      while (box1.bottomhit.hitTestPoint(hero.localToGlobal(new Point(hero.playerbottom.x, hero.playerbottom.y)).x, hero.localToGlobal(new Point(hero.playerbottom.x, hero.playerbottom.y)).y - radians, true)) {
                                          hero.y += speed;
                                      }
                                      
                                      • 16. Re: Collision problem
                                        gaminflash Community Member

                                        Thanks it worked perfectly! You certainly made my day