Android Question Smple sprite animation (left-right) and vis versa

GMan

Well-Known Member
Licensed User
Longtime User
I want to display a simple sprite moving from left to right (etc.) on the screen.
Which lib is useful for this ?
I thougth some years ago we had a simple sprite demo as i need, but couldnt find it
 

GMan

Well-Known Member
Licensed User
Longtime User
Checked them all shortly, but not what i need.
i think i remember ;-) that the demo was a little android robot moving in the top of the screen
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Klaus,
Thx a lot, could be nice BUT i need a loaded image that should be displayed.
Get nuts about this :confused:
Couldnt not so difficult to move an image from left to rigth and back, but i am absolut no grafic-"freak".
 
Upvote 0

Cableguy

Expert
Licensed User
Longtime User
Do you want the user to control the sprite, or do you and it just to go from one side to the other without user interference?
For the second one, try animationplus lib...
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Klaus,
i found the following code which works (so far)
B4X:
Sub movegraph
  For l = 250 To 1 Step -1
   ImageView2.Left = l
   DoEvents
Next

For l = 1 To 250
  ImageView2.Left = l
  DoEvents
Next

i called movegraph from the Activity_Create-Part and in the Activity_Resume.

But: it only moves twice from right to left and back, not always.

The image is a simple transparent .png in an ImageView (on a panel) loaded through the Designer.
Also the graph is moved by the App, not through the user
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
You should use a timer and do the positionchange in the timer tick. You dont need doevents (most probably it is not the correct solution)
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Manfred,
thx for that hint - but, it's like in a real language: without training you lost a lot after a while ;-)

I created a timer like this:

B4X:
    Timer1.Initialize("Timer1",1000)
    Timer1.Enabled = True
    Timer1_Tick

The code inner the Timer1 looks like this:
B4X:
Sub Timer1_Tick
    ' move ball left
       For l = 250 To 1 Step -1
           ImageView2.Left = l
    Next
' now move ball back to original position
    For l = 1 To 250
        ImageView2.Left = l
    Next
End Sub

But it won't move....
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
One solution using a timer to show the idea.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private WImageView1 As ImageView
    Private position As Int
    Private DirectionLeft As Boolean
    Private t As Timer
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    position = WImageView1.Left
    DirectionLeft = False
    t.Initialize("Timer",10)
    t.Enabled = True
End Sub
Sub Timer_Tick
    Dim pos As Int = WImageView1.Left
    If DirectionLeft Then
        If WImageView1.Left > 0 Then
            WImageView1.Left = pos - 1
        Else
            DirectionLeft = False
        End If
    Else
        If WImageView1.Left < 100%x-WImageView1.Width Then
            WImageView1.Left = pos + 1
        Else
            DirectionLeft = True
        End If
    End If
   
End Sub


another solution could be the viewanimation build in b4a. Using callsubutils class for example.

B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

    Private WImageView1 As ImageView
    Private position As Int
    Private DirectionLeft As Boolean
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    Activity.LoadLayout("Layout1")
    Move_Right
End Sub
Sub Move_Right
    WImageView1.SetLayoutAnimated(1000,100%x-WImageView1.Width,0,100dip,100dip)
    Starter.csu.CallSubPlus(Me, "Move_Left", 1000)
End Sub

Sub Move_Left
    WImageView1.SetLayoutAnimated(1000,0,0,100dip,100dip)
    Starter.csu.CallSubPlus(Me, "Move_Right", 1000)
End Sub
 

Attachments

  • ImageTimerCSU.zip
    35.3 KB · Views: 185
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Hoi Manfred,
Thx a lot - i got the 1st one of your samples running, looks very cool and is exactly what i want :D
Maybe for the most one's this is simpliest, but i can better send some datas via bluetooth from/to an Arduino and/or PC and control servos, steppers, relays etc. this way.
(my hobby is model railroad in Scale N (1:160)) :)
 
Upvote 0
Top