How can I move sprites with a finger gesture?

Just starting out? Need help? Post your questions and find answers here.
AZJIO
Posts: 93
Joined: Wed Dec 14, 2022 1:13 pm

How can I move sprites with a finger gesture?

Post by AZJIO »

I want to move a cube right, left, up, down with a swipe of my finger on the screen. How do I do this?
Is there a way to do debugging on a computer? I get the message "No touchscreen device detected". Can I temporarily make use of the mouse? Do I need to buy a separate touchpad device to run the tests.
In the 2048 game, it would be more convenient not to press buttons, but to make gestures.

Code: Select all

EnableExplicit

#Square = 0

Global DspSize, x, y, s

Define DspWidth, DspHeight

ExamineDesktops()
DspWidth = DesktopWidth(0)
DspHeight = DesktopHeight(0)
If DspHeight > DspWidth
	DspSize = DspWidth
Else
	DspSize = DspHeight
EndIf
s = DspSize / 4


OpenScreen(DspSize, DspSize, 32, "")
x = DspSize / 2 - SpriteWidth(#Square)
y = DspSize / 2 - SpriteHeight(#Square)
DisplaySprite(#Square, x, y)
ZoomSprite(#Square, s, s)

Procedure RenderFrame()
	Protected k, DeltaX, DeltaY, cnt
	Static SpriteFinger = -1
	Static x, y
	
	ClearScreen($3F3F3F)
	
	If ExamineTouchScreen()
		
		For k = 0 To 4
			If TouchScreenPushed(k) And SpriteFinger = -1
				SpriteFinger = k
				Break
			EndIf
		Next
		
		If SpriteFinger <> -1 And Not TouchScreenPushed(k)
			DeltaX = TouchDeltaX(SpriteFinger)
			DeltaY = TouchDeltaY(SpriteFinger)
			Debug Str(DeltaX) + ":" + Str(DeltaY)
			If Abs(DeltaX) > Abs(DeltaY)
				If DeltaX > 0
					x = s * 3
				Else
					x = 0
				EndIf
			Else
				If DeltaY > 0
					y = 0
				Else
					y = s * 3
				EndIf
			EndIf
			SpriteFinger = -1
		EndIf
		
		DisplaySprite(#Square, x, y)
		
		FlipBuffers()
	Else
		Debug "No touchscreen device detected"
	EndIf
	
EndProcedure

Procedure Loading(Type, Filename$, ObjectId)
	Static NbLoadedElements
	
	NbLoadedElements+1
	If NbLoadedElements = 1
		FlipBuffers()
	EndIf
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
	Debug Filename$ + ": loading error"
EndProcedure

BindEvent(#PB_Event_Loading, @Loading())
BindEvent(#PB_Event_LoadingError, @LoadingError())
BindEvent(#PB_Event_RenderFrame, @RenderFrame())


LoadSprite(#Square, "data/favicon.png")
Example2

Code: Select all


EnableExplicit

#Square = 0
#Finger = 1
#Circle = 2
#Font = 0
Structure Point
	x.i
	y.i
EndStructure

Global DspSize, x, y, s, x0, y0, x1, y1, Tracking = 1
Global IsLoadFont
Global SpriteFinger = -1
Global SpriteFingerOld = 0
Global NewList CoordinatesList.Point()

Define DspWidth, DspHeight, IsCreateSprite

ExamineDesktops()
DspWidth = DesktopWidth(0)
DspHeight = DesktopHeight(0)
If DspHeight > DspWidth
	DspSize = DspWidth
Else
	DspSize = DspHeight
EndIf
s = DspSize / 4



Procedure RenderFrame()
	Protected i, DeltaX, DeltaY
	
	ClearScreen($3F3F3F)
	
	If ExamineTouchScreen() ; запрос данных тачпада
		
; 		постоянно сканируем нажатие пальцев
		For i = 0 To 4
			If TouchScreenPushed(i)
				SpriteFinger = i
				DisplaySprite(#Circle, TouchX(SpriteFinger) - s/4, TouchY(SpriteFinger) - s/4)
				Break ; нет смысла отлеживать если SpriteFinger уже не даст отработать условие, поэтому выпрыгиваем, чтобы не делать лишних шагов цикла.
			EndIf
		Next
		; рисуем какой палец, только при изменении
		If SpriteFinger <> SpriteFingerOld
			SpriteFingerOld = SpriteFinger
			If StartDrawing(SpriteOutput(#Finger))
				Box(0, 0, s, s, $552200)
				If IsLoadFont
					DrawingFont(FontID(#Font))
				EndIf
				DrawText(2, 2, Str(SpriteFinger), $00FFFF, $552200)
				StopDrawing()
			EndIf
		EndIf
			
		If SpriteFinger <> -1 And TouchScreenPushed(SpriteFinger) ; пока палец нажат на тачпад
			Tracking = 0
			; сохраняем стартовые координаты нажатия
			If ListSize(CoordinatesList()) < 2 ; ограничиваемся двумя координатами, координаты конца переписываем несколько раз
				AddElement(CoordinatesList())
			EndIf
			CoordinatesList()\x = TouchX(SpriteFinger)
			CoordinatesList()\y = TouchY(SpriteFinger)
		Else
            SpriteFinger = -1
; 			SpriteFinger = -1 ; Сбрасываем палец, когда отпущен
			If Tracking = 0
				Tracking = 1 ; Сбрасываем отслеживание
				LastElement(CoordinatesList())
				x1 = CoordinatesList()\x ; координаты начала движения пальца
				y1 = CoordinatesList()\y
				FirstElement(CoordinatesList())
				; Вычетаем координаты конца движения пальца, чтобы получить длину пути
				x1 - CoordinatesList()\x
				y1 - CoordinatesList()\y
				ClearList(CoordinatesList()) ; Очищаем координаты
				; Так как отладка не работает, то рисуем текст отлабки на спрайте
				If StartDrawing(SpriteOutput(#Square))
					Box(0, 0, s, s, $FF8800)
					Box(2, 2, s - 4, s - 4, $994400)
					DrawText(2, 2, "E " + Str(x1) + " : " + Str(y1), $00FFFF, $994400)
					StopDrawing()
				EndIf
				If Abs(x1) > Abs(y1) ; DeltaX и DeltaY
					If x1 > 0
						x = s * 3
					Else
						x = 0
					EndIf
				Else
					If y1 > 0
						y = s * 3
					Else
						y = 0
					EndIf
				EndIf
			EndIf
		EndIf
		
		DisplaySprite(#Square, x, y)
		DisplaySprite(#Finger, 0, 0)
		
		FlipBuffers()
		Debug "No touchscreen device detected"
	EndIf
	
EndProcedure

Procedure Loading(Type, Filename$, ObjectId)
	Static NbLoadedElements
	
	NbLoadedElements+1
	If NbLoadedElements = 1
		FlipBuffers()
	EndIf
EndProcedure

Procedure LoadingError(Type, Filename$, ObjectId)
	Debug Filename$ + ": loading error"
EndProcedure

If OpenScreen(DspSize, DspSize, 32, "")
	
	If LoadFont(#Font, "Roboto", 33, #PB_Font_Bold)
		IsLoadFont = 1
	EndIf
	If CreateSprite(#Circle, s / 2, s / 2)
		If StartDrawing(SpriteOutput(#Circle))
			Circle(s/4, s / 4, s / 4, $FF00FF) 
			StopDrawing()
		EndIf
	EndIf
	If CreateSprite(#Square, s, s)
		If StartDrawing(SpriteOutput(#Square))
			Box(0, 0, s, s, $FF8800)
			Box(2, 2, s - 4, s - 4, $994400)
			DrawText(2, 2, "--", $00FFFF, $994400)
			StopDrawing()
			IsCreateSprite = 1
		EndIf
	EndIf
	If CreateSprite(#Finger, s / 2, s / 2)
		If StartDrawing(SpriteOutput(#Finger))
			Box(0, 0, s, s, $552200)
			If IsLoadFont
				DrawingFont(FontID(#Font))
			EndIf
			DrawText(2, 2, "--", $00FFFF, $552200)
			StopDrawing()
		EndIf
	EndIf
	If Not IsCreateSprite
		LoadSprite(#Square, "data/favicon.png")
		ZoomSprite(#Square, s, s)
	EndIf
	x = DspSize / 2 - SpriteWidth(#Square)
	y = DspSize / 2 - SpriteHeight(#Square)
	
	
	BindEvent(#PB_Event_Loading, @Loading())
	BindEvent(#PB_Event_LoadingError, @LoadingError())
	BindEvent(#PB_Event_RenderFrame, @RenderFrame())
	
	DisplaySprite(#Square, x, y)
	DisplaySprite(#Finger, 0, 0)
	FlipBuffers()
EndIf