[Qt-qml] Animating sprites - how do do it "right" ?
Gregory Schlomoff
gregory.schlomoff at gmail.com
Thu Dec 30 10:12:22 CET 2010
The idea of declarative languages is to think in terms of rules that
govern your scene. So, if one of your rules is: "the sprite must
bounce everytime it touches an obstacle", you could express that by
putting your collision detection code in
a javascript function that gets callled from onXChanged and onYChanged.
This way, you now have the guarantee that no matter where your object
goes, your rule will be applied, and the collision detection code will
be executed.
Cheers,
Greg
On Thu, Dec 30, 2010 at 2:39 PM, Tim Murphy <tnmurphy at gmail.com> wrote:
> 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/
> _______________________________________________
> Qt-qml mailing list
> Qt-qml at qt.nokia.com
> http://lists.qt.nokia.com/mailman/listinfo/qt-qml
>
More information about the Qt-qml
mailing list