#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Type kid(id As Int, x As Float, y As Float, width As Float, height As Float, moveDirection As Int, sprites(6) As B4XBitmap, frame As Int)
Private fx As JFX
Private MainForm As Form
Private cnv As B4XCanvas
Private kidsList As List
Private vpW, vpH As Float
Private myTimer As Timer
Private frames As Int
Private kidIndex As Int = 0
Private xui As XUI
Private speed As Float = 3
Private animationSpeed As Float = 6
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.Show
kidsList.Initialize
myTimer.Initialize("myTimer",1000/60)
cnv.Initialize(MainForm.RootPane)
vpW = MainForm.RootPane.Width
vpH = MainForm.RootPane.Height
'create here the girls
kidsList.Add(createGirl(vpW/2,vpH/2,0)) 'run right
' kidsList.Add(createGirl(vpW/2,vpH/2-(vpH*0.3),1)) 'run left
' kidsList.Add(createGirl(vpW/2,vpH/2+(vpH*0.3),1)) 'run left
myTimer.Enabled = True
End Sub
Sub createGirl(x As Float, y As Float, direction As Int) As kid
Dim girl As kid
girl.Initialize
girl.id = kidIndex 'uniqe id
Dim fullsprite As B4XBitmap = xui.LoadBitmap(File.DirAssets,"spritestrip.png")
Dim bmpwidth As Float = fullsprite.Width/girl.sprites.Length
Dim bmpTop As Float
If direction = 1 Then bmpTop = fullsprite.Height/2 Else bmpTop = 0
For i = 0 To girl.sprites.Length-1
girl.sprites(i) = fullsprite.Crop(bmpwidth*i,bmpTop,bmpwidth,fullsprite.Height/2)
Next
girl.width = vpW*0.2
girl.height = girl.width
girl.x = x-(girl.width/2)
girl.y = y-(girl.height/2)
girl.moveDirection = direction '0=right, 1=left
girl.frame = 0
kidIndex = kidIndex + 1
Return girl
End Sub
Sub myTimer_Tick
frames = frames+1
clearcanvas
moveallKids
drawAllKids
End Sub
Sub clearcanvas
cnv.ClearRect(cnv.TargetRect)
End Sub
Sub moveallKids
For Each child As kid In kidsList
If child.moveDirection = 1 Then
child.x = child.x - speed
If child.x < -child.width Then child.x = vpW+child.width
Else
child.x = child.x + speed
If child.x > vpW Then child.x = -child.width
End If
Next
End Sub
Sub drawAllKids
For Each child As kid In kidsList
If frames Mod animationSpeed = 0 Then child.frame = (child.frame+1) Mod child.sprites.Length
cnv.DrawBitmap(child.sprites(child.frame),returnRect(child.x,child.y,child.width,child.height))
Next
End Sub
Sub returnRect(x As Float, y As Float, w As Float, h As Float) As B4XRect
Dim r As B4XRect
r.Initialize(x,y,x+w,y+h)
Return r
End Sub
'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub