Using animations



  • Hello,

    Here is a minimal version of a code revealing the problem which is:

    Moving the racket when playing the game on the Desktop kit (Windows) doesn't affect the speed of ball's movement and is fine. But when run on an Android device, moving the racket affects the speed of ball's movement as though their movements have been tied together.

    I guess if I use Animations that problem may be solved. How to use Animations in this program, please?

    main.qml:

    import QtQuick 2.9
    import QtQuick.Window 2.2
    
    Window {
        visible: true
        width: 720
        height: 620
        title: qsTr("Movement Test")
    
        Rectangle {
            id: table
            anchors.fill: parent
            color: "gray"
    
            Rectangle {
                id: ball
                property double xincrement: Math.random() + 0.5
                property double yincrement: Math.random() + 0.5
                width: 15
                height: width
                radius: width / 2
                color: "white"
                x: 300; y: 300
            }
    
            Racket {
                id: myRacket
                x: table.width - 50
                y: table.height/3
                color: "blue"
            }
    
            Timer {
                interval: 5; repeat: true; running: true
    
                function collision() {
                    if((ball.x + ball.width >= myRacket.x  &&
                        ball.x < myRacket.x + myRacket.width) &&
                       (ball.y + ball.height >= myRacket.y &&
                        ball.y <= myRacket.y + myRacket.height))
                        return true
                    return false
                }
    
                onTriggered: {
                    if(ball.x + ball.width >= table.width)
                        running = false
    
                    else if(ball.x <= 0)
                        ball.xincrement *= -1
    
                    else if (collision())
                        ball.xincrement *= -1
                                   
                                // Move the ball 
                    ball.x = ball.x + (ball.xincrement * 1.5);
                    ball.y = ball.y + (ball.yincrement * 1.5);
    
                    if(ball.y <= 0 || ball.y + ball.height >= table.height)
                        ball.yincrement *= -1
                }
            }
        }
    }
    

    Racket.qml:

    import QtQuick 2.9
    
    Rectangle {
        id: root
        width: 15; height: 65
    
        MouseArea {
            anchors.fill: parent
            anchors.margins: -root.height
            drag.target: root
            drag.axis: Drag.YAxis
            drag.minimumY: table.y
            drag.maximumY: table.height - root.height - 10
        }
    }
    


  • When I change the kit from Desktop to Android on Qt Creator, I get these messages/warnings on Issues window:

    :-1: warning: "D:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-gcc" is used by qmake, but "D:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-gcc.exe" is configured in the kit.
    Please update your kit or choose a mkspec for qmake that matches your target environment better.

    :-1: warning: "D:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++" is used by qmake, but "D:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.9\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe" is configured in the kit.
    Please update your kit or choose a mkspec for qmake that matches your target environment better.

    Isn't it related to the problem with the program?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.