Ooops
November 24, 2020, 09:45:56 AM

Author Topic: [bb] 2D Click 'n' Move by Paul Murray [ 1+ years ago ]  (Read 602 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] 2D Click 'n' Move by Paul Murray [ 1+ years ago ]
« on: June 29, 2017, 12:28:39 AM »
Title : 2D Click 'n' Move
Author : Paul Murray
Posted : 1+ years ago

Description : 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...

<a href="http://www.sitesled.com/members/avengedsevenfold/clicknmove.zip" target="_blank">http://www.sitesled.com/members/avengedsevenfold/clicknmove.zip[/url]


Code :
Code: BlitzBasic
  1. ;************************************************
  2. ;       2D Click 'n' Move Example - written by Muzzer 2006     
  3. ;************************************************              
  4. ;                              
  5. ;       This code should give beginners an idea about    
  6. ;       moving an image to a position clicked on screen        
  7. ;
  8. ;       Experienced Blitz users may think it's written
  9. ;       incorrectly, and they're probably right.
  10. ;
  11. ;       Anyway, it works.
  12. ;
  13. ;       Soldier sprite ripped from Metal Slug and grabbed from http://www.emugifs.emuita.it
  14. ;
  15. ;************************************************
  16.  
  17.  
  18. AppTitle "2D Click 'n' Move Example"
  19.  
  20. Graphics 640,480,0,2
  21.  
  22. SetBuffer BackBuffer()
  23.  
  24. Global LastClickX = 400         ;these coords are the same as the image start position to
  25. Global LastClickY = 240         ;prevent it movingas soon as the program runs
  26.  
  27. Global ShipX = 400                      ;Image start postions
  28. Global ShipY = 240
  29.  
  30. imgMove = LoadImage("soldier.PNG")
  31. MaskImage imgMove,255,0,255
  32.  
  33. ;------------------
  34. ;Main Loop
  35. ;------------------
  36.  
  37. While Not KeyHit(1)
  38. Cls
  39.  
  40.         ;set the LastClick positions
  41.         If MouseDown(1) Then LastClickX = MouseX() LastClickY =  MouseY()       ;if left button is pressed, set the LastClick variables to the current MouseX() and MouseY() coordinates
  42.                        
  43.        
  44. ;DrawImage imgBack,0,0
  45. DrawImage imgMove,ShipX - 32, ShipY -32 ; the -32 offsets the image so the centre moves to the clicked point
  46.  
  47.         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
  48.        
  49.         If ShipX > LastClickX Then ShipX = ShipX - 2    ;if the image's X position is greater than LastClickX, move 2 units to the left
  50.                
  51.         If ShipY < LastClickY Then ShipY = ShipY + 2    ;if the image is lower than the LastClickY variable, move the image down
  52.        
  53.         If ShipY > LastClickY Then ShipY = ShipY - 2    ;if the image is higher than the LastCLickY variable, move the image up
  54.        
  55.        
  56.         ;------------------
  57.         ;Debug Text     - simply to display the coordinates on screen
  58.         ;------------------
  59.        
  60.  
  61.         Text 20,15, "Last ClickX = " + LastClickX       ;displays the last 'X' coordinates clicked
  62.         Text 20,30, "Last ClickY = " + LastClickY       ;displays the last 'Y' coordinates clicked
  63.        
  64.         ;constantly displays the Mouse coordinates
  65.         Text 20,45, "Mouse X = " + MouseX()                    
  66.         Text 20,60, "Mouse Y = " + MouseY()
  67.        
  68. Flip
  69.  
  70. Wend
  71.  
  72. End


Comments :


Jams(Posted 1+ years ago)

 Try this one:
Code: [Select]
Type point2
Field x#, y#
End Type

;Calculates the distance between 2 2D points.
Function point2_CalcDistance#( A.point2, B.point2 )
Local dX# = Abs( Ax - Bx )
Local dY# = Abs( Ay - By )
Return Sqr( (dX*dX) + (dY*dY) )
End Function

;Calculates the angle between 2 2D points.
Function point2_CalcAngle#( A.point2, B.point2 )
Local dX# = Ax - Bx
Local dY# = Ay - By
Return ATan2( dX#, dY# ) +180
End Function

Local Pos.point2 = New point2; Current position.
Local Target.point2 = New point2; Target position.

;Move position and target to the centre of the screen.
Posx = 320: Posy = 240
Targetx = 320: Targety = 240

Graphics 640,480, 0, 2
SetBuffer BackBuffer()
Repeat:Flip:Cls

If MouseHit( 1 )
;Update target with new mouse coords.
Targetx = MouseX()
Targety = MouseY()
EndIf

;Check to see if the sprite is not already 'at' Target.
If point2_CalcDistance( Pos, Target ) > 1
Angle# = point2_CalcAngle( Pos, Target ); Calculate the angle needed to go to Target.
Posx = Posx + Sin( Angle# );Move along x axis.
Posy = Posy + Cos( Angle# );Move along y axis.
EndIf

Oval( posx-5, posy-5, 10, 10 ); Draw sprite

Text 0,0,"Distance to target: "+ point2_CalcDistance( Pos, Target )
Text 0,15,"Angle to target: "+ point2_CalcAngle( Pos, Target )

Until KeyHit(1)
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.


octothorpe(Posted 1+ years ago)

 <div class="quote">    Local dX# = Ax - Bx   Local dY# = Ay - By   Return ATan2( dX#, dY# ) +180 </div>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.


Jams(Posted 1+ years ago)

 Ahh i thought there was something a little fishy about that +180, thanks octothorpe :) [/i]

 

SimplePortal 2.3.6 © 2008-2014, SimplePortal