var animation = PropertyAnimation {这段QML代码有什么问题呢?需要怎么修改呢?

亭台六七座 2023-05-18 10:11:19

 

     Timer {
        id: timer2
        interval:1000 // 每隔1秒触发一次
        running: true // 启动定时器
        repeat: true //重复触发
        onTriggered: {
            var animation = PropertyAnimation {
                target: rect
                property: ["x", "y"]
                to: Qt.point(Math.random() * (parent.width - rect.width), Math.random() * (parent.height - rect.height))
                duration:1000 // 动画持续时间为1秒
                easing.type: Easing.InOutQuad // 缓动函数为InOutQuad
            }
            animation.start() // 启动属性动画
        }
    }

 

var animation = PropertyAnimation {

这段QML代码有什么问题呢?需要怎么修改呢?

...全文
174 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
CSDN-Ada助手 2023-05-18
精选
  • 打赏
  • 举报
回复
您的问题已经帮您同步至问答, 链接: https://ask.csdn.net/questions/7946926, 请您保持关注, 如果回答有帮助解决此问题, 麻烦您动动小手给相关的回答点赞, Ada会在评论区为您更新结题状态
诺谦 2023-05-28
  • 打赏
  • 举报
回复

对象不能在函数里直接创建,所以把对象放在外部:

Timer {
    id: timer2
    interval: 1000 // 每隔1秒触发一次
    running: true // 启动定时器
    repeat: true // 重复触发
    onTriggered:  {
          animation.to = Qt.point(Math.random() * (parent.width - rect.width), Math.random() * (parent.height - rect.height))
            animation.restart()
    }
    
}

Rectangle {
    id: rect // 添加目标元素的定义
    width: 100
    height: 100
    color: "red"
}


PropertyAnimation {
    id: animation
    target: rect
    property: ["x", "y"] 
    duration: 1000 // 动画持续时间为1秒
    easing.type: Easing.InOutQuad 
}
coolcalf 2023-05-18
  • 打赏
  • 举报
回复

这段 QML 代码存在以下问题:

PropertyAnimation 是一个元素类型,而不是变量类型。它应该在元素声明的内部使用,而不是在变量声明的内部使用。因此,将 PropertyAnimation 移动到元素声明的内部。

rect 在代码中未定义,因此动画无法正确地找到目标。你需要确保在 target 属性中指定正确的目标元素。

修改后的代码如下:

Timer {
id: timer2
interval: 1000 // 每隔1秒触发一次
running: true // 启动定时器
repeat: true // 重复触发
onTriggered: {
var animation = PropertyAnimation {
target: rect // 修改为正确的目标元素
property: ["x", "y"]
to: Qt.point(Math.random() * (parent.width - rect.width), Math.random() * (parent.height - rect.height))
duration: 1000 // 动画持续时间为1秒
easing.type: Easing.InOutQuad // 缓动函数为InOutQuad
}
animation.start() // 启动属性动画
}
}

Rectangle {
id: rect // 添加目标元素的定义
width: 100
height: 100
color: "red"
}
请注意,这只是对原始代码的一种修正方法,具体修改取决于你想要实现的效果。

16

社区成员

发帖
与我相关
我的任务
社区描述
你好陌生人,很高兴认识你,我们一起共同进步吧!!加入社区,超度福利等你来拿
音视频c++ 技术论坛(原bbs) 四川省·成都市
社区管理员
  • 诺谦
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧