SpriteFont work around... [SOLVED]

For questions about using Classic.

Post » Fri Apr 08, 2011 7:23 pm

I've been having a lot of trouble with apparent bugs in SpriteFont recently, to make a long story short, and I think it's time to find an alternate way to accomplish the same effect using events.

I was using it for my Mario game to represent the number of lives and coins you have, as well as your score and time remaining. These numbers (score in particular) can get pretty large, so I'm looking for a way to change sprites according to the value of a global variable that can get as big as, say, a billion.

I'm... not even sure how to ask the question, so hopefully someone knows what I mean and can help me out with it. :oops:
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Fri Apr 08, 2011 8:45 pm

Here are two examples that may be helpful:
[url:1p902c5k]http://www.scirra.com/forum/viewtopic.php?f=8&t=7730[/url:1p902c5k]
[url:1p902c5k]http://www.scirra.com/forum/viewtopic.php?f=8&t=1995[/url:1p902c5k]
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Fri Apr 08, 2011 9:21 pm

Well, I've been staring at the non-Python one for a good ten minutes now and I think I got a decent handle on it. Not really sure how the hash table thing works, since I never worked with those before... It's mostly this line I don't get:

imageFont: Set 'index' to HashTable(Uppercase(mid(Function.Param(1), LoopIndex, 1)))-1

And the parts where you "destroy" imageFont, yet it's still there... What's all this about, if you don't mind explaining a bit? :)
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Sat Apr 09, 2011 12:44 am

[quote="SullyTheStrange":1go1pued]It's mostly this line I don't get:

imageFont: Set 'index' to HashTable(Uppercase(mid(Function.Param(1), LoopIndex, 1)))-1[/quote:1go1pued]
The hash table is an array where you access its content by keys instead of an index. So instead of index-value-pairs you have key-value-pairs. ROJOhound cleverly used the characters as keys and made the values being the index ('A' = 1, 'B' = 2, etc.)
Every 200ms the text box changes its content, like so:
0ms -> "T"
200ms -> "Th"
400ms -> "Thi"
600ms -> "This"
...
Now he destroys every instance of 'imageFont' and calls the function. The function determines the current length of the text in the text box and in a loop creates one 'imageFont'-object per character.
He now needs to get the value of a key that corresponds to the actual character in the loop. These characters can be lowercase (e.g. 'h'), but the keys he generated are all uppercase (see event 2). Whatever character he gets, he needs to make sure it is uppercase. The expression 'convert to uppercase', => uppercase(string), does exactly that.
mid returns a substring of a string at position p and with the length l: mid(text, p, l). this is 1-based. Something like mid("Four", 4, 1) returns 1 character starting at position 4, which is "r".
For example, at 600ms, the text of the text box is "This". The loop starts at 1 and ends at len(text), which is 4 in this case. In the first iteration of the loop, mid(Function.Param(1), LoopIndex, 1) equals mid("This", 1, 1) and therefore returns "T". The second iteration equals mid("This", 2, 1) which returns "h", etc. As mentioned earlier, that string is converted to uppercase, which returns "T", "H", etc.
He now has exactly one character of the text, and that character is uppercase. He can now use this character as a key of the hash table to get its value (which is the index of that char). For example, HashTable("T") was set to the value 20 in event 2.
mid (which he used to create the key-value-pairs in event 2) is 1-based, while image offset is 0-based. That's why 1 is substracted from the value of the key.

[quote="SullyTheStrange":1go1pued]And the parts where you "destroy" imageFont, yet it's still there...[/quote:1go1pued]
The whole concept of this is to not have one imageFont, but one instance of imageFont per character. These are rebuilt every 200ms, that's why all of them need to be destroyed before creating them again.
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Sat Apr 09, 2011 9:04 am

Superb explanation tulamide, much better than what I would have written.

*tips hat*
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sun Apr 10, 2011 1:20 am

Okay, after fixing a handful of other issues, I can finally get around to doing this. And, uhh... that's quite an explanation. :shock:

I more or less understand most of it now... The way I'm doing it, though, is to have it set to display the contents of a variable constantly rather than every 200 ms. Would I need to destroy and recreate each digit every tick?
B
20
S
9
G
6
Posts: 607
Reputation: 6,112

Post » Sun Apr 10, 2011 2:04 am

The example I posted probably does more than what you need. Like letters and writing one letter at a time.

Here is another example that is simpler and easier to set up:
It uses a 10 frame sprite for the digits and a text object to store the origin and value.
[url:2hiot28g]http://dl.dropbox.com/u/5426011/examples4/simpledigits.cap[/url:2hiot28g] made in 0.99.97

Just change the text and the number will change.
B
79
S
24
G
54
Posts: 4,746
Reputation: 40,755

Post » Sun Apr 10, 2011 3:25 am

Awesome. :) Altered a bit for what I need, and it works perfectly. Thanks!
B
20
S
9
G
6
Posts: 607
Reputation: 6,112


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 8 guests