Moving an object with tha mouse...

BlitzPlus Forums/BlitzPlus Programming/Moving an object with tha mouse...

Hi Valgar,

This is an example using a grid of 32*32, hopefully you'll be able to work it for your own needs.

graphics 640,480

type cursor_type
   field x,y
end type

global cursor.cursor_type = new cursor_type

type block_type
   field id,x,y
end type

global blocks.block_type

global block_im = createimage(32,32)
setbuffer imagebuffer(block_im)
color 64,64,255
rect 2,2,28,28
color 32,32,64
rect 4,4,24,24,false

global cursor_im = createimage(32,32)
setbuffer imagebuffer(cursor_im)
color 255,255,255
rect 0,0,32,32,false

Set_up_Blocks

; main prog

setbuffer backbuffer()

while not(keydown(1))
   cls
   display_blocks
   mx = mousex() : my = mousey()
   mx = (mx shr 5) shl 5 ; this is the bit that makes the 
   my = (my shr 5) shl 5 ; position a value divisible by 32 exactly
   ; to do the same with 100 try
   ; mx = int(mx / 100)*100
   ; my = int(my / 100)*100

   drawimage cursor_im,mx,my
   flip
wend

end

function Set_up_Blocks()
id = 0 
for y = 0 to 9
   for x = 0 to 9
      blocks = new block_type
      blocks\id = id
      blocks\x = x shl 5
      blocks\y = y shl 5
   next
next
end function

function Display_Blocks()
for blocks = each block_type
   drawimage block_im,blocks\x,blocks\y
next
end function



That should work (I'm in work so cannot test it) and should give you an idea of where to go. I hope it's what you're after.

Later,

Jes