Copy link to clipboard
Copied
I am trying to make an "achievements" page for my game, where if the user has a high score greater than 100, then the achievement can be unlocked. I tried using a code like this, but it didn't seem to work!
function Check();
if(endscreen_mc.highscore_txt > 100)
{
medals.roachLock.visible = false;
}
else if(endscreen_mc.highscore_txt < 100)
{
medals.roachLock.visible = true;
}
else if(endscreen_mc.visible == 100)
{
medals.roachLock.visible = true;
}
The high score value is saved inside a shared object. What's wrong with my code, and what can I do to fix it?
Copy link to clipboard
Copied
What is endscreen_mc.highscore_txt ? Is it a textfield?
Copy link to clipboard
Copied
Yes, it's a text field. My last conditional was incorrectly typed..it's really "else if(endscreen_mc.highscore_txt == 100) I
Copy link to clipboard
Copied
As Rob described, you need to compare the text property of the textfield and it has to be treated as a numeric value.
You don't need that third condition. You can change the second one to be...
...
else if(Number(endscreen_mc.highscore_txt.text) <= 100)
{
medals.roachLock.visible = true;
}
Copy link to clipboard
Copied
I tried doing that, but it still didn't work..this is what's inside my endscreen layer
function showresults():void
{
Rchannel.stop();
// display current score
endscreen_mc.scoreR_txt.text = String(score);
// calculate, display and save (if necessary) a new highscore
var so:SharedObject = SharedObject.getLocal("alltimeHighScore");
if (!so.data.score || score > so.data.score)
{
endscreen_mc.highscore_txt.text = String(score);
so.data.score = score;
so.flush();
Check();
}
else
{
endscreen_mc.highscore_txt.text = String(so.data.score);
}
if(so.data.score==score)
{
endscreen_mc.score_txt.text = String(score);
endscreen_mc.highscore_txt.text = String(score);
}
}
Copy link to clipboard
Copied
Try pputting a trace in that function to make sure it is getting called when you expect it should. If this is anything like yesterday's issue, you might not be calling that function at all.
Copy link to clipboard
Copied
Well the trace statements are working, but it's just that the value is really 111, but it's thinking that it's less than 100..so it's calling on a wrong conditional for some reason
Copy link to clipboard
Copied
Have you traced the value you think is 111?
Copy link to clipboard
Copied
Yes. Flash seems to think my highscore_txt value on a different layer, is 0. The highscore_txt is a dynamic layer and populates when the game is finished.
Copy link to clipboard
Copied
Would get the value placed inside the shared object work? If so, how do you do that?
Copy link to clipboard
Copied
Are you seeing any errors when you run that?
Is the instance name of the score text "highscore_txt? If thats's the case then you want to compare "endscreen_mc.highscore_txt.text". And you need to compare it to a string. Since you're comparing strings, you can't directly use a greater than - less than method of comparison. You'll have to convert the string in the textfield to a number to do the comparison.
if(Number(endscreen_mc.highscore_txt.text) > 100)
Copy link to clipboard
Copied
I tried doing this, but it's still not working..
function Check():void
{
if(Number(endscreen_mc.highscore_txt.text) > 100)
{
medals_mc.roachLock.visible = false;
medals_mc.medal_mc.visible = true;
}
else if(Number(endscreen_mc.highscore_txt.text) <= 100)
{
medals.roachLock.visible = true;
}
}
This is what's inside my endscreen layer
function showresults():void
{
Rchannel.stop();
// display current score
endscreen_mc.scoreR_txt.text = String(score);
// calculate, display and save (if necessary) a new highscore
var so:SharedObject = SharedObject.getLocal("alltimeHighScore");
if (!so.data.score || score > so.data.score)
{
endscreen_mc.highscore_txt.text = String(score);
so.data.score = score;
so.flush();
Check();
}
else
{
endscreen_mc.highscore_txt.text = String(so.data.score);
}
if(so.data.score==score)
{
endscreen_mc.score_txt.text = String(score);
endscreen_mc.highscore_txt.text = String(score);
}
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now