#Region Module Attributes
#FullScreen: False
#IncludeTitle: True
#ApplicationLabel: moverdemo
#VersionCode: 1
#VersionName: 1
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
'Activity module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Type PointType(x As Int,y As Int)
Dim Touch As PointType
Dim dx As Int
Dim dy As Int
Dim Pixel As Int: Pixel= 10 'Changing this Value changes Sprite Speed
Dim timer1 As Timer
Dim Magnitude As Double
Dim Angle As Double
Dim IsMoving As Boolean
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
Dim sprite As ImageView
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Draw a Demo Sprite
sprite.initialize("sprite")
Activity.AddView(sprite,50%x,50%y,32,32)
sprite.Color=Colors.red
Magnitude = 0
Angle = 0
IsMoving = False
'Setup Timer
timer1.Initialize("Timer1",100) 'Changing Interval changes Sprite speed!
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub Activity_Touch (Action As Int, X As Float, Y As Float)
Select Case Action
Case Activity.ACTION_DOWN
Touch.X = X
Touch.Y = Y
timer1.enabled = True
Case Activity.ACTION_UP
'Timer1.enabled = False
Case Else
End Select
Magnitude = Sqrt((Touch.X - sprite.Left)*(Touch.X - sprite.Left) + (Touch.Y-sprite.Top)*(Touch.Y-sprite.Top))
Angle = ATan((Touch.Y-sprite.Top)/(Touch.X-sprite.Left))
Log(Angle*180/cPI)
End Sub
Sub Timer1_Tick
Dim vx As Int
Dim vy As Int
Magnitude = Sqrt((Touch.x - sprite.Left)*(Touch.x - sprite.Left) + (Touch.y-sprite.Top)*(Touch.y-sprite.Top))
If Touch.x < sprite.Left Then
Angle = cPI - ATan(-(Touch.y-sprite.Top)/(Touch.x-sprite.Left))
Else
Angle = ATan((Touch.y-sprite.Top)/(Touch.x-sprite.Left))
End If
Pixel = Rnd(-10,10)
vx = (Magnitude/Pixel) * Cos(Angle)
Pixel = Rnd(-10,10)
vy = (Magnitude/Pixel) * Sin(Angle)
'Log("vx: " & vx & " vy:" & vy)
sprite.Left=sprite.Left + vx
If sprite.Left<0 Then sprite.Left=0
If sprite.Left>(100%x-sprite.Width) Then sprite.Left=(100%x-sprite.Width)
sprite.Top=sprite.Top + vy
If sprite.Top<0 Then sprite.Top=0
If sprite.Top>(100%y-sprite.height) Then sprite.Top=(100%y-sprite.height)
If sprite.Left = Touch.x AND sprite.Top = Touch.y Then
timer1.Enabled=False
End If
End Sub