Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Question: Sprite to fire when target within range?

Author
Message
sweeteye
13
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 3rd Dec 2011 08:32
For my 2D wargame I would like to have my sprites begin firing projectiles only when an opposing sprite comes within range. At the moment they just continuously fire. Trying to figure out how to go about coding this to make it happen. Thought perhaps I might get a tip from someone that might get me started and then I could adjust it to fit my coding.. I have just started thinking about how to implement this and it seems as though an "if" "then" statement would be used and/or a "true" and "false" statement. I suppose <= would also have to be used for the range. Happy Holidays.....
Wizz
15
Years of Service
User Offline
Joined: 27th Apr 2009
Location:
Posted: 3rd Dec 2011 13:50 Edited at: 3rd Dec 2011 13:53
You will need to use some maths here. Use the distance formula (http://www.purplemath.com/modules/distform.htm). The code should look something like this:



Hassan
15
Years of Service
User Offline
Joined: 4th May 2009
Location: &lt;script&gt; alert(1); &lt;/script&gt;
Posted: 3rd Dec 2011 14:29 Edited at: 3rd Dec 2011 14:30
yes it's just a simple distance check, if you want further help you could show some code on how are you handling the enemies, how do you know their existance? are you using a vector or something?

alos a little tip: don't use sqrt, its slow, since you don't need the real distance, get the square distance (same thing but without sqrt) and make the comparision "range" square (range*range)

sweeteye
13
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 3rd Dec 2011 16:17 Edited at: 3rd Dec 2011 18:15
Thank you both for the replies....interesting.... I will think about it some more....I am a bit hesitant to post a lengthy code because the last time I tried to post a snippet it did not work right and ended up not showing as a snippet...I used the code button and tried to enclose the code in between the words "" but I must have not done something right.. Maybe I will try again and hope I post it right.. I believe I see what I did wrong..... let me try a little test....Right now my opposing sprites are not hidden although I would like them to be.... This is just a small piece of code for a snippet test.... I will post more if it works right.
sweeteye
13
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 3rd Dec 2011 18:19 Edited at: 3rd Dec 2011 19:34
Here is the code I am using for my sprites
header:



Main:



Here is the code I am using for firing projectiles... I am sure there is a better way to do this but this is the first way I have found that is working for me....The projectiles are set up in the same manner as the unit sprites....

Main:



My code for the game is getting very lengthy and will be even more so... Eventually I will post it all .... or just upload the game... sprites and all...... Have a long ways to go.....
sweeteye
13
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 5th Dec 2011 12:50
I have your bit of code put in Wizz....and am able to compile the program....sprites are not firing yet....Will have to keep working with it...Not exactly sure what I am doing wrong....leaving something out.....

Login to post a reply

Server time is: 2024-11-19 06:29:00
Your offset time is: 2024-11-19 06:29:00