The last code snip was the one with the mouse instead of arrow keys. I decided to change it to be like Uncle Sams example that doesn't snap to the grid.
The following uses what is known as a switch (either 0=Off or 1=On) to determine if the box will follow the mouse or not. It checks for a mouse button being hit then checks if the mouse is on the sprite. If it is on the sprite it then checks if the Move switch is off, offsets the sprite relative to the mouse, and realigns the sprite to prevent any "jumping" and turns the Move switch on. Once you let go of the mouse button it'll turn off the Move switch, realign the sprite to prevent "jumping" again, and reset the offset to zeros.
The lag you mentioned was because of the drawing of the grid every loop. I took it out of the DO/LOOP and grabbed an image of the completed grid so this version is lag free.
` Set sync rate to max and turn it on
sync rate 0
sync on
set window size 800,600
` Create a box
ink rgb(0,100,0),0
box 0,0,100,100
` Grab the image
get image 1,0,0,100,100,1
` Make a bitmap (#1 so the user doesn't see it)
create bitmap 1,800,600
` Change color for grid drawing
ink rgb(100,100,100),0
` Show the grid 32x32
for y=0 to screen height() step 32
for x=0 to screen width() step 32
` Draw the lines of the grid
line x,y,x+32,y
line x,y,x,y+32
next x
next y
` Grab the grid image
get image 2,0,0,800,600,1
` Get rid of the bitmap
delete bitmap 1
` Initial sprite creation (so no errors are seen)
sprite 1,PlayerX,PlayerY,1
do
` Show the grid image
paste image 2,0,0
` Check for a mouse button being clicked
if mouseclick()
` Check if the mouse is over sprite number 1
if mousex()=>sprite x(1) and mousey()=>sprite y(1) and mousex()<=sprite x(1)+sprite width(1) and mousey()<=sprite y(1)+sprite height(1)
` Check if the Move switch is off
if Move=0
` Offset the sprite to wherever the mouse is at
offset sprite 1,mousex()-sprite x(1),mousey()-sprite y(1)
` Increase the players coordinates to match offset (so no "jumping" occurs)
inc PlayerX,sprite offset x(1)
inc PlayerY,sprite offset y(1)
endif
` Turn the Move switch on
Move=1
endif
endif
` Check for mouse button not being clicked
if mouseclick()=0
` Turn Move switch off
Move=0
` Decrease the players coordinates to match no offset (so no "jumping" occurs)
dec PlayerX,sprite offset x(1)
dec PlayerY,sprite offset y(1)
` Reset offset to normal (no offset)
offset sprite 1,0,0
endif
` Check if Move switch is on
if Move=1
` Move player coordinates with the mouse
PlayerX=mousex()
PlayerY=mousey()
endif
` Show the player
sprite 1,PlayerX,PlayerY,1
` Show player coordinates
ink rgb(255,255,255),0
text 0,0,"PlayerX = "+str$(PlayerX)
text 0,20,"PlayerY = "+str$(PlayerY)
text 0,40,"You are in edit mode."
` Testing
text 0,60,"Move = "+str$(Move)
text 0,80,"MouseX = "+str$(Mousex())
text 0,100,"MouseY = "+str$(mousey())
text 0,120,"X Offset = "+str$(sprite offset x(1))
text 0,140,"Y Offset = "+str$(sprite offset y(1))
text 0,160,"mousex()-sprite x(1) = "+str$(mousex()-sprite x(1))
text 0,180,"mousey()-sprite y(1) = "+str$(mousey()-sprite y(1))
` Update screen
sync
loop
