0

I am having trouble with getting some code dealing with collision detection to work. I have tested to ensure that it does detect the collisions, and it does, however I am unable to get it to work as it just walks through the block. I also tried just taking away -10 from the x value after it collides but it will stay for the first couple of tries then just walk through the block.

private function collisionDetect(evt:Event):void{
    if(IMG3.hitTestObject(block)){
        if(IMG3.x > block.x){
            IMG3.x = block.x-1;
        }
    }
}
Sam DeHaan
  • 10,246
  • 2
  • 40
  • 48
user1017243
  • 177
  • 1
  • 2
  • 8
  • How does hitTestObject work and when is collisionDetect triggered, I don't think it's possible for others to find answer with this snippet. – kyohiro Nov 23 '11 at 01:51
  • 1
    hitTestObject returns true if the two objects collide, and collisionDetect is trigged by addEventListener(Event.ENTER_FRAME,collisionDetect); – user1017243 Nov 23 '11 at 03:32
  • 1
    `IMG3.x > block.x` This is problematic, unless IMG3's is located at the right edge and block's center is located at the left edge. You need to account for their width, so assuming them both have their center at the left edge, the correct would be `IMG3.x + IMG3.width > block.x`. – felipemaia Nov 23 '11 at 04:38

1 Answers1

0

It depends on what rate the objects are moving. Provide more details on what you're trying to accomplish. Your code should work fine if both object rects are colliding AND img's center point x is greater than block's center x.

Where's the code responsible for moving the object?

Anyway another approach would be a while loop:

private function collisionDetect(evt:Event):void{
  if(IMG3.hitTestObject(block)){
    while (IMG3.x > block.x) 
    {
      IMG3.x--;
    }
  }
}

But again it depends on how the IMG is moved to begin with and where it happens in the code. Also note felipemaia's comment on the center point of the object. The correct way would be (IMG3.x + IMG3.width / 2) > ( block.x - block.width / 2) assuming both center points of the objects are indeed their precise center.

p.s rephrase the title - this is not a Flash Builder problem

ido
  • 811
  • 2
  • 8
  • 20