[Qt-qml] Animating sprites - how do do it "right" ?

Tim Murphy tnmurphy at gmail.com
Thu Dec 30 08:39:15 CET 2010


Hi,

 I have been playing with animations in QML and my current example
uses a timer and a bit of javascript to update the locations of my
"sprite" every 20ms or so. It seems to work.

I feel that I need to be involved in the position updates to the
sprite so that I can eventually detect collisions, implement the logic
for the sprites to react to the user's actions and to make them bounce
off, e.g., the sides of the window.

What I am doing seems wrong to me - not very declarative and using
lots of javascript to minutely control the position of the object. I
get the impression that I'm going to be in trouble when I try to rely
on my knowledge of where the item is because I'm guessing that it
might not have arrived where I sent it by the time the timer triggers
again.

I am wondering: is there a better way to do this?

My "spinner" object is just a bit of text that has a SmoothedAnimation
attached to it.  Here is the timer loop where I decide if the sprite
needs to bounce or not.  Strictly speaking I could calculate the
bounce point in advance and then I would not need this timer loop but
I am thinking about changing the animation more dynamically - e.g.
when I have multiple "spinners" they might bounce on collision.

Timer {

id: timer
interval: 20
repeat: true
running: true
onTriggered: {

spinner.rotation += 1
var newx = spinner.x + spinner.xinc
var newy = spinner.y + spinner.yinc

if (newx + spinner.width >= rect.width || newx <= 0) {
spinner.xinc = - spinner.xinc
}
if (newy + spinner.height >= rect.height|| newy <= 0) {
spinner.yinc = - spinner.yinc
}

spinner.x += spinner.xinc
spinner.y += spinner.yinc
}
}

-- 
You could help some brave and decent people to have access to
uncensored news by making a donation at:

http://www.thezimbabwean.co.uk/


More information about the Qt-qml mailing list