Home
Products
Community
Manuals
Contact
Login or Signup

Code archives/Graphics/Dragable Image

This code has been declared by its author to be Public Domain code.

Download source code

Dragable Image by n8r2k(Posted 1+ years ago)
Just click the img and drag it. Sorry this isn't commented. Will get to that soon. This is now on http://n8r2k.deviousbytes.com/
Graphics 800,600,16,2 
AutoMidHandle True
HidePointer()

Global mouse = CreateImage(10,10)
Global img = CreateImage(100,20)
Global imgx = 100
Global imgy = 100
Global distx = 0
Global disty = 0

SetBuffer ImageBuffer(mouse)
Color 100,100,100
Rect 0,0,10,10,1
SetBuffer ImageBuffer(img)
Color 100,0,0
Rect 0,0,100,20,1
SetBuffer BackBuffer()

While Not KeyHit(1)
Cls 
DrawImage img,imgx,imgy
DrawImage mouse,MouseX(),MouseY()
If ImagesCollide(mouse,MouseX(),MouseY(),0,img,imgx,imgy,0)
	distx = imgx - MouseX()
	disty = imgy - MouseY()
	DragImage()
EndIf
Flip
Wend 

Function DragImage()
While MouseDown(1)
Cls 
DrawImage img,imgx,imgy
DrawImage mouse,MouseX(),MouseY()

imgx = MouseX() + distx
imgy = MouseY() + disty
Flip 
Wend
End Function

Comments

Rck(Posted 1+ years ago)
I suggest a code inversion to remove a 1 frame delay

the code has

Function DragImage()
While MouseDown(1)
Cls 
DrawImage img,imgx,imgy
DrawImage mouse,MouseX(),MouseY()

imgx = MouseX() + distx
imgy = MouseY() + disty
Flip 
Wend
End Function


I suggest

Function DragImage()
While MouseDown(1)
Cls
imgx = MouseX() + distx
imgy = MouseY() + disty

DrawImage img,imgx,imgy
DrawImage mouse,MouseX(),MouseY()
Flip 
Wend
End Function



With the final code being




Code Archives Forum