2

I want to create a component that starts animations from a list. I've provided an example of what I want to do below. I don't know if this is the correct way in terms of performance and timer accuracy. Is it better to use a dedicated timer for each animation?

import QtQuick 2.0

Item {
    id: root

    function startOne() {
        animOne.start()
    }

    function startTwo() {
        animTwo.start()
    }

    property var listEvent: {
        1000: root.startOne,
        2000: root.startTwo
    }

    property int elapsed: 0
    property int last_elapsed: 0

    Rectangle {
        id: one
        x: 0
        y: 0
        width: 200
        height: 200
        color: "black"
    }

    Rectangle {
        id: two
        x: 600
        y: 600
        width: 200
        height: 200
        color: "black"
    }

    PropertyAnimation {
        id: animOne
        target: one
        properties: "x"
        to: 600
        running: false
    }

    PropertyAnimation {
        id: animTwo
        target: two
        properties: "x"
        to: 0
        running: false
    }

    function pop(last_elapsed, elapsed) {
        for (var i = last_elapsed; i <= elapsed; i++) {
            if (i in root.listEvent) {
                listEvent[i]()
            }
        }
    }

    function scheduleEvent(delta) {
        root.last_elapsed = root.elapsed
        root.elapsed += delta
        root.pop(root.last_elapsed, root.elapsed)
    }

    Timer {
        id: scheduler
        interval: 16
        running: true
        repeat: true
        onTriggered: {
            root.scheduleEvent(scheduler.interval)
            console.log("timer: " + root.elapsed)
        }
    }
}
Mitch
  • 23,716
  • 9
  • 83
  • 122
Antonio Caristia
  • 143
  • 2
  • 11
  • 3
    `Timer` is actually not that accurate (see [this nice answer](http://stackoverflow.com/a/22754926/2538363)) but it can still be viable for the resolution used in the example. A C++ solution (e.g. [this implementation](http://stackoverflow.com/a/31000152/2538363)) can improve substantially accuracy. Regarding performances...ah, it smells we are falling into premature optimisation here. :) – BaCaRoZzo Jan 18 '16 at 19:48

0 Answers0