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.

2D All the way! / mouseclick sprite select and movement

Author
Message
sweeteye
12
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 27th Aug 2013 22:26
I am looking for a tutorial or a code example of how to select a sprite with a mouse click and then have the sprite move to a location selected by clicking the mouse pointer on the map.I do not want to select and drag the sprite.I am using dark GDK.Any help would be greatly appreciated.....
sweeteye
12
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 30th Aug 2013 21:43 Edited at: 30th Aug 2013 21:46
Have been searching the web for code examples and tutorials that might apply to the project I am working on.Not really finding the right information that will apply,only bits and pieces.Plus the code has to work with dark gdk of course.My 2D wargame is kind of at a standstill until I can better implement the movement of friendly units by mouseclick and refine the system of firing.I started the project a couple of years ago but ended up putting it aside because of lack of progress with refining the code.Right now the map is just a backdrop although I have made some progress on putting in a map made from tiles.I have a general idea of what needs to be done for getting units to move by mouseclick but I just do not have the proper coding....

Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 8th Sep 2013 04:58
Click the screen. See if mouse coordinates are within the boundaries of any of your sprites. If so, mark that sprite as selected. Then when you click somewhere on the map that isn't selecting a sprite, there is your target coordinates. Use atanfull to compute the angle between the sprite's current position and its destination. Use that angle to move towards the destination. Stop moving the sprite once target is reached.

sweeteye
12
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 8th Sep 2013 07:04
Thanks for taking the time to respond...have been waiting many days for any sort of response...Unfortunately I do not understand your explanation, though I assume it would work.If I click on the game screen nothing happens....how do I get a readout of the coordinates?I have no idea what atanfull means...sounds like a mathemamatical procedure....either that or it means getting a sunburn at the beach.......Thanks again though...
Phaelax
DBPro Master
20
Years of Service
User Offline
Joined: 16th Apr 2003
Location: Metropia
Posted: 9th Sep 2013 16:14 Edited at: 9th Sep 2013 16:17
Press F1 to open the DB help where you can find about atanfull. It is a math function. It takes two prameters, an X and Y value. These values are the difference between the coordinates of point A and point B. So if your unit is standing at A and you want to move to B: atanfull(B.x-A.x, B.y-A.y)

That will return an angle between the two points. To move your unit in that direction is simple trig:

unit.x = unit.x + sin(angle)*speed
unit.y = unit.y + cos(angle)*speed


Here's an example demonstrating that:





If you're curious about the math behind atanful, you can view my own function of it here. Basically what the parameters of the function do is create a directional vector of point A to point B.


mousex() and mousey() will get you the screen coordinates of your mouse cursor.

You can use this function to see if the mouse is within a certain area (a bounding box)



Here's the basic structure for selecting units:


sweeteye
12
Years of Service
User Offline
Joined: 18th Nov 2011
Location: OHIO USA
Posted: 9th Sep 2013 19:49
Thank you for the explanation...was never too good in math but am capable of working with numbers to some degree.My game development kind of ground to a halt since I hit this snag with moving sprites with a mouse click....At least now your help will get me rolling again...Thanks a million......

Login to post a reply

Server time is: 2024-03-01 01:03:27
Your offset time is: 2024-03-01 01:03:27