[Solved] cannot set property of undefined
-
Hi, i build my project without errors, but when i tried start EXE files, i got this error.
Cannot set 'focus' of undefined@function create_newgame(num)
{
sound_main.stop();
if (num==5)
{
var component = Qt.createComponent("Big_game.qml");
if (component.status == Component.Ready)
var bg_game = component.createObject(main_Window, {});
bg_game.focus = true;
}
else
{
var component = Qt.createComponent("Game.qml");
if (component.status == Component.Ready)
var game = component.createObject(main_Window, {});
game.focus = true; //THIS IS ERRORswitch (num) { case 1: game.image_number = "qrc:/image/num_1.png"; break; case 2: game.image_number = "qrc:/image/num_2.png"; break; case 3: game.image_number = "qrc:/image/num_3.png"; break; case 4: game.image_number = "qrc:/image/num_4.png"; break; } } if(num!=4) game.multiplier = num; else game.multiplier = 1;
}@
-
your game object is outside of the if-scope, do you have experience with programming in general?
anyway the solution is to put { } around the if-block, like:
@
if (component.status == Component.Ready) {
var game = component.createObject(main_Window, {});
game.focus = true; //THIS IS ERROR
}
// game is out of scope after the } here and will be "undefined"
@in general if you omit the { } after an if or while-loop etc only the first expression will be used in the block, so I always use { } no matter if there is only 1 expression or more, that is some good practice but it's up to you :)
another tip, instead of you "switch (num)" you could just use a single expression if you want:
@
game.image_number = "qrc:/image/num_" + num + ".png";
@ -
Thanks, but i have good programming experience. About last tip: it's great that i can do this.
About this block
@if (component.status == Component.Ready)
var game = component.createObject(main_Window, {});
game.focus = true; //THIS IS ERROR
@
I need created object "game", and it should be error when component is not ready, but it's ready! I did as you said, and create object without IF but ERROR remain.
There are similar questions, but i don't understand solution.
https://qt-project.org/forums/viewthread/30016 -
No i didn't say remove the if, just look at the code I provided you forgot the {} around "game.focus = true" :)
you need to use it like this (again):
@
if (component.status == Component.Ready)
{
var game = component.createObject(main_Window, {});
game.focus = true;
}
@same with your other object "bg_game" off course.
-
@if (component.status == Component.Ready)
{
var game = component.createObject(main_Window, {});
game.focus = true;
}@@if (component.status == Component.Ready)
var game = component.createObject(main_Window, {});
game.focus = true;
@
If construction "IF" returned TRUE. I did as you say, ERROR disappeared but whatever didn't work, *It seems that IF returned false. * and component are not ready.
Why in QT all is ok, but from EXE i have this problem. -
well I'm sorry to tell you this but the code is not the same.
@
if (component.status == Component.Ready)
var game = component.createObject(main_Window, {});
game.focus = true;
@@
if (component.status == Component.Ready)
{
var game = component.createObject(main_Window, {});
}
game.focus = true;
@
that is why game.focus = true produces and warning because "game" is undefined at that line... seems like nobody else is reading this thread but you tr it yourself if you don't believe me -
Hi petro2033,
Welcome to Devnet.
I suggest you experiment with if blocks and braces and debug in every possible place to see what your variables contain as values.
@
If (A)
B;
C;
@Is not the same as
@
If (A)
{ B;
C;
}
@PS: using capitals is generally considered as shouting to people. Please avoid it on the forum. Xander is just willingly to help you
-
-
hey of course it is the same if your "if" condition is evaluated to "true", but not if its "false".
in your case if the component is not ready there might be a problem in the pother QMl files (you didn't post it here so we can't help you with that):
@
var component = Qt.createComponent("Game.qml");
@
if that doesn't create a component either the QMl engine can't find the file or there is an error in the Game.qml, in any case you should see some error in the application console? -
[quote author="petro2033" date="1396232213"]ty all, but probles was that QtGraphicalEffects needs no dll, but needs QML file from Qt\Qt5.0.2\5.0.2\mingw47_32\qml\QtGraphicalEffects[/quote]
Does that mean that you figured out the problem? If so, please mark the thread as [Solved] by editing the title of you first post.)