[SOLVED]Problem with counter.
by Vlad I · in Torque Game Builder · 04/20/2012 (11:09 pm) · 10 replies
I'm trying to make counter of whacked objects.
I made an object Sc1 with class Scorpion01 and text object Whacked with Counter class.
I want to decrement Whacked count by one when I click on Sc1. But it doesn't update. Console doesn't show any error. What gives?
I'm using this code:
I made an object Sc1 with class Scorpion01 and text object Whacked with Counter class.
I want to decrement Whacked count by one when I click on Sc1. But it doesn't update. Console doesn't show any error. What gives?
I'm using this code:
function Scorpion01::onLevelLoaded(%this, %scenegraph)
{
P1.attachObject(Sc1, 10, -1, 2, 3, "restart", -1, true);
}
function Counter::onLevelLoaded(%this, %scenegraph)
{
%this.whackedCount = 15;
// Reset the GuiTextCtrls
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
}
function Scorpion01:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sc1))
{
%this.whackedCount--;
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
}
}About the author
#2
If the counter is attached to the scorpion (part of the class?) you should change line 18 to "%this.counter.whackedCount--;" ... or whatever, depending on the counter object's name.
Alternately just skip the counter 'class' and give the scorpion a whackedCount. Then you'd be back to %this.whackedCount--, and the Scorpion could track his own?
04/21/2012 (2:20 pm)
Seems to me you're setting your Counter object's whackedCount to 15... but then in the onMouseDown you're decrementing the SCORPION object's whackedcount (which defaults to zero... hence the -1.)If the counter is attached to the scorpion (part of the class?) you should change line 18 to "%this.counter.whackedCount--;" ... or whatever, depending on the counter object's name.
Alternately just skip the counter 'class' and give the scorpion a whackedCount. Then you'd be back to %this.whackedCount--, and the Scorpion could track his own?
#3
GuiWhacked is a text object with class Counter.
Your alternative way works, but only works for one scorpion.
here is what I have now if I use it for 2 or more scorpions:
when I click on Sc1 it decrements 1 to 14, but doesn't decrement to 13 when I click on Sc2, it just stays 14 no matter how many times I click on Sc2. Unless I click one more time on Sc1, then the score decrements to 13 and only then I can click on Sc2 but when I click on it the score changes to 14 and later if I click again on Sc1 the score changes to 12.
Some weirdness happens.
Also if I click few times on Sc1 let's say 5 times it would decrement to 10, then I click on Sc2 the score changes to 14, if I after click on Sc1 it changes to 9. You see what I mean?
04/21/2012 (11:33 pm)
Hi TimGuiWhacked is a text object with class Counter.
Your alternative way works, but only works for one scorpion.
here is what I have now if I use it for 2 or more scorpions:
function Scorpion01::onLevelLoaded(%this, %scenegraph)
{
P1.attachObject(Sc1, 10, -1, 2, 3, "restart", -1, true);
%this.whackedCount = 15;
// Reset the GuiTextCtrls
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
}
function Scorpion01:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sc1))
{
%this.whackedCount--;
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
// playEffect01(%this);
// %this.safeDelete();
}
}
function Scorpion02::onLevelLoaded(%this, %scenegraph)
{
P1.attachObject(Sc1, 10, -1, 2, 3, "restart", -1, true);
%this.whackedCount = 15;
// Reset the GuiTextCtrls
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
}
function Scorpion02:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sc2))
{
%this.whackedCount--;
GuiWhacked.text = "Whacked:" SPC %this.whackedCount;
// playEffect01(%this);
// %this.safeDelete();
}
}So, what happens now with my score is this:when I click on Sc1 it decrements 1 to 14, but doesn't decrement to 13 when I click on Sc2, it just stays 14 no matter how many times I click on Sc2. Unless I click one more time on Sc1, then the score decrements to 13 and only then I can click on Sc2 but when I click on it the score changes to 14 and later if I click again on Sc1 the score changes to 12.
Some weirdness happens.
Also if I click few times on Sc1 let's say 5 times it would decrement to 10, then I click on Sc2 the score changes to 14, if I after click on Sc1 it changes to 9. You see what I mean?
#4
It looks like it starts decrementing clicks on the first Sc1 object and memorizes it. If you start clicking Sc2 object it memorizes clicks for itself too. And changes the display score according to the amount of clicks it received for Sc1 and for Sc1.
So it displays 2 separate click results as soon as you click on either object.
How to combine it all in one?
04/23/2012 (4:05 am)
Confused, really need help on this one, having nightmares that I decremented something wrong and started nuclear war.It looks like it starts decrementing clicks on the first Sc1 object and memorizes it. If you start clicking Sc2 object it memorizes clicks for itself too. And changes the display score according to the amount of clicks it received for Sc1 and for Sc1.
So it displays 2 separate click results as soon as you click on either object.
How to combine it all in one?
#5
I'm not even sure it's possible to use functions the way you are currently... unless you have two separate classes Scorpion01 and Scorpion02 -- which would be a terribly odd way to go about it.
Assuming all scorpions will be the same, you should only have one of each function:
function Scorpion::onLevelLoaded()
and
function Scorpion::onMouseDown()
Are you trying to get the counter to decrement no matter which one you click on? So each scorpion doesn't have a count of 15 but they have a total of 15 between the two of them?
Also (not sure it'll help but...) in line 26 you've got a "Sc1" in there...
04/23/2012 (6:58 am)
Vlad,I'm not even sure it's possible to use functions the way you are currently... unless you have two separate classes Scorpion01 and Scorpion02 -- which would be a terribly odd way to go about it.
Assuming all scorpions will be the same, you should only have one of each function:
function Scorpion::onLevelLoaded()
and
function Scorpion::onMouseDown()
Are you trying to get the counter to decrement no matter which one you click on? So each scorpion doesn't have a count of 15 but they have a total of 15 between the two of them?
Also (not sure it'll help but...) in line 26 you've got a "Sc1" in there...
#6
So, I have 15 scorpions I want to kill by clicking on them and the score should decrement 1 by 1.
Yeah, your logic is sound and I was gonna do it at first just like that with one
function Scorpion::onLevelLoaded()
and
function Scorpion::onMouseDown()
,it does make sense, however some scorpions are attached to different paths and have set speeds and directions on level load.
Do you think i should take them all and put into one Scorpions ::onLeveloaded() and Scorpions :: onMouseDown()
Wouldn't it interfere?
yes, line 26 should be Sc2, I typed my code wrong, sorry.
04/23/2012 (10:08 am)
Yes Tim, I'm trying to decrement no matter which one I click on. Like in the mole tutorial. And they all have 15 between them.So, I have 15 scorpions I want to kill by clicking on them and the score should decrement 1 by 1.
Yeah, your logic is sound and I was gonna do it at first just like that with one
function Scorpion::onLevelLoaded()
and
function Scorpion::onMouseDown()
,it does make sense, however some scorpions are attached to different paths and have set speeds and directions on level load.
Do you think i should take them all and put into one Scorpions ::onLeveloaded() and Scorpions :: onMouseDown()
Wouldn't it interfere?
yes, line 26 should be Sc2, I typed my code wrong, sorry.
#7
Just make it global!
When the level starts up:
$g_WhackCount = 15;
Then in mousedown:
$g_WhackCount--;
GuiWhacked.text = "Whacked:" SPC $g_WhackCount;
And you're good to go.
As for setting your scorpions on different paths and whatnot, I'd have to see the code before I could really say much... but you should definitely only have one scorpion::onLevelLoaded() etc..
If you're attaching them to paths and things in the editor they'll still be OK and remain on those paths. It's the OBJECT (each scorpion) that is attached to a path, not the CLASS (Scorpion::).
-T
04/23/2012 (10:33 am)
If you only need one of something (like a whackedCount)... Just make it global!
When the level starts up:
$g_WhackCount = 15;
Then in mousedown:
$g_WhackCount--;
GuiWhacked.text = "Whacked:" SPC $g_WhackCount;
And you're good to go.
As for setting your scorpions on different paths and whatnot, I'd have to see the code before I could really say much... but you should definitely only have one scorpion::onLevelLoaded() etc..
If you're attaching them to paths and things in the editor they'll still be OK and remain on those paths. It's the OBJECT (each scorpion) that is attached to a path, not the CLASS (Scorpion::).
-T
#8
04/23/2012 (10:51 am)
I'm trying this and as soon as I click on either scorpion the score (15) disapearsfunction Counter::onLevelLoaded(%this, %scenegraph)
{
$g_whackedCount = 15;
// Reset the GuiTextCtrls
GuiWhacked.text = "Whacked:" SPC $g_whackedCount;
}
function Scorpion01::onLevelLoaded(%this, %scenegraph)
{
P1.attachObject(Sc1, 10, -1, 2, 3, "restart", -1, true);
}
function Scorpion01:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sc1))
{
$g_whackedCount--;
GuiWhacked.text = "Whacked:" SPC $g_WhackCount;
// playEffect01(%this);
// %this.safeDelete();
}
}
function Scorpion02::onLevelLoaded(%this, %scenegraph)
{
P1.attachObject(Sc1, 10, -1, 2, 3, "restart", -1, true);
}
function Scorpion02:: onMouseDown (%this, %modifier, %worldPosition, %clicks)
{
if(isObject(Sc2))
{
$g_whackedCount--;
GuiWhacked.text = "Whacked:" SPC $g_WhackCount;
// playEffect01(%this);
// %this.safeDelete();
}
}
#9
Thank you so much Tim! it does work now.
License me your HO framework! :D
04/23/2012 (10:54 am)
ops Sorry typo :D , I spotted it $g_whackedCount and $g_whackCount differentThank you so much Tim! it does work now.
License me your HO framework! :D
Vlad I
Default Studio Name
So, here is what happens now. When I click on it, it updates the counter straight to -1, then -2, -3 and so on.
All I want to do is to make it tick back from 15 to 0.
What am I missing here?