[SOLVED][Moved] Destroying Objects Based on text and buttons
-
hi, were trying to destroy objects based on numbers displayed on text..
whenever the user clicks on buttons numbers will be displayed on text.. these will be the basis for the destruction of objects.. but were kinda having a problemi have this javascript function to verify answer of the user
@function verify(t){
for(var i=0;i<maxIndex;i++){
if(fireArray[i]!=null){
if(fireArray[i].answer==displayText.toString()){
fireArray[i].visible=false;
fireArray[i]=null;
index--;
fireArray.sort();
return true;
}
}
}
return false;
}@this is my codes for displayText
@ Text {
id: displayText
x: 82
y: 75
anchors {
right: parent.right; verticalCenter: parent.verticalCenter; verticalCenterOffset: 76
rightMargin: 19; bottom:sip.bottom
}
font.pixelSize: 31; text: "0"; anchors.bottomMargin: -77; horizontalAlignment: Text.AlignRight; elide: Text.ElideRight
color: "#343434"; smooth: true; font.bold: true}@
so how will i do it?
i love panda
-
Sorry, but i can't understand what you mean. Which one you want to destroyed explicitly ?
-
what is this fireArray, is it array of buttons or something else?
-
@chuck gao: Im trying to destroy falling objects.. this post is related to my previous post
@Vijay Bhaska Reddy: its an array of falling objects..
-
ok.. what is the problem you are facing.. does the falling objects become invisible when you set visible to false?? or your problem is something else.
-
[quote author="lexan" date="1311225300"]@chuck gao: Im trying to destroy falling objects.. this post is related to my previous post
[/quote]I'm not sure all who read this post knows about your prev post (I read through all forum posts, but remember only part of them), so, please when you start new post leave a link or describe problem more detailed.
-
actually nothing happens. i was expecting that every time i input a correct answer the falling object would be invisible.. but to no avail
@chuck gao: sorry Im kinda confused on what to do so i wasnt able to focus on what to say right now
-
Can you give your code if its ok with you.. for me it looks like it should work. Zip your entire project directory and upload to some file uploading site and post the link here..
-
hi vijay
this is the link of the application im trying to do.. all i want to do in my sort of typing maniac game is that every time user inputs his answer the falling objects will be destroyed
anyway this is my link
-
Ok, I just checked it out.
Now, here's the first important part (why your code isn't working):
@Button { y: -1; width: 48; height: 26; color: 'purple'; smooth: false; opacity: 1; operation: "Ok" }
MouseArea{
....
}
@
The MouseArea needs to be inside the Button. And most importantly, it needs to take up the whole button (anchors.fill: parent).Now, I've a simpler solution. It seems you created a 'clicked' signal for Button that you aren't using.
Time to use it.Old code
@ Button { y: -1; width: 48; height: 26; color: 'purple'; smooth: false; opacity: 1; operation: "Ok" }
MouseArea{
anchors.fill: parent
onClicked:{
if(answerr!=""){
if(Logic.verify(text))
text=""
}
}
}
}@Fixed code
@ Button { y: -1; width: 48; height: 26; color: 'purple'; smooth: false; opacity: 1; operation: "Ok"
onClicked:{
if(answerr!=""){
if(Logic.verify(text))
text=""
}
}
}
@
Although, I'd go further and suggest integrating your button changes in to Button.qml itself since all the buttons look the same ;).Now, the second important part:
@if(answerr!=""){@
What's answerr? I can't find any similar variable in that QML file but I assume it is just display.text. The thing is, your verify function doesn't even use this value -- so it isn't required here.Fixed code
@Button { y: -1; width: 48; height: 26; color: 'purple'; smooth: false; opacity: 1; operation: "Ok"
onClicked: Logic.verify();
}@Now here's the third important part. You may be wondering why I removed some of the code like clearing the textbox -- well you can do this in the Javascript.
Old Code (with luzon code added in)
@function verify(){
for(var i=0;i<maxIndex;i++){
if(fireArray[i]!=null){
if(fireArray[i].answer==display.text){
fireArray[i].visible=false;
fireArray[i]=null;
index--;
fireArray.sort();
display.text="";
return true;
}
}
}
return false;
}@This code is fine.
But we need to make sure that fireArray[i].answer exists.
In Fire.qml:
@AnimatedImage{
property string answer: Logic.answer
id:fire@
and remove:
@id: answer@
in the Text element.Working fine now :)
P.S. Debugging: You can use console.log(variable) in QtCreator and it will print out the variables value for you. For example, console.log(fireArray[i].answer) will always be 'undefined' in your original code.
-
the answer refers to the answer displayed on the falling object.. i have decided to put the answer on the falling object along with the question so that that will be the peg for my verification.. it resides on another qml file... i'll just have to change its opacity so as to hide it.. well that's my idea..
though.. still not work... i have tried what you have posted here..
i think i have to clean up my codes first..
-
Ok, I will do quick summary:
Luzon.qml
@ Row{
x: -1
y: -29
Button { y: -1; width: 48; height: 26; color: 'purple'; smooth: false; opacity: 1; operation: "Ok"
onClicked: Logic.verify(); // turned MouseArea code in to this
}}@
Logic.js
@function verify(){ // remove 't' variable
for(var i=0;i<maxIndex;i++){
if(fireArray[i]){
if(fireArray[i].answer==display.text){
fireArray[i].visible=false; // this is redundant
fireArray[i]=null;
index--;
fireArray.sort();
display.text=""; // added this
return true;
}
}
}
return false;
}@Fire.qml
@AnimatedImage{
property string answer: Logic.answer // added this
id:fire
...Text{
// id: answer <-- removed this
x: 10
y: 10
color: "#ffffff"
text: qsTr(" "+Logic.answer)@Any questions, just ask :)
-
haha. thanks xsacha.. I have decided to clean up my codes first and rearrange some and then I included your modifications.. THANKS it finally worked.. the only thing is i have to change my design because I believe mine was poorly designed.. :D THANK YOU VERY MUCh.. one of my three problems in my game is now considered solved :D
*but before anything else.. i just wanna know exactly what are my mistakes? and what's the reason behind some exclusion of old codes and inclusion of new ones in mine?
-
Well the first mistake was that your MouseArea which calls 'Logic.verify()' was never being pressed
So your Javascript function, verify(), never got a chance to run at all.
The reason was because the MouseArea didn't have an area associated with it. It was outside the Button. To solve this, the MouseArea needed to be inside the button with "anchors.fill: parent". I chose the simpler solution of "onClicked", however, as I noticed you have a clicked signal.The second mistake was fireArray[i].answer was not what you thought it was. It was a Text element. You can't compare that to a string like "8" :P.
So my solution was to remove the 'answer' id which was associated to the Text element and instead make 'answer' a string property associated with Logic.answer (that is, the value you use).As for why I removed the 't' variable and the isVisible: they weren't used/redundant.