Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

My question is about targeting a child with a unique parent. For example. Let's say I have a box people can move called box_mc and 3 platforms it can jump on called:

  • Platform_1
  • Platform_2
  • Platform_3

All of these platforms have a child element called hit.

  • Platform_1
    • Hit
  • Platform_2
    • Hit
  • Platform_3
    • Hit

I use an array and a for each statement to detect if box_mc hits one of the platforms childs.

var obj_arr:Array = [Platform_1, Platform_2, Platform_3];
for each(obj in obj_arr){
   if(box_mc.hitTestObject(obj.hit)){
      trace(obj + " " + obj.hit);
      box_mc.y = obj.hit.y - box_mc.height;
   }
}

obj seems to output the unique parent it is hitting but obj.hit ouputs hit, so my theory is that it is applying the change of y to all the childs called hit in the stage. Would it be possible to only detect the child of that specific parent?

share|improve this question
add comment

1 Answer 1

You should make use of the hitArea property of your platforms (AS3 docs).

So first make the hit children the hitArea of your platforms, like so:

var obj_arr:Array = [Platform_1, Platform_2, Platform_3];
for each(obj in obj_arr){
   obj.hitArea = obj.hit;
}

Then you can detect collisions with your platforms like so:

for each(obj in obj_arr){
   if(box_mc.hitTestObject(obj)){
      // collision occured
   }
}

The collision test will be between your box_mc and your platforms, but the shape of the hit object will be used as collision shape of your platform.

share|improve this answer
    
Hi bummzack, Thanks for the answer! I have one more question. The hitarea is not the only child in the platform parent that can be hit. Each platform parent has a hit, bot and base child which the player can hit. Do you perhaps have any tips on how to do this? –  Koen Mar 9 '13 at 13:27
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.