1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

B4J Question Tracking the position of an animation

Discussion in 'B4J Questions' started by ElliotHC, May 28, 2019.

  1. ElliotHC

    ElliotHC Active Member Licensed User

    I am moving an image with this code:

    Does anyone know how I can track the X and Y position of the image as it does it's animation?

  2. Daestrum

    Daestrum Well-Known Member Licensed User

    Small example (uses JavaObject Library) adds a changelistener to the imageview position
    Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim iv As ImageView
    Dim im As Image
    End Sub
    Sub AppStart (Form1 As Form, Args() As String)
     MainForm = Form1
    "","c:/temp/hannah.jpg"' the image
    Dim jo As JavaObject = iv ' the imageview
     Dim o As Object = jo.CreateEvent("javafx.beans.value.ChangeListener","moved",False)
    "layoutXProperty",Null).RunMethod("addListener",Array(o)) ' make sure you choose property that will change
    End Sub
    Sub moved_Event(MethodName As String, Args() As Object)' ignore the arguments
     Log("X :"&iv.Left&" Y :"&iv.Top)' read the posn from the imageview
    End Sub
    Last edited: May 28, 2019
  3. Erel

    Erel Administrator Staff Member Licensed User

    Another option is to implement the animation yourself. It is quite simple.

    Sub Button1_Click
    1000Rnd(0500), Rnd(0500))
    End Sub

    Sub AnimateTo(View As B4XView, Duration As Int, TargetX As Int, TargetY As Int)
    If Duration = 0 Then
    View.Left = TargetX
    View.Top = TargetY
    End If
    Dim StartTime As Long = DateTime.Now
    Dim StartX As Int = View.Left
    Dim StartY As Int = View.Top
    Do While DateTime.Now < StartTime + Duration
    Dim f As Float = (DateTime.Now - StartTime) / Duration
    Dim x As Int = StartX + f * (TargetX - StartX)
    Dim y As Int = StartY + f * (TargetY - StartY)
    View.Left = x
    View.Top = y
    '       Log(x & ", " & y)
    View.Left = TargetX
    View.Top = TargetY
    End Sub
    It should be smooth in release mode.
  4. ElliotHC

    ElliotHC Active Member Licensed User

    Thankyou Daestrum, although I'm really still quite inexperianced with B4J. Do you think you might be able to help me join a few dots here?

    What goes in "layoutXProperty"? Also, 'MethodName' am I supposed to change that too?

    I'm currently trying to implement this with iv1 but it's saying Method: layoutXProperty not found, probably pretty obvious to most, any chance you might be able to help me figure out what I'm supposed to put in there?

    Thanks again.
  5. Daestrum

    Daestrum Well-Known Member Licensed User

    'layoutXProperty' is a property that all nodes (controls) contain. It basically holds the controls X position within the rootpane.
    It's related property is layoutYProperty, which is the Y position.

    You don't have to put anything into layout(X/Y)Property, you just refer to it by name.

    MethodName is supplied by the changeListener, again you don't need to do anything with it.

    Can you post the error you are getting.
  6. ElliotHC

    ElliotHC Active Member Licensed User

  7. Daestrum

    Daestrum Well-Known Member Licensed User

    It should be layoutXProperty, you have LayoutXProperty.
    It's not a class, so following java convention, it will start with a lowercase letter.
  8. ElliotHC

    ElliotHC Active Member Licensed User

    Thanks, that fixed the error but I'm not seeing anything in the log.. Just says "Program started"
  9. Daestrum

    Daestrum Well-Known Member Licensed User

    What position is it moving from > to, if there is no change in the X value(it moves vertically), then you have to use layoutYProperty, if it moves horizontally then you use layoutXProperty. If it moves in both then you can use either.
    ElliotHC likes this.
  10. ElliotHC

    ElliotHC Active Member Licensed User

    Ah yes, that was it. They were only moving in the Y axis. Thanks Daestrum.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice