A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: hitTest

  1. #1
    Senior Member
    Join Date
    Feb 2001
    Posts
    109
    I've got a movie clip of a ball that is bouncing around the screen using the code below within the clip. I want to make a second instance of the ball and detect collision between the two instances, if a collision is detected then the instances should reverse direction of travel in other words if they collide they bounce of each other. I've racked my brain with this and still get confused/syntax errors! Anyone able to help me?




    Code:
    onClipEvent (load) 
    {
    	dx = 4;// set the initial momemtum of the ball
    	dy = 4;
    }
    
    onClipEvent (enterFrame)
     {
        _x += dx; //move the ball along dx and dy
        _y += dy;
        if (_x < 0)
        {
    
          _x = 0; //move it back on screen
          dx *= -1; //reverse the horizontal momentum
        }
        else if (_x > 550)
        {
          _x = 550; //move it back on screen
          dx *= -1 //reverse the horizontal momentum
        } 
    
        if (_y < 0)
        {
          _y = 0;
          dy *= -1;
        }
        else if (_y > 400) 
        {
          _y = 400; 
          dy *= -1;
        }
    }
    I tried adding an extra if statement to the end of the code

    Code:
       if (this.hitTest(ball2))
       {
       dy *= -1;
       dx *= -1;
    }
    but it didn't work.. I know there should be some other stuf f in there too.. any help greatly appreciated.



  2. #2
    Senior Member
    Join Date
    Sep 2001
    Posts
    443
    No, you shouldn't need much else, as long as the second mc of a ball is called "ball2" in its instance, you may want to change it to

    if (this.hitTest(_root.ball2))

    and of course you have to put

    if (this.hitTest(_root.ball1))

    in ball2's mc instance.

    When you have more balls, then maybe move to a loop to test wheher _root.ball1.hitTest(_root["all" +i]) etc.
    It also becomes quicker to test that the distance between two balls is less than the combined radii, rather than hitTest - but you have all that to come!!

    http://www.btinternet.com/~fsjms/fla...nce/Movie1.swf

    swills


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