Why can't only the customed property "kind" one be allocated data correctly?
-
Below are the part of my main codes where I try to allocate the data"root.bulletkind" which has been defined correctly to a incubator to creat a object.
What the problem is that except"kind",all of the other property could be allocated correctly,and toward is a customed property,and below is the part of my bullet.qml codes:
It always console log "undefined" for "root.kind",I don't know the reason.I think the customed proeprety "toward" is same to it,but it just can't work correctly😵💫 -
Ids don't work like properties. You can try something like this.
function fire(t,nx,ny){ var comp = Qt.createComponent("MyBullet.qml"); var incubateMe = comp.incubateObject(root,{x:nx,y:ny,toward:t}) incubateMe.onStatusChanged = function(status){ if (status === Component.Ready){ console.log("Bullet object is ready ") incubateMe.object.kind = root.bulletKind incubateMe.object.visible = true incubateMe.object.color = "red"; } } }
You can refer the complete example here
Also don't post the images. pasting the code or example will help.
-
Below are the code of the main QML I have corrected
function fire(t,nx,ny) { var com=Qt.createComponent("bullet.qml"); var incubator=com.incubateObject(root,{x:nx,y:ny,toward:t}); incubator.onStatusChanged=function(status) { if(status===Component.Ready) { console.log("OK"); incubator.object.visible=true; incubator.object.kind=root.bulletkind incubator.object.destroy(4000); addbullet(incubator.object); } } }
And below are my bullet.qml:
import QtQuick Rectangle { id:root; width:20; height:width; radius:width/2; visible:false; z:1; color:"yellow"; property var toward;//1对应左,2对应上,3对映右,4对应下 property int speed:200; property var kind; // property var bound:[];//1对应左,2对应上,3对映右,4对应下 onVisibleChanged: { if(visible) { console.log(root.kind); moveanimation(root.toward,root.kind); } } // onXChanged: // { // if(x<=bound[1]||x>=bound[3]) // {visible=false; // console.log("消失在x")} // } // onYChanged: // { // if(y<=bound[2]||y>=bound[4]) // { visible=false; // console.log("xiao shi zai y")} // } NumberAnimation { id:ani; target:root; easing.type:Easing.Linear; onFinished: { root.visible=false; } } function moveanimation(a,b)//a用来选择方向,b用来选择动画 { //here I want to use kind to choose the style that the bullet shoot // switch(b) // { // case 1: // { // ani.easing.type=Easing.InOutExpo; // root.speed=200; // break; // } // case 2: // { // ani.easing.type=Easing.Linear; // root.speed=300; // break; // } // case 3: // { // ani.easing.type=Easing.InOutElastic; // root.speed=200; // break; // } // } switch(a) { case 1: { ani.property="x"; ani.duration=1000*x/(root.speed); ani.to=0; ani.start(); break; } case 2: { ani.property="y"; ani.to=0; ani.duration=1000*y/(root.speed); ani.start(); break; } case 3: { ani.property="x"; ani.to=640; ani.duration=1000*(640-x)/(root.speed); ani.start(); break; } case 4: { ani.property="y"; ani.to=480; ani.duration=1000*(480-y)/(root.speed); ani.start(); break; } } } }
-
@dheerendra Sorry,I have tried like you telling,but it still can't work in my project😵💫I am ready confused why could this happen?
-
Code is not exactly same as mine. Revert the order like the following. It works.
incubator.object.kind=root.bulletkind
incubator.object.visible=true; -
@dheerendra Amazing🥳!It works,but why could not it run correctly before?I think thery are similar,just a little different.Is the problem on the order of the property?🧐
-
-