粒子与不同的图像。 [QML]

时间:2017-06-26 15:50:27

标签: image qt qml particles

我正在使用粒子系统。我的粒子从中心出来,但我想要出现不同的图像。
这些图像的文件路径存储在一个数组中。我做了一个功能来完成这个安排并返回每个位置的内容 返回的是每张图片要发送到 ImageParticle的来源的路径。

问题是,如果你滚动整个数组但只返回最后一个图像的路径,从逻辑上讲,我得到一个图像而不是存储在数组中的二十个图像。
有人可以帮帮我吗? 帮助和建议被广泛接受。

这是我的代码:

import QtQuick 2.0
import QtQuick.Particles 2.0    

Rectangle {
    id: bg
    width: 1920         //360
    height: 1080            //360
    color: "black"

    ParticleSystem {
        id: particleSys
    }            

    Emitter{
        id: particles
        anchors.centerIn: parent
        height: 1; width: 1
        system: particleSys
        emitRate: 30
        lifeSpan: 4000
        lifeSpanVariation: 500
        maximumEmitted: 1000
        size: 5
        endSize: 200                      

        velocity: TargetDirection{      
            targetX: 100; targetY: 100
            targetVariation: 360
            magnitude: 250
        }            
    }         

    property var picturesList: [
        "images/Image1.png", "images/Image2.png", "images/Image3.png", "images/Image4.png", "images/Image5.png", "images/Image6.png", "images/Image7.png", "images/Image8.png", "images/Image9.png", "images/Image10.png",
        "images/Image11.png", "images/Image12.png", "images/Image13.png", "images/Image14.png", "images/Image15.png", "images/Image16.png", "images/Image17.png", "images/Image18.png", "images/Image19.png", "images/Image20.png"
    ]

    function getImage(arr){
        var flag = "";
        for(var i = 0; i < arr.length ; i++){                    
            flag = arr[i];
            console.log("Image: " + arr[i] + " flag: " + flag )   //To check if array is traversing.
        }    
        return flag;
    }

    ImageParticle{            
        property var link: getImage(picturesList)
        source: link
        system: particleSys
    }

    // To check which image is returned.
    MouseArea {  
        anchors.fill: parent
        onClicked: {
            console.log(getImage(picturesList))
        }
    }             
}

1 个答案:

答案 0 :(得分:1)

我是问过上一个问题的人,我在这里给出答案 我非常清楚,如果您知道另一种方式我邀请您分享,可以通过多种方式解决问题 欢迎提出有关回应的建议。

我在这里分享代码:

import QtQuick 2.0
import QtQuick.Particles 2.0

Rectangle {
    id: bg
    width: 1920         
    height: 1080        
    color: "black"

    ParticleSystem {
        id: particleSys
    }           

    Emitter{
        id: particles
        anchors.centerIn: parent
        height: 1; width: 1
        system: particleSys
        emitRate: 30
        lifeSpan: 4000
        lifeSpanVariation: 500
        maximumEmitted: 1000
        size: 5
        endSize: 200                          

        velocity: TargetDirection{      //4
            targetX: 100; targetY: 100
            targetVariation: 360
            magnitude: 250
        }            
    }         
    property var picturesList: [
        "images/Image1.png", "images/Image2.png", "images/Image3.png", "images/Image4.png", "images/Image5.png", "images/Image6.png", "images/Image7.png", "images/Image8.png", "images/Image9.png", "images/Image10.png",
        "images/Image11.png", "images/Image12.png", "images/Image13.png", "images/Image14.png", "images/Image15.png", "images/Image16.png", "images/Image17.png", "images/Image18.png", "images/Image19.png", "images/Image20.png"
    ]          

    ItemParticle {
        id: particle
        system: particleSys
        delegate: itemDelegate
    }

    Component {
        id: itemDelegate
        Rectangle {
            id: container
            width: 26*Math.ceil(Math.random()*3); height: width
            color: 'white'
            Image {
                anchors.fill: parent
                anchors.margins: 3
                source: 'images/Image'+Math.ceil(Math.random()*20)+'.png'            
            }
        }
    }            
} 

如您所见,将输出文件夹中的所有图像 我只需将&#34; ImageParticle&#34; 更改为&#34; ItemParticle&#34; ,然后我添加 & #34;组件&#34; 作为代理所需的图像。
所以我们不会使用数组(在前面的代码中省略它),因为现在只连接图像的路径和一个随机数(选择图像)。
我希望你能为他们服务,如果你有更好的实施,请告诉他们。