Skip navigation
gaminflash
Currently Being Moderated

Collision problem

May 15, 2011 7:34 PM

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

 
Replies
  • Currently Being Moderated
    May 16, 2011 2:23 AM   in reply to gaminflash

    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?

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2011 10:01 AM   in reply to gaminflash

    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...

     
    |
    Mark as:
  • Currently Being Moderated
    May 16, 2011 10:30 AM   in reply to gaminflash

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2011 4:05 AM   in reply to gaminflash

    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

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2011 10:18 AM   in reply to gaminflash

    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.

     
    |
    Mark as:
  • Currently Being Moderated
    May 18, 2011 1:38 PM   in reply to gaminflash

    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;
    }
    
     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points