Skip to main content
Participating Frequently
April 13, 2015
Answered

How to find the objects names which hit on other object

  • April 13, 2015
  • 3 replies
  • 1273 views

Dear Friends,

I am moving a movieclip on the screen every where on the screen, during the movement I will hit or overlap other objects on the screen. I want to find the name of the objects which hit or overlap my movieclip.

for example I used trace(my_mc.hitTestObject(objnames). Here the objnames may be any object(movie clips) on the screen. I want to find which object hitted my movieclip..

please help me...its like a game project.

Thanks in Advance,

Syed Abdul Rahim

This topic has been closed for replies.
Correct answer moccamaximum

Dear Mr.Moccamaxium,

Thks for the reply.. iam getting the following error:

RangeError: Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/getChildAt() at Untitled_fla::MainTimeline/detectCollision()

in ur code I have adjusted the parenthesis...like below:

stage.addEventListener(MouseEvent.MOUSE_MOVE, detectCollision);

function detectCollision(e:MouseEvent):void
{
for (var i:int = 0; 1<this.numChildren-1; i++)
{
  trace("test "+this.numChildren);
  //don`t test an objects collision with itself
  if (getChildAt(i).name != "myObject")
  {
   trace(i);
   if (myObject.hitTestObject(getChildAt(i)))
   {

    trace("my Object hit " +getChildAt(i).name);
   }
  }
}
}

Thanks in Advance,

Syed Abdul Rahim


My fault, i had a typo in my loop:

var i:int = 0; 1<this.numChildren;i++


should be


var i:int = 0; i<this.numChildren;i++



Now it works as expected



3 replies

Participating Frequently
April 15, 2015

Dear Mr.Moccamaxium,

Thks for the reply.. iam getting the following error:

RangeError: Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/getChildAt() at Untitled_fla::MainTimeline/detectCollision()

in ur code I have adjusted the parenthesis...like below:

stage.addEventListener(MouseEvent.MOUSE_MOVE, detectCollision);

function detectCollision(e:MouseEvent):void
{
for (var i:int = 0; 1<this.numChildren-1; i++)
{
  trace("test "+this.numChildren);
  //don`t test an objects collision with itself
  if (getChildAt(i).name != "myObject")
  {
   trace(i);
   if (myObject.hitTestObject(getChildAt(i)))
   {

    trace("my Object hit " +getChildAt(i).name);
   }
  }
}
}

Thanks in Advance,

Syed Abdul Rahim

Participating Frequently
April 14, 2015

Dear Friends,

Pls reply me.... How can I find the hitted object name, My object will move with my mouse and hit or overlap the objects in the screen.. I want to get those name (name of the objects)... pls help me to find...

Thanks in advance,

Syed Abdul Rahim

Inspiring
April 14, 2015

with an object named "myObject" on stage:

stage.addEventListener(MouseEvent.MOUSE_MOVE, detectCollision);

function detectCollision(e:MouseEvent):void{

for (var i:int = 0; 1<this.numChildren;i++){

//don`t test an objects collision with itself

   if (getChildAt(i).name != "myObject"){

  if(myObject.hitTestObject(getChildAt(i)){

   trace("my Object hit " +getChildAt(i).name;

     }

   }

}

CAVEAT: Flash native collision detection has low standards, it`s not designed to detect collisions of complex shapes.

For Pixel Perfect collision detection you will have to look further

Pixel Perfect Collision Detection | Free ActionScript

Participating Frequently
April 15, 2015

Dear Mr.Moccamaxium,

Thks for the reply.. iam getting the following error:

RangeError: Error #2006: The supplied index is out of bounds. at flash.display::DisplayObjectContainer/getChildAt() at Untitled_fla::MainTimeline/detectCollision()

in ur code I have adjusted the parenthesis...like below:

stage.addEventListener(MouseEvent.MOUSE_MOVE, detectCollision);

function detectCollision(e:MouseEvent):void
{
for (var i:int = 0; 1<this.numChildren-1; i++)
{
  trace("test "+this.numChildren);
  //don`t test an objects collision with itself
  if (getChildAt(i).name != "myObject")
  {
   trace(i);
   if (myObject.hitTestObject(getChildAt(i)))
   {

    trace("my Object hit " +getChildAt(i).name);
   }
  }
}
}

Thanks in Advance,

Syed Abdul Rahim

Participating Frequently
April 13, 2015

trace(my_mc.hitTestObject(objnames).

I mean I want to find the objnames here..