Game Development Community

Help getting crosshair to recognize enemy

by Scott McCann · in Torque Game Builder · 12/30/2005 (11:53 pm) · 5 replies

Hello,

I started working on a shooting game and have ran into a some problems. when I put the crosshair over the enemy, I would like it to recognize that the enemy is there and then give the player the ability to destroy the enemy. My screen right now has only a crosshair, enemy, and the background. What do I do?


thanks in advance,
Scott

About the author

Recent Threads


#1
12/31/2005 (1:11 am)
Look into collision. The Space Shooter tutorial uses this to have you blow up enemy ships. In your case, the collision will not automatically destroy the enemy. It will allow you to know when the enemy COULD be killed, if the player pulls the trigger. Does that make sense?

In other words: Set up collision detection between your crosshair and the enemy. When the crosshair collides with an enemy, keep track of that in a global variable. When the user "fires" and the global is set, kill the enemy. When the crosshair stops colliding with an enemy, update the state of your global variable.
#2
12/31/2005 (6:36 am)
You could also use the "t2dSceneGraph::pickXXX" commands to detect which objects are under your crosshair.

- Melv.
#3
01/13/2006 (11:18 pm)
Hey,

I looked into what you said, Jason, and got it working, except for one problem. I can't get the collisions only to reconizes the target and not to delete it. As my game is right now, when I put the crosshair over the enemy it destroys it instead of what I would like is put its name just above it. I think the problem llies in code I got from the tutorial, It says something about ".safedelete"

looks like this:

function fxSceneObject2D::onCollision( %srcObj, %dstObj, %srcRef, %dstRef, %time,
%normal, %contactCount, %contacts )
{
if (%srcObj == $player)
{

%dstObj.safeDelete();
}
else if (%dstObj == $player)
{

%srcObj.safeDelete();
}
else
{
%srcObj.safeDelete();
%dstObj.safeDelete();
}
}

again I am not sure this is the problem

thanks for helping,
Scott
#4
01/14/2006 (10:30 pm)
.safeDelete removes deletes objects. Assuming your crosshair is the $player object, this code basically says "if the player is either the source or destination of the collision, delete the non-player object." The last case, the else, says if any two objects collide and neither is the player, delete them both.

What I suggested in my earlier post would be to replace the delete with setting a variable, list $crosshairOver = %dstObject (in the first case)... $crosshairOver = %srcObject (in the second case). Then, when the user clicks the fire button, do $crosshairOver.safeDelete();

This should work except for one case: when the crosshair goes over an object and then STOPS colliding with it. You need a way to reset $crosshairOver.

Melv: Does onCollision only fire "onCollision" or does it fire every frame? If it fires every frame, you could just reset $crosshairOver in the onSceneUpdate event handler, and then the onCollision handler would reset it each frame. Barring that, pickXXX is your best bet.
#5
01/17/2006 (12:24 am)
Jason,

The "OnCollision" callback only gets called when there's a collision. If there's a collision every frame (because the object(s) don't solve the collision) then you'll get the callback every frame.

- Melv.