Hi There
Just to finish this Thread
Here is my code that solved the problem - Im sure there is a more effective way.
What I discovered that made my head hurt for a long time was what virtualNomad pointed out in the post:
findString() returns an index that begins at 1 - so the first letter in a string begins at 1
but getTextCharX() and getTextCharY() uses an index that starts at 0 - so the first char begins at 0
I guess the reason for this is that findString() belongs to the string commands and getTextCharX() belongs to the text command
When I understood that I could fin my workaround . Here is the code.
SetErrorMode(2)
// set window properties
SetWindowTitle( "test" )
SetWindowSize( 1024, 768, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetDisplayAspect( 4.0/3.0 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 30, 0 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 )
Global Left#=20.0
Global Top#=30.0
delimiters$ =",.-:;!"+chr(10)+chr(32)
// Create a string and a text object to hold it
myString$="Once upon a time there was an old sow with three little pigs, "
myString$=myString$+"and as she had not enough to keep them, she sent them out to seek their fortune."
myText= CreateText(myString$)
SetTextPosition(myText,Left#,Top#)
SetTextMaxWidth(myText,40.0)
SetTextSize(myText,4.0)
global myText
// Create a type array that will contain each token(word) in myText and the x and y position of each token
Type word
token$ as string
x# as float
y# as float
textObject as integer
index as integer
endtype
global token as word[0]
// Put the tokens and their x,y position into the array.
// Create a new text object for each token
tokenAmount = CountStringTokens(getTextString(myText),delimiters$)
token.length = tokenAmount
tIndex=1
For t=1 to tokenAmount
token[t].token$ = getStringToken(getTextString(myText),delimiters$,t)
// This is where I got problems. FindString is a command that belongs to
// the string commands and returns an index that begins at 1
token[t].index = FindString(getTextString(myText),token[t].token$,1,tIndex)
// The GetTextCharX or GetTextCharY belongs to the text commands and
// returns an index that begins with 0 - that is why I decrease 1 to match up
// with the findString command
token[t].x# = GetTextCharX(myText,token[t].index-1)
token[t].y# = GetTextCharY(myText,token[t].Index-1)
token[t].textObject = CreateText(token[t].token$)
setTextSize(token[t].textObject,4.0)
SetTextPosition(token[t].textObject,token[t].x#+Left#,token[t].y#+Top#)
SetTextColor(token[t].textobject,140.0,140.0,255.0,0.0)
// Since findString is a string command it can in some cases
// find words within words as in "is" in "this"
// to make sure this doesnt happen I make sure that I update the
// value tIndex to the latest findString index and add the length of
// the token for the NEXT FOR loop.
tIndex = token[t].index + len(token[t].token$)
next
do
Hilite()
sync()
loop
function Hilite()
for t=1 to token.length
if getTextHitTest(token[t].textObject,getPointerX(),getPointerY())
SetTextColorAlpha(token[t].textObject,255.0)
else
SetTextColorAlpha(token[t].textObject,0.0)
endif
next
endfunction
Thanks for the help.
Tim