Animation 在矩形内旋转的球,QML项目

Animation 在矩形内旋转的球,QML项目,animation,qml,qtquick2,qtquickcontrols2,Animation,Qml,Qtquick2,Qtquickcontrols2,我有一个球在QML项目中的一个矩形内设置动画,如下所示,当它碰到矩形的边界时,它会返回到内部,直到它碰到另一个边界,然后再次返回,以此类推 我已经为此编写了此代码,但不知道使用什么代码使球在击中边界时返回 你能帮帮我吗 main.qml: import QtQuick 2.6 import QtQuick.Window 2.2 Window { visible: true width: 800 height: 600 title: qsTr("Ball_in_

我有一个球在QML项目中的一个矩形内设置动画,如下所示,当它碰到矩形的边界时,它会返回到内部,直到它碰到另一个边界,然后再次返回,以此类推

我已经为此编写了此代码,但不知道使用什么代码使球在击中边界时返回
你能帮帮我吗

main.qml:

import QtQuick 2.6
import QtQuick.Window 2.2

 Window {
    visible: true
    width: 800
    height: 600
    title: qsTr("Ball_in_Room")

    Rectangle {
        id: root
        width: 700; height: 500
        border.width: 10
        border.color: "gray"
        color: "moccasin"
        property real xPos: root.width
        property real yPos: Math.random() * root.height

        Ball { id: ball }

        ParallelAnimation {
           id: anim
               NumberAnimation {
                        target: ball
                        properties: "x"
                        to: root.xPos
                        duration: 1000
                        easing.type: Easing.Linear
                    }
               NumberAnimation {
                        target: ball
                        properties: "y"
                        to: root.yPos
                        duration: 1000
                        easing.type: Easing.Linear
                    }
        }

         MouseArea {
             anchors.fill: ball
             onClicked: anim.start()
         }
       }
     }
import QtQuick 2.8

Rectangle {
    width: 20; height: 20
    x: 250; y: 250
    color: "blue"
    radius: width/2
}
Ball.qml:

import QtQuick 2.6
import QtQuick.Window 2.2

 Window {
    visible: true
    width: 800
    height: 600
    title: qsTr("Ball_in_Room")

    Rectangle {
        id: root
        width: 700; height: 500
        border.width: 10
        border.color: "gray"
        color: "moccasin"
        property real xPos: root.width
        property real yPos: Math.random() * root.height

        Ball { id: ball }

        ParallelAnimation {
           id: anim
               NumberAnimation {
                        target: ball
                        properties: "x"
                        to: root.xPos
                        duration: 1000
                        easing.type: Easing.Linear
                    }
               NumberAnimation {
                        target: ball
                        properties: "y"
                        to: root.yPos
                        duration: 1000
                        easing.type: Easing.Linear
                    }
        }

         MouseArea {
             anchors.fill: ball
             onClicked: anim.start()
         }
       }
     }
import QtQuick 2.8

Rectangle {
    width: 20; height: 20
    x: 250; y: 250
    color: "blue"
    radius: width/2
}

弹跳球的非常简单的实现:

矩形{
id:集装箱
锚定。填充:父级
border.color:“橙色”
边框宽度:3
长方形{
id:球
属性双X增量:Math.random()+0.5
属性双Y增量:Math.random()+0.5
宽度:50
高度:宽度
半径:宽度/2
颜色:“浅绿色”
x:300
y:300
计时器{
间隔时间:1
重复:对
跑步:对
反对:{
ball.x=ball.x+(ball.xin增量*2.0);
ball.y=ball.y+(ball.y增量*2.0);
if(ball.x=容器宽度)
ball.xincrement*=(-1);
if(ball.y=容器高度)
ball.i增量*=(-1);
}
}
}
}

你说“返回”是什么意思?是否要在此处实现反射效果?我认为这个问题与QML无关。我不只是计算,你只需要实现一些已知的算法来获得正确的方向和碰撞检测。顺便说一句,你为什么不买一些2D引擎,而不是自己实现呢?看一看box2dqml插件。谢谢。(+1). 是的,我是说反思。假设你在一个房间里,用你的脚射出一个球。它向房间的墙壁移动,然后反射回来,撞击房间的另一面墙,然后再次发生,直到球停下来。我的意思是如何让球感觉到墙和回来等等。我想解决这个练习。谢谢你的回答。我可能有更多的问题要问你,如果可能的话,请继续指导我。我的问题是关于计时器主体中的最后一个if。正如我所说,ball.y是一个非常简单的实现,可能会忽略一些特定的情况。我仍然建议你使用一些2D引擎,当然如果你的目标不是自己发明它的话。我需要这样来完成这个游戏。我不知道2D游戏引擎,但这个简单的游戏对我来说就像一个练习。如果可能的话,请告诉我为什么当ball.y为零时,减小其y坐标会使其向下移动!?它应该使它逻辑地向上移动。好吧,我想你做到了。无论如何,您可以使用调试器检查它。对我来说,这段代码运行得很好,也许你的用例是不同的。