Learn what Felgo offers to help your business succeed. Start your free evaluation today! Felgo for Your Business

Forums

OverviewFelgo 3 Support (Qt 5) › Inherit Movement Animation

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #15882

    Rob

    Hi,

    I made two Entities one is a Street which is animated to go down.

     

    EntityBase {
        id: street
        entityType: "street"
    
        property double delay: 0
        property int variationDistance: 70
    
        MultiResolutionImage{
            id: streetpic
            source:"../../assets/street.png"
            anchors.fill: parent
        }
    
        MovementAnimation {
          id: animation
          target: parent
          property: "y"
          velocity: +150
          running: true
          maxPropertyValue: gameWindow.height
          onLimitReached: {
            reset()
          }
        }
    ...

    When I create my Car Entity which slides from left to right or otherwise like this:

        Street{
            id: str2
            width: crossstreet.width + 400
            height: crossstreet.height/5
            variationDistance: 210
    
            Car{
                id: car2
                width: str.height
                height: str.height
                randSite: Math.random()<0.5 ? 0 : 1
            }
    }

    everthing works fine. This means my car inherits the movement animation from the street. It actually is positioned right on the street.

    But I want to create the Car dynamically and random like this:

            Timer {
                     id: timer
                     interval: Math.random()*3000 + 2000
                     running: true // start running from the beginning, when the scene is loaded
                     repeat: true // otherwise restart wont work
                     onTriggered: {
    
                         var newEntityProperties = {
                            width: str.height,
                            height: str.height,
                            randSite: Math.random()<0.5 ? 0 : 1,
                         }
    
                         entityManager.createEntityFromUrlWithProperties(Qt.resolvedUrl("../Entities/Car.qml"),
                                     newEntityProperties);
    
                         // recalculate new interval between 2000 and 5000ms
                         interval = Math.random()*3000 + 2000
    
                         // restart the timer
                         timer.restart()
                     }
                 }

    Problem is I don’t know how to inherit the movement animation from the street like before. The cars just don’t end up on the street.

    Any Idea how I could solve this problem?

    Thx

    #15886

    Günther
    Felgo Team

    Hi!

    If you place the Car item within the Street (as in your first example), the position of the car is always relative to the position of the Street (the parent).
    The movement animation of the Street then also affects the Car within the Street Item.

    When creating the car dynamically with EntityManager, it is created within EntityManager::entityContainer. If you specify your Street as the entity container, then the car will be again placed within the Street item move together with the street.

    If you also intend to create other entities that will not be placed within the Street, you can also use e.g. your Scene as the entity container.
    It is then required to manually modify the Car position to take the Street position into account. For example, you can add a binding that keeps the car y-position updated:

    // bind car entity y position to street y-pos + 20 (relative pos of car within street)
    // e.g. if str.y = 30, carEntity.y will be 50
    carEntity.y = Qt.binding(function() { return str.y + 20 })

    Alternatively, you can also add a direct property binding to Car.qml – this required to add a parameter to access the Street item then:

    EntityBase {
      id: car
    
      property Item parentStreet: undefined
      y: parentStreet ? parentStreet.y + 20 : 0 // 20 px offset within street, 0 if no parentStreet is set
    
      // ...
    }

    You can then only set the parentStreet property when creating the Car:

    var newEntityProperties = {
                            parentStreet: str,
                            width: str.height,
                            height: str.height,
                            randSite: Math.random()<0.5 ? 0 : 1,
                         }

     

    Hope this helps!

    Best,
    Günther

     

     

    #15892

    Rob

    Thank you this worked perfectly!!

     

Viewing 3 posts - 1 through 3 (of 3 total)

RSS feed for this thread

You must be logged in to reply to this topic.

Qt_Technology_Partner_RGB_475 Qt_Service_Partner_RGB_475_padded