Skip to main content
Inspiring
August 4, 2006
Question

Recognise word in text block and then change it's colour

  • August 4, 2006
  • 21 replies
  • 3103 views
Hey there helpful people.

Got another doozy for you.

If i have a block of text how can i get flash to recognise a word in that block of text (when it's clicked) and then change colour?

Will it need to recognise how many characters along the text block, before it executes a change in colour?

Does this code help or is it a bit of a run-around to the problem ( http://www.senocular.com/flash/actionscript.php?file=ActionScript_1.0/Prototypes/TextField/wordSelect.as)

This topic has been closed for replies.

21 replies

Inspiring
May 16, 2007
Wow Craig I totally forgot about this one.

How do you track previous questions? This question was like a month or two ago.

Anyhow thanks for that. It works a charm! And I understand you're reasoning behind it too (which is amazing for me :D)

But yeah I'm up to Chapter 7 Objects and Classes although I think I'm going to have to go over Functions again in Chapter 6. It's been a while since I've looked at it due to work commitments (which is possibly looking like a lot of 3D work coming up), but thanks for asking.
Craig Grummitt
Inspiring
May 16, 2007
sorry for late reply i've had my head in flex... yes you could easily modify this code for sentences by replacing the line:

if (r == " ") {

with:

if (r == "." || r == "?") {

how's the actionsscript book going?
Inspiring
April 11, 2007
Is there anyway to use this code to select separate sentences rather than separate words?
Craig Grummitt
Inspiring
August 18, 2006
twistedpancreas, the code recognises when a word is selected in the runWordSelect method, which sets the element in the wordSelected array for the currently selected word to either true or false on lines 44 and 48 of the code.

NSurveyor that looks very interesting but twistedpancreas wants every word clickable, and since the other code is working to twistedpancreas' satisfaction and i'm exhausted(!) i'm going to leave it there...

twistedpancreas good luck with the actionscript book!
Inspiring
August 17, 2006
ok i'm slowly getting there and snippets of code are vaguely making sense to me so here's what I've come up with so far:

to have an array with all the words, ie:

var myArr:Array = newArray (textbox.wordSelected[1], textbox.wordSelected[2], textbox.wordSelected[3]);

then have another array, on the checking button, looking for a particular value/s (im not sure how to values so i'll only do one here), ie:

var searchString:String = textbox.wordSelected[1]
var matchesFound:Number = 0;
for (var j:Number = 0; j < myArr.length; j++) {
if (myArr == searchString) {
_root.feedback.gotoAndStop("correct");
}
else
{
_root.feedback.gotoAndStop("incorrect");
}

so basically the first word out of the three word sentence will be correct. i probably don't have the syntex right, but is this heading in the right direction?

But in regards to you're comments on the post before about the 3 word sentence, i swapped the correct and incorrect code around and was getting similar results.
Craig Grummitt
Inspiring
August 18, 2006
i don't really understand what you're attempting there? with the code you've written your checking sequence is always going to come up with correct. this is because you're setting a variable to something and then checking if it equals that same something... make sense? eg. in simple terms you've said:
a=b
if a==b ...

(if you're confused i'm referring to the following line, when j=0)
if (myArr == searchString) {

in addition you've set a variable matchesFound that though it may been a good idea, you're not using elsewhere...

ok i see you're trying though, yet struggling, so i think it might be time for another big push in the right direction. remember when i said this?
>>>>but probably an easier way is set them both Array.toString() because you can compare two strings to eachother. eg. if (array1.toString()==array2.toString()) {

here's an implementation of that(see attached code). the main differences are:
as i suggested in an earlier post, i've initialised the wordSelected array with false.
i've set the text field to a shorter sentence to make this simpler for testing.
i've created an array called correctWordSelected which you'll have to update with the words that should be selected.
the checking code compares the two arrays by converting them to strings first.
i've changed the selection color to red for no apparent reason...
Inspiring
August 18, 2006
Yeah sorry my knowledge in this area is really lacking, but never fret I have "Actionscript for Flash 8" sitting beside me, and hopefully after going through that and from what I've read up in LiveDocs I can stop asking frustrating questions :D

But yeah I was a bit lost as to what to do. Really "myArr" and "matchesFound" were both pointing to nowhere ("myArr" really should have been "textbox", but that still probably wouldn't have made sense).

So how does you're code realise that a wordSelected becomes true? I can see it turning all text to false, and then later checking against correctWordSelected, but I can't see how it's recognising a selected word to true. Maybe i haven't grasped the idea of Array.toString()

Thanks for all this btw.

PS it's working too :D
Craig Grummitt
Inspiring
August 16, 2006
afraid not only does the syntax of your code need work, you're barking up the wrong tree... 'switch' really doesn't do much that 'if' can't do - it's just a slightly more succinct approach in cases where you're only checking the contents of one variable. The 'default' option is equivalent to the 'if' statement's 'else'.

we however, want to check the contents of several variables.

by the way, i've wondered whether your post a few posts back where you were experimenting with a three word sentence whether you just had correct and incorrect around the wrong way. just have a close look at the logic of your 'if' statement and you should be able to work it out. translate it into english if it helps.
eg. if word 1 is selected and word 2 is selected and word 3 is not selected then...(i don't know what comes next in your code as it stands!)
Craig Grummitt
Inspiring
August 15, 2006
i'm sure you'll work it out. remember you can trace variables to see what they contain at runtime or you can use the debugger.
Inspiring
August 16, 2006
Ok I've been reading through LiveDocs and am about to get to Loops, but I came across Switches and was wondering if we could use this when the check button is clicked:

switch (condition) {

case A :
(this._parent.textbox.wordSelected[2]!=true || this._parent.textbox.wordSelected[4]!=true)
_root.feedback.gotoAndStop("correct");
break;

default :
_root.feedback.gotoAndStop("incorrect");
break;
}

I'm not sure what the condition would be, but i noticed in LiveDocs they had an example where if you didn't click any keyboards keys then the code automatically went to "default", so I was wondering if we could do the same here.

But I'll be reading up on loops tomorrow, but for now it's home time :)

Inspiring
August 14, 2006
uh oh,

found a problem.

Say you do select the 2nd and 4th word and then select the 5th word then it's still seeing that as a correct answer :(
Craig Grummitt
Inspiring
August 14, 2006
ok if you need to check not only that some words are selected but also that some words are not, then your checking section is just going to have to be a longer if statement. perhaps something like:

if (this._parent.textbox.wordSelected[2]==true && this._parent.textbox.wordSelected[4]==true && this._parent.textbox.wordSelected[5]!=true && etc) {
//correct words selected
}

by the way, i am writing !=true instead of ==false because we don't have any initialisation of these variables at the beginning. how the code is set up at the moment, if a word is not selected it could either equal false, or undefined. initialisation would involve a for loop looping through all the variables in the array(of which there are as many as there are words in the text box) and setting them all to false. if you did this, then you could set up your checking code differently.

for example, you could set up a 'correct words' array(which is basically how you want your 'selected words' array to look when the correct words are selected). you could then check if your 'selected words' array is equivalent to the correct words array. unfortunately you can't just say if (array1==array2) - well you can but two identical arrays won't return true.

its okay though - there are ways you can do this. you could use a for loop to loop through both arrays comparing each element to the equivalent in the other array looking for discrepancies. but probably an easier way is set them both Array.toString() because you can compare two strings to eachother. eg. if (array1.toString()==array2.toString()) {

see how you go with that information...
Inspiring
August 15, 2006
quote:

Originally posted by: Craig Grummitt
if (this._parent.textbox.wordSelected[2]==true && this._parent.textbox.wordSelected[4]==true && this._parent.textbox.wordSelected[5]!=true && etc) {
//correct words selected
}


Thanks Craig,

I tried this on a 3 word sentence (with 2 words to be selected for a correct answer),ie :

if (this._parent.textbox.wordSelected[1]==true && this._parent.textbox.wordSelected[2]==true && this._parent.textbox.wordSelected[3]!=true

and was getting weird stuff like:

if you select no words and click the check button then it says correct

if you still selected one correct word but not the other it was saying correct

But I'll get back to you on the other things you suggested. Bit busy at the moment, but i really appreciate you're time and explainations.
Inspiring
August 14, 2006
sorry are you saying

this._parent.textbox.wordSelected[2] || this._parent.textbox.wordSelected[4]
if the second word is selected or the fourth word is selected(or both!)

will bring a true result?

and

this._parent.textbox.wordSelected[2]!=true || this._parent.textbox.wordSelected[4]!=true
if the second word is not selected or the fourth word is not selected(or neither!)

will bring a false result?
Craig Grummitt
Inspiring
August 14, 2006
it really depends what words are selected...?

do you understand that i was saying that what you wrote:
>>if (this._parent.textbox.wordSelected[2]!=true || this._parent.textbox.wordSelected[4]!=true) {

...can be translated into english like so:
if the second word is not selected or the fourth word is not selected(or neither!)
Inspiring
August 14, 2006
quote:

Originally posted by: Craig Grummitt
...can be translated into english like so:
if the second word is not selected or the fourth word is not selected(or neither!)


then the result on the screen will be incorrect (taking into account all the other code we have)

right?

whereas on the other hand if if the second word is selected along with the fourth word then the result on the screen will be correct (taking into account all the other code we have)

Craig Grummitt
Inspiring
August 14, 2006
good work! i knew you'd get a better sense of satisfaction if you worked it out yourself! your two last posts in english for you:

this._parent.textbox.wordSelected[2] || this._parent.textbox.wordSelected[4]
if the second word is selected or the fourth word is selected(or both!)

this._parent.textbox.wordSelected[2]!=true || this._parent.textbox.wordSelected[4]!=true
if the second word is not selected or the fourth word is not selected(or neither!)