Android Question A moving graphic

mearleybrook

Member
Licensed User
Longtime User
Might I ask for a little guidance on this matter. a hint? an approach?
I am working on a small app to help my granddaugther with reading. It has some graphics of letters in a panel at the top of the screen and at the bottom. Upon her selection I can find the graphic in the lower panel and calculate where it needs to be placed in the upper panel. But -- I want it to drift from the one position to the other in a smooth curved and looping path , most importantly the final destination not being evident until the last portion of the drift, and the drift not being predictable as just a repeat of previous path.
What basic design approach should I be looking into please?
 

Tom Christman

Active Member
Licensed User
Longtime User
Check out
"rotation-and-moving.11186/#content" with code examples from Klaus and thedesolatesoul

The following is a modification of thedesolatesoul code where your letter would be the "sprite" referred to in the code. Hope this helps!
Tom


B4X:
#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
 
Upvote 0
Top