Hit Test Error In Fps
- FiendMachine
-
FiendMachine
- Member since: Jul. 13, 2010
- Offline.
-
- Forum Stats
- Member
- Level 21
- Game Developer
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....
- Etherblood
-
Etherblood
- Member since: Apr. 14, 2013
- Offline.
-
- Forum Stats
- Member
- Level 12
- Game Developer
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
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
- MintPaw
-
MintPaw
- Member since: Jun. 11, 2006
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
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.
- Etherblood
-
Etherblood
- Member since: Apr. 14, 2013
- Offline.
-
- Forum Stats
- Member
- Level 12
- Game Developer
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
- FiendMachine
-
FiendMachine
- Member since: Jul. 13, 2010
- Offline.
-
- Forum Stats
- Member
- Level 21
- Game Developer
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.
- MintPaw
-
MintPaw
- Member since: Jun. 11, 2006
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
- FiendMachine
-
FiendMachine
- Member since: Jul. 13, 2010
- Offline.
-
- Forum Stats
- Member
- Level 21
- Game Developer
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
- GeoKureli
-
GeoKureli
- Member since: Apr. 1, 2003
- Offline.
-
- Forum Stats
- Supporter
- Level 19
- Game Developer
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.
This blog I made | This game sucks | FlxGreed | Home Run Swingers: Rhythm Baseball
many typos
- MintPaw
-
MintPaw
- Member since: Jun. 11, 2006
- Offline.
-
- Forum Stats
- Member
- Level 10
- Programmer
Well just post the code you're currently using for collision.
- FiendMachine
-
FiendMachine
- Member since: Jul. 13, 2010
- Offline.
-
- Forum Stats
- Member
- Level 21
- Game Developer
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

