Hello, I am quite new in QT and QML and I`d like to ask about object life in QML. I have a sample code for a “small game”. The idea is to press the same colors with both fingers ( touch only) and they will vanish. It`s in concept only, and I`ve made some small code to aid it. The idea is that the screen is populated with random images ( colors with Rect in my case ). When I click on specific Rect – it vanishes. Here is my main.qml file ( where all starts ).
import QtQuick 2.0
import "Engine.js" as Engine
import "ObjectCreator.js" as ObjectCreator
Item {
id: main
Component.onCompleted: (function() {
Engine.Engine.createScreen(0,0, 640, 480);
(function() {
var color = ["red", "green", "blue", "black", "pink","yellow"];
var tmp = 0;
for ( var i=0; i <640 ; i+= 40) {
for ( var j=0; j < 480; j+= 40) {
(function(arg){
console.log(Math.floor(Math.random()*color.length));
Engine.Engine.createObject(
i, j, 40, 40,
color[Math.floor(Math.random()*color.length)]
, "o"+tmp,
function() {
Engine.Engine.getObjectByIndex(arg).getSelf().destroy();
});
})(tmp++);
} //emd for() 2
} // end for() 1
})();
})();
MouseArea {
/*optional */
id: event
anchors.fill: parent
onClicked: function () {}
}
Timer {
id: mainThread;
interval: 100;
running: true;
repeat: true;
onTriggered: function(){
var o = Engine.Engine.getAllObjects();
for(var i=0; i < o.length; i++) {
}
};
}
} //end main
When I run it – on click it performs as expected – the object is destroyed and a white field replaces it, however, the log says : “file:///home/ilian/QT5/untitled2/inline:1: TypeError: Cannot read property of null”. I want to ask – is it safe or is it malicious? There is no object in the clicked area so can I just skip the error or I must make some checks to avoid it?
I can`t add Engine.js, CallStack.js and ObjectCreator.js since they are large, but the idea is that Engine calls ObjectCreator which makes a Rect in X,Y with Width and Height and color, and adds a function to Object`s private CallStack for further usage. For now I just add a foo and pop it from 0 index. In my case the method passed in the ObjectCreation so click performs destroy(). getSelf() return reference to the Rect object – not to the ObjectCreator, Engine has a list of ObjectCreators – the class is not factory and the name should be Object for convinience but for now I`ve left it as it is, since I`ve planned it to be a factory.
↧