Posted: 8th Dec 2022 23:31
Ok so I am trying something new and different and practicing new ways of doing things.

I have a grid of sprites, marbles lined up together on a grid and I can move each one to my pointer if selected, but I need to limit how far I can move them
due to there past positions and cell size. I am trying one way and it works but only for one side of its x pos and not the other.

Here is a example of my code.

+ Code Snippet
 if gride[x,y].Selected_1=1 and GetPointerState()=1
gride[x,y].Newposx=GetSpriteXByOffset(gride[x,y].sprite)
 gride[x,y].Newposy=GetSpriteYByOffset(gride[x,y].sprite)
if gride[x,y].Newposx<gride[x,y].oldposx+CELL_SIZE  then SetSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))
SetSpriteDepth(gride[x,y].sprite,0)
endif


I tried many things, even this but it does not work for both sides.

if gride[x,y].Newposx<gride[x,y].oldposx+CELL_SIZE or gride[x,y].Newposx>gride[x,y].oldposx+CELL_SIZE then SetSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))

I fell like it is so simple but I do not see it.

Thanks for any help of understanding on this.

In case anyone is wondering I am setting up different games in my match three game and this is one of the other games. You have to move each marble over the pets then they drop on them and explode. but they have to be limited to only the next slot over from there own position.

This is the whole function I created if it helps.

+ Code Snippet
	function MoveGrid()
         for y = 0 to BOARD_SIZE_Y
           for x = 0 to BOARD_SIZE_X
			   
			    if ( GetPointerPressed ( ) = 1 )
                hit = GetSpriteHitTest (gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ) )
                if hit=1 then gride[x,y].Selected_1=1
                gride[x,y].oldposx=GetSpriteXByOffset(gride[x,y].sprite)
                gride[x,y].oldposy=GetSpriteYByOffset(gride[x,y].sprite)
               endif 
               
                 if gride[x,y].Selected_1=1 and GetPointerState()=1
                 gride[x,y].Newposx=GetSpriteXByOffset(gride[x,y].sprite)
                 gride[x,y].Newposy=GetSpriteYByOffset(gride[x,y].sprite)
				 if gride[x,y].Newposx&lt;gride[x,y].oldposx+CELL_SIZE  then SetSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))
				 SetSpriteDepth(gride[x,y].sprite,0)
			    endif
				 
                  if gride[x,y].Selected_1=1 and GetPointerReleased()=1 
				  gride[x,y].Selected_1=0
				  SetSpritePositionByOffset(gride[x,y].sprite,gride[x,y].oldposx,gride[x,y].oldposy)
			     endif
				  
				 if gride[x,y].Selected_1=0 
				 SetSpriteDepth(gride[x,y].sprite,1)
			    endif
				  
        next x
    next y
endfunction
Posted: 9th Dec 2022 2:56
Im not sure if this is what you are after but i did notice that getspritehittest didnt work but getspritehit did for some reason
+ Code Snippet
// Project: testcode 
// Created: 2022-12-09

// show all errors
SetErrorMode(2)

// set window properties
SetWindowTitle( "testcode" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window

// set display properties
SetVirtualResolution( 1024, 768 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
#constant BOARD_SIZE_Y 10
#constant BOARD_SIZE_X 10
type gridtype
	oldposx as integer
	oldposy as integer
	newposx as integer
	newposy as integer
	sprite as integer
	selected as integer
endtype
#constant CELL_SIZE=10
global gride as gridtype[10,10]	 
for y = 0 to BOARD_SIZE_Y
	for x = 0 to BOARD_SIZE_X
		gride[x,y].sprite=createsprite(0):setSpritesize(gride[x,y].sprite,10,10)
		gride[x,y].selected=0:gride[x,y].oldposx=x*50:gride[x,y].oldposy=y*50
		SetSpritePositionByOffset(gride[x,y].sprite,gride[x,y].oldposx,gride[x,y].oldposy)
		
	next x
next y	
x=0:y=0
do
	
	 if gride[x,y].Selected=1 and GetPointerState()=1
gride[x,y].Newposx=GetSpriteXByOffset(gride[x,y].sprite)
 gride[x,y].Newposy=GetSpriteYByOffset(gride[x,y].sprite)
if gride[x,y].Newposx&lt;gride[x,y].oldposx+CELL_SIZE  then SetSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))
SetSpriteDepth(gride[x,y].sprite,0)
endif
    MoveGrid()

    Print( ScreenFPS() )
    Sync()
loop

	function MoveGrid()
         p as point
         for y = 0 to BOARD_SIZE_Y
           for x = 0 to BOARD_SIZE_X
			    
			    if ( GetPointerPressed ( ) = 1 )
                hit = GetSpriteHit(ScreenToWorldX(GetPointerX ( )),ScreenToWorldY( GetPointerY ( ) ))
                p=lookuptile(hit)
                if hit&gt;0 then gride[p.x,p.y].selected=1
                //if hit=1 then gride[x,y].Selected=1
                gride[x,y].oldposx=GetSpriteXByOffset(gride[x,y].sprite)
                gride[x,y].oldposy=GetSpriteYByOffset(gride[x,y].sprite)
               endif 
               
                 if gride[x,y].Selected=1 and GetPointerState()=1
                 gride[x,y].Newposx=GetSpriteXByOffset(gride[x,y].sprite)
                 gride[x,y].Newposy=GetSpriteYByOffset(gride[x,y].sprite)
				 if gride[x,y].Newposx&lt;gride[x,y].oldposx+CELL_SIZE  then SetSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))
				 SetSpriteDepth(gride[x,y].sprite,0)
			    endif
				 
                  if gride[x,y].Selected=1 and GetPointerReleased()=1 
				  gride[x,y].Selected=0
				  SetSpritePositionByOffset(gride[x,y].sprite,gride[x,y].oldposx,gride[x,y].oldposy)
			     endif
				  
				 if gride[x,y].Selected=0 
				 SetSpriteDepth(gride[x,y].sprite,1)
			    endif
				  
				  
				  print(hit)
        next x
    next y
endfunction

type point 
	x as integer
	y as integer
endtype	

function lookuptile(hit)
	p as point:p.x=0:p.y=0
	for y = 0 to BOARD_SIZE_Y
           for x = 0 to BOARD_SIZE_X
			   if gride[x,y].sprite=hit 
				   p.x=x
				   p.y=y
				   exitfunction p
				endif
			next x
	next y
endfunction p				
			

The code above allows moving they just dont snap into place
Posted: 9th Dec 2022 13:35
Hi fubarpk

Thanks for looking.

It made no difference.

What I am looking for is limited movement for the marbles. Do you see how they can not move to the right past there neighbor? that is how I need it for left to right to up and down. They can move, but only only slot.

Let me say you put together a whole example, that was nice.
Posted: 9th Dec 2022 16:29
Taking a guess here based on your first snippet, your method only works to the RIGHT because that's a positive quadrant. You should check the absolute values instead, then the same check can work for both directions

Try something like this
+ Code Snippet
if gride[x,y].Selected_1=1 and GetPointerState()=1
	gride[x,y].NewposX = GetSpriteXByOffset(gride[x,y].sprite)
	gride[x,y].NewposY = GetSpriteYByOffset(gride[x,y].sprite)
	
	if abs(gride[x,y].NewposX - gride[x,y].oldposX) &lt;= CELL_SIZE  AND  abs(gride[x,y].NewposY - gride[x,y].oldposY) &lt;= CELL_SIZE
		setSpritePositionByOffset(gride[x,y].sprite, GetPointerX ( ), GetPointerY ( ))
	endif
	SetSpriteDepth(gride[x,y].sprite,0)
endif
Posted: 9th Dec 2022 16:55
You should check the absolute values instead


Yes this is true, Also it works and I can now implement the new mini game, thank you for the great answer.