A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Collision with 5 different objects

  1. #1
    Junior Member
    Join Date
    May 2011
    Posts
    2

    Collision with 5 different objects

    Hello I've got a simple function checking the collision between one object to four others. I'm pretty sure I made it work earlier today but it doesn't anymore, I'm utterly perplexed by this.

    Code:
                    if(square, edge1, edge2, edge3, edge4.hitTestObject(Circle)){
                                
                                gotoAndStop(46);
                                removeChild(Circle);
    
                 removeEventListener(MouseEvent.MOUSE_DOWN,mousePress);
                 removeEventListener(Event.ENTER_FRAME,doAll);
                                
                            }
    It only collides with the square, which is the first object stated.
    I've tried using one of these "checks" for each object but that doesn't work either.
    Is the code faulty in any way, or is there another solution to this?

    Thanks in advance

  2. #2
    Total Universe Mod jAQUAN's Avatar
    Join Date
    Jul 2000
    Location
    Honolulu
    Posts
    2,429
    You have to run a hit test on each object in question using the or (||) operator

    if(square.hitTestObject(Circle) || edge1.hitTestObject(Circle) || edge2.hitTestObject(Circle) ... etc.

    Yes there are better ways to do it but it's somewhat involved. I highly recommend the book "Making Things Move" - AS3 edition by Keith Peters.

  3. #3
    Senior Member
    Join Date
    Jun 2008
    Posts
    549
    You could also use a For each statement to run a hit test for each item.

    Code:
    for each (var item in [square,edge1,edge2,edge3,edge4]) {
    	if (item.hitTestObject(Circle)) {
    		
    		gotoAndStop(46);
    		removeChild(Circle);
    
    		removeEventListener(MouseEvent.MOUSE_DOWN,mousePress);
    		removeEventListener(Event.ENTER_FRAME,doAll);
    	}
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center