Moving an object with tha mouse...
BlitzPlus Forums/BlitzPlus Programming/Moving an object with tha mouse...| 
 | ||
| I'm currently writing a code to move an image with the mouse(very simply...) But i want this image not under the perfect mouse coordinates but with "step" of 100 pixels! Something like "if i move the mouse right my image doesn't move right until my mousex coordinates are mouse x+100" I think this code may be very simple but i have a lot of headaches doing this -_-' I want to "simulate" a moving with the mouse within a grid of 100*100 pixel... | 
| 
 | ||
| gheight=480 ; resolution y gwidth=640 ; resolution x height#=gheight/14.0 ; number of tiles y width#=gwidth/20.0 ; number of tiles x Graphics gwidth,gheight ;Set graphics mode SetBuffer BackBuffer() ; Set buffer While Not KeyHit(1) ; Wait for escape button Cls ; Clear Screen xmouse=MouseX() ; Mouse coordinate on x axis ymouse=MouseY() ; Mouse coordinate on y axis mx=Floor(xmouse/width#)*width# ; calculate tile starting x my=Floor(ymouse/height#)*height# ; calculate tile starting y tx=Floor(xmouse/width#) ; calculate tile cursor is on x ty=Floor(ymouse/height#) ; calculate tile cursor is on y Rect mx,my, width#, height#, 0 ; draw tile Rect xmouse-2,ymouse-2, 5,5,1 ; draw mouse cursor Text 10,10,"Mouse coordinates: "+ xmouse + ", "+ ymouse Text 10,20,"Upperleft of current tile: "mx + ", " + my ; Text 10,30,"Tile: " + tx + ", " + ty ; Tile coordinate Flip ; flip buffer Wend EDIT: Just made it slightly more interesting as you can now enter the resolution you want and the program will make sure that there are always 8*6 squares on the screen... Sorry, I couldn't resist! Andy | 
| 
 | ||
| Many thanks! If do you only know the code i'm about to write to do the same thing.... I'm about to use xspeed and yspeed and compare the start to the end and vice-versa o_O' A waste of programmer-time... hehehe | 
| 
 | ||
| >Many thanks! You're welcome... Andy | 
| 
 | ||
| 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 | 
| 
 | ||
| Yes this works wonderful,both your's and Andy's examples have make me understand how to move object with various "step" :) I'm now trying to implement scrolling also but....hemm..... Here's my code,if you cut&paste in the editor you see wath i'm trying to explain... -_-' Graphics 800,600,32,1 SetBuffer BackBuffer() Global image=LoadImage("c:\tile1.bmp") ;a simple image of 100*100 Global sfondo=LoadImage("c:\tile2.bmp") ;a simple image of 100*100 Global or_x=0 Global or_y=0 Global bit=1 ;MAIN While Not KeyHit(1) Cls scrollmap() maptiles() draw_grid() Origin 0,0 ;with this i "reset" the origin to always see the "pointer"inside the screen muovi_tile() Flip Wend End ;END MAIN Function muovi_tile() xmouse=MouseX() ymouse=MouseY() mx=Int(xmouse/100) my=Int(ymouse/100) DrawImage image,mx*100,my*100 Text 100,100,mx*100 Text 200,100,my*100 End Function Function maptiles() tilex=0 tiley=0 For tiley=0 To 2000 Step 100 For tilex=0 To 2000 Step 100 DrawBlock sfondo,tilex,tiley Next Next End Function Function scrollmap() If MouseX()=0 Then ;this is the left border that the image can't go on If or_x<0 Then or_x=or_x+bit EndIf EndIf If MouseX()=799 Then ;this is the right border that the image can't go on If or_x>-2400 Then or_x=or_x-bit EndIf EndIf If MouseY()=0 Then ;this is the up border that the image cant'go on If or_y<0 Then or_y=or_y+bit EndIf EndIf If MouseY()=599 Then ;this is the down border that the image can't go on If or_y>-1800 Then or_y=or_y-bit EndIf EndIf Origin or_x,or_y ;the important piece.....i change the origin coordinates to make the scrolling End Function Function draw_grid() ;trace a grid with the command rect Color 255,255,255 ;the color of the rect,withe posizionex=0 posizioney=0 For posizionex=0 To 2000 Step 100 ;paint rect from 0 to 2000 with step of 100 pixels For posizioney=0 To 2000 Step 100 ;paint rect from 0 to 2000 with step of 100 pixels Rect posizionex,posizioney,100,100,0 Next Next End Function As you can see the image that are under the mouse pointer doesn't stay aligned with the grid when i start scrolling..... Also i use the command "origin x,y" to scroll the map,but if i want to insert other object the code became too complex because i have to use this command 10000 times....is there a better command to scroll a map or the only command is "origin"? | 
| 
 | ||
| i think i fixed if for you, you needed an extra Origin or_x,or_y 
Graphics 800,600,32,1 
SetBuffer BackBuffer() 
Global image=CreateImage(100,100);LoadImage("c:\tile1.bmp") ;a simple image of 100*100 
SetBuffer ImageBuffer(image)
Color 0,0,255
Rect 0,0,100,100
Global sfondo=CreateImage(100,100) ;a simple image of 100*100 
SetBuffer ImageBuffer(sfondo)
Color 255,0,0
Rect 0,0,100,100
SetBuffer BackBuffer()
Global or_x=0 
Global offset
Global or_y=0 
Global bit=1 
;MAIN 
While Not KeyHit(1) 
Cls 
scrollmap() 
maptiles() 
draw_grid() 
Origin 0,0 ;with this i "reset" the origin to always see the "pointer"inside the screen 
muovi_tile() 
Flip 
Wend 
End 
;END MAIN 
Function muovi_tile() 
xmouse=MouseX() 
ymouse=MouseY() 
mx=Int((xmouse-or_x)/100) 
my=Int((ymouse-or_y)/100) 
Origin or_x,or_y ;EXTRA Origin
DrawImage image,mx*100,my*100 
Text 100,100,mx*100 
Text 200,100,my*100 
End Function 
Function maptiles() 
tilex=0 
tiley=0 
For tiley=0 To 2000 Step 100 
For tilex=0 To 2000 Step 100 
DrawBlock sfondo,tilex,tiley 
Next 
Next 
End Function 
Function scrollmap() 
If MouseX()=0 Then ;this is the left border that the image can't go on 
If or_x<0 Then 
or_x=or_x+bit
EndIf 
EndIf 
If MouseX()=799 Then ;this is the right border that the image can't go on 
If or_x>-2400 Then 
or_x=or_x-bit 
EndIf 
EndIf 
If MouseY()=0 Then ;this is the up border that the image cant'go on 
If or_y<0 Then 
or_y=or_y+bit 
EndIf 
EndIf 
If MouseY()=599 Then ;this is the down border that the image can't go on 
If or_y>-1800 Then 
or_y=or_y-bit 
EndIf 
EndIf 
Origin or_x,or_y ;the important piece.....i change the origin coordinates to make the scrolling 
End Function 
Function draw_grid() ;trace a grid with the command rect 
Color 255,255,255 ;the color of the rect,withe 
posizionex=0 
posizioney=0 
For posizionex=0 To 2000 Step 100 ;paint rect from 0 to 2000 with step of 100 pixels 
For posizioney=0 To 2000 Step 100 ;paint rect from 0 to 2000 with step of 100 pixels 
Rect posizionex,posizioney,100,100,0 
Next 
Next 
End Function 
 | 
| 
 | ||
| Greathhhhhhhhhhhhhhhhhhhhh!!! I have tried myself to add an extra "Origin 0,0" but it seems that i add the wrong "origin" hehe I don't have tried to add"origin or_x,or_y"..... But how do you understood that an extra "origin or_x,or_y" should have fixed the problem? (i hope that i translate well what i write...) | 
| 
 | ||
| because as far as i could tell it was not alligned with the rest of the world and then i realized that you were reseting it and that why :) | 
| 
 | ||
| hehe :) |