我有一个球在QML项目中的一个矩形内设置动画,如下所示,当球碰到矩形的边界时,它会返回到内部,直到它碰到另一个边界,然后再返回,依此类推。
我已经为此编写了此代码,但不知道使用什么代码使球在碰到边界时返回!
你能帮帮我吗?
主要的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()
}
}
}
球qml:
import QtQuick 2.8
Rectangle {
width: 20; height: 20
x: 250; y: 250
color: "blue"
radius: width/2
}