Be a Supporter!

Hit Test Error In Fps

  • 160 Views
  • 11 Replies
New Topic Respond to this Topic
FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Hit Test Error In Fps 2014-05-25 10:45:01 Reply

I am using hitTest code for automatic weapons in a fps game.
When the mouse button is down,a small invisble box is placed on the crosshair and any target touching it gets destroyed.
I am applying hitTest code on the target in reference to the the small invisble box.

The problem is that what if 3 targets are placed one behind the other with each of them getting shifted by 5.0 on the _x axis(see IMage).
Now,if target 2 is fired,target 1 and 3 also get hit although ONLY target 3 should get hit because it's in the front.
What should I do so that only the target in the front gets hit and then when its destroyed,the target below it and so on....

Hit Test Error In Fps


BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Hit Test Error In Fps 2014-05-25 10:59:14 Reply

At 5/25/14 10:45 AM, MetalBooster wrote: I am using hitTest code for automatic weapons in a fps game.
When the mouse button is down,a small invisble box is placed on the crosshair and any target touching it gets destroyed.
I am applying hitTest code on the target in reference to the the small invisble box.

The problem is that what if 3 targets are placed one behind the other with each of them getting shifted by 5.0 on the _x axis(see IMage).
Now,if target 2 is fired,target 1 and 3 also get hit although ONLY target 3 should get hit because it's in the front.
What should I do so that only the target in the front gets hit and then when its destroyed,the target below it and so on....

Just sort the targets in z-order, and when iterating through them break out of the loop after finding the first one hitting the crosshair.

FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Hit Test Error In Fps 2014-05-25 13:27:58 Reply

Just sort the targets in z-order, and when iterating through them break out of the loop after finding the first one hitting the crosshair.

Sorry sir,I didn't get you


BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Hit Test Error In Fps 2014-05-25 13:41:41 Reply

You're looping through all the movieClips to test which ones the block is over right? When it hits one run the command "break" to end the loop.

If the array is in order of depth then it will always remove the one on top. Otherwise you'll have to check the depths in the loop.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
Etherblood
Etherblood
  • Member since: Apr. 14, 2013
  • Offline.
Forum Stats
Member
Level 12
Game Developer
Response to Hit Test Error In Fps 2014-05-25 16:03:44 Reply

At 5/25/14 01:27 PM, Vitraxity wrote:
Just sort the targets in z-order, and when iterating through them break out of the loop after finding the first one hitting the crosshair.
Sorry sir,I didn't get you

What didn't you understand?
Sorting in z-order in this case means, that the frontmost element will be the first element in your array and the last arrayelement will be at the back.
Now you iterate/loop from front to back through your array and memorize which element was first or just break/interrupt the loop after finding your first element.

FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Hit Test Error In Fps 2014-05-25 22:07:15 Reply

Got it


BBS Signature
FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Hit Test Error In Fps 2014-05-26 12:00:24 Reply

Wait,what do I exactly put the condition in the loop,say if,the targets in order are
'victim1' 'victim2' and 'victim3'

What didn't you understand?
Sorting in z-order in this case means, that the frontmost element will be the first element in your array and the last arrayelement will be at the back.
Now you iterate/loop from front to back through your array and memorize which element was first or just break/interrupt the loop after finding your first element.

BBS Signature
MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Hit Test Error In Fps 2014-05-26 14:11:55 Reply

Post the code you're using.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Hit Test Error In Fps 2014-05-26 22:35:16 Reply

At 5/26/14 02:11 PM, MintPaw wrote: Post the code you're using.

that's the problem,I dont get it what code to use in the loop condition


BBS Signature
GeoKureli
GeoKureli
  • Member since: Apr. 1, 2003
  • Offline.
Forum Stats
Supporter
Level 19
Game Developer
Response to Hit Test Error In Fps 2014-05-27 10:47:10 Reply

At 5/26/14 10:35 PM, Vitraxity wrote:
At 5/26/14 02:11 PM, MintPaw wrote: Post the code you're using.
that's the problem,I dont get it what code to use in the loop condition
var child:DisplayObject;
for(var i:int = container.numChildren-1; i >= 0; i++){
     child = getChildAt(i);
     if(collision logic here) {
           onObectShot();
           break; // <--- breaks the loop
     }
}

objects are z-ordered by their spot in the parents list. so if the parent has 4 objects, getChildAt(3) is in front, getChildAt(0) is in back.

MintPaw
MintPaw
  • Member since: Jun. 11, 2006
  • Offline.
Forum Stats
Member
Level 10
Programmer
Response to Hit Test Error In Fps 2014-05-27 13:07:02 Reply

Well just post the code you're currently using for collision.


If ya have something to say, PM me. I have a lot of time to spare.
Also never PM egg82.

BBS Signature
FiendMachine
FiendMachine
  • Member since: Jul. 13, 2010
  • Offline.
Forum Stats
Member
Level 21
Game Developer
Response to Hit Test Error In Fps 2014-05-27 13:18:28 Reply

Thanks for the code!
I was confused at the point how to access the different objects in a loop,will try out the given code now


BBS Signature