Code archives/Miscellaneous/2D Click 'n' Move
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Very basic example of clicking a position on the screen and having an image move to that position automatically. Download the *.bb file and image used in the example here... http://www.sitesled.com/members/avengedsevenfold/clicknmove.zip | |||||
;************************************************ ; 2D Click 'n' Move Example - written by Muzzer 2006 ;************************************************ ; ; This code should give beginners an idea about ; moving an image to a position clicked on screen ; ; Experienced Blitz users may think it's written ; incorrectly, and they're probably right. ; ; Anyway, it works. ; ; Soldier sprite ripped from Metal Slug and grabbed from http://www.emugifs.emuita.it ; ;************************************************ AppTitle "2D Click 'n' Move Example" Graphics 640,480,0,2 SetBuffer BackBuffer() Global LastClickX = 400 ;these coords are the same as the image start position to Global LastClickY = 240 ;prevent it movingas soon as the program runs Global ShipX = 400 ;Image start postions Global ShipY = 240 imgMove = LoadImage("soldier.PNG") MaskImage imgMove,255,0,255 ;------------------ ;Main Loop ;------------------ While Not KeyHit(1) Cls ;set the LastClick positions If MouseDown(1) Then LastClickX = MouseX() LastClickY = MouseY() ;if left button is pressed, set the LastClick variables to the current MouseX() and MouseY() coordinates ;DrawImage imgBack,0,0 DrawImage imgMove,ShipX - 32, ShipY -32 ; the -32 offsets the image so the centre moves to the clicked point If ShipX < LastClickX Then ShipX = ShipX + 2 ;if the image's X position is less than the LastClickX coord we set, move 2 units to the right If ShipX > LastClickX Then ShipX = ShipX - 2 ;if the image's X position is greater than LastClickX, move 2 units to the left If ShipY < LastClickY Then ShipY = ShipY + 2 ;if the image is lower than the LastClickY variable, move the image down If ShipY > LastClickY Then ShipY = ShipY - 2 ;if the image is higher than the LastCLickY variable, move the image up ;------------------ ;Debug Text - simply to display the coordinates on screen ;------------------ Text 20,15, "Last ClickX = " + LastClickX ;displays the last 'X' coordinates clicked Text 20,30, "Last ClickY = " + LastClickY ;displays the last 'Y' coordinates clicked ;constantly displays the Mouse coordinates Text 20,45, "Mouse X = " + MouseX() Text 20,60, "Mouse Y = " + MouseY() Flip Wend End |
Comments
| ||
Try this one: Uses sin & cos to give more accurate movement. Also has 2 very useful functions to calculate the distance between two points, and to calculate the angle between 2 points. |
| ||
Local dX# = A\x - B\x Local dY# = A\y - B\y Return ATan2( dX#, dY# ) +180 If you subtract A from B instead, you won't need to add 180. ;) Also, you might want to multiply the results of Sin() and Cos() by a constant (SPEED) to make it obvious how to change the speed. |
| ||
Ahh i thought there was something a little fishy about that +180, thanks octothorpe :) |
Code Archives Forum