Results 1 to 5 of 5

Thread: A simple animation

  1. #1
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default A simple animation

    Hi all,

    We have a simple rectangle:

    Qt Code:
    1. Rectangle {
    2. id: root
    3. width: 800; height: 600
    4. border.width: 5
    5. border.color: "white"
    6. color: "green"
    7. }
    To copy to clipboard, switch view to plain text mode 

    And also a small ball as a white rectangle:

    Qt Code:
    1. Rectangle {
    2. width: 20; height: 20
    3. x: 250; y: 250
    4. color: "white"
    5. radius: width/2
    6. }
    To copy to clipboard, switch view to plain text mode 

    That ball is inside that rectangle. How to make that ball at a fixed speed go up and when it hits the upper side of the rectangle, it returns down and up again, for 10 times, please?
    Last edited by franky; 6th December 2017 at 12:22.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A simple animation

    Show us what you have tried and explain to us what it does wrong.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: A simple animation

    I tried to use NumberAnimation but it needs x and y properties, while I want the ball goes up/down/right/left just like a normal ball when you shoot it in a closed room and it goes toward any direction until it stops. What type of animations should I use for that and using what specifications, please?

  4. #4
    Join Date
    Jan 2016
    Posts
    76
    Thanks
    28
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: A simple animation

    I used this:

    main.qml:

    Qt Code:
    1. import QtQuick 2.6
    2. import QtQuick.Window 2.2
    3.  
    4. Window {
    5. visible: true
    6. width: 800
    7. height: 600
    8. title: qsTr("Ball_in_Room")
    9.  
    10. Rectangle {
    11. id: root
    12. width: 700; height: 500
    13. border.width: 10
    14. border.color: "gray"
    15. color: "moccasin"
    16. property real xPos: root.width
    17. property real yPos: Math.random() * root.height
    18.  
    19. Ball { id: ball }
    20.  
    21. ParallelAnimation {
    22. id: anim
    23. NumberAnimation {
    24. target: ball
    25. properties: "x"
    26. to: root.xPos
    27. duration: 1000
    28. easing.type: Easing.Linear
    29. }
    30. NumberAnimation {
    31. target: ball
    32. properties: "y"
    33. to: root.yPos
    34. duration: 1000
    35. easing.type: Easing.Linear
    36. }
    37. }
    38.  
    39. MouseArea {
    40. anchors.fill: ball
    41. onClicked: anim.start()
    42. }
    43. }
    44. }
    To copy to clipboard, switch view to plain text mode 

    Ball.qml:
    Qt Code:
    1. import QtQuick 2.8
    2.  
    3. Rectangle {
    4. width: 20; height: 20
    5. x: 250; y: 250
    6. color: "blue"
    7. radius: width/2
    8. }
    To copy to clipboard, switch view to plain text mode 


    Would you please help me complete the animation?

  5. #5
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: A simple animation

    Have a look here http://doc.qt.io/qt-5/qtquick-animation-example.html, specifically the section 'PropertyAnimation' which is basically doing what you want.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. Replies: 1
    Last Post: 3rd May 2017, 11:54
  2. Replies: 3
    Last Post: 11th September 2015, 14:42
  3. Replies: 11
    Last Post: 30th June 2015, 14:19
  4. Simple Animation Help (Nevermind)
    By Pembar in forum Qt Programming
    Replies: 1
    Last Post: 19th May 2009, 12:33
  5. QT Animation Simple Pushbutton Help
    By Pembar in forum Qt Programming
    Replies: 4
    Last Post: 6th May 2009, 13:38

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.