Android Question Converting the Running Animation B4J project to B4A

toby

Well-Known Member
Licensed User
Longtime User
code I've changed from
b4j:
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
To
b4a:
Process_Globals
    Private xui As XUI
    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 Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    kidsList.Initialize
    myTimer.Initialize("myTimer",1000/60)
    cnv.Initialize(Activity)
    
    '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
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
the reason for that is very simple you have removed the lines where vpW and vpH are given a value and that means that they will stay 0 and it is impossible to see something that was drawn with a width and height of 0.

why have you removed those lines?

B4X:
    vpW = MainForm.RootPane.Width
    vpH = MainForm.RootPane.Height

in b4a you can just change it to:

B4X:
    vpW = 100%x
    vpH = 100%y

in b4i you will need to do that in the page_resize event and set the vpw to the width and vph to the height of the page.
 
Upvote 0

toby

Well-Known Member
Licensed User
Longtime User
the reason for that is very simple you have removed the lines where vpW and vpH are given a value and that means that they will stay 0 and it is impossible to see something that was drawn with a width and height of 0.

why have you removed those lines?

B4X:
    vpW = MainForm.RootPane.Width
    vpH = MainForm.RootPane.Height

I deleted them by accident. ?

With the changes in place, now I can finally see the girl. However, she wouldn't move by herself and I have to keep pressing the button in order for her to move continuously.
How can I fix it?


TIA
 

Attachments

  • runningAnimation3.zip
    278.4 KB · Views: 102
Upvote 0

ilan

Expert
Licensed User
Longtime User
add the following to the myTimer_Tick event

B4X:
Sub myTimer_Tick
    frames = frames+1
    clearcanvas
    moveallKids
    drawAllKids
    Activity.Invalidate '<-----'
End Sub

in b4a you need to call the invalidate method to redraw the activity.

btw. why are you putting the Process_Globals section in the middle of the activity instead of having it on the top??
try always to keep your code as readable as possible.
 
Upvote 0
Top