Lingo to match two clicked on sprites - like a matching game
by shulamis Durden
on
Jan 29, 2009 at 4:40:13 am
Hi.
I would like to write a simple lingo script to match a letter to its name. There are seven different letters with their names on the page. If a user clicks a match then he or she will go to a different frame with a 'correct message'. If the user doesn't click on a match then the user will go to another page with an 'incorrect' message.
Thanks
Shulamis
Re: Lingo to match two clicked on sprites - like a matching game by William McGrath on Feb 10, 2009 at 6:12:07 pm
You can do a single script for each pair of sprites. Simply identify them in a property such as this:
property myLetter
on beginSprite me
myLetter="a" --or whatever
end
To match the clicks, I would make two globals, say gClick1 and gClick2.
Remember to write global gClick1, gClick2 at the top
on mouseUp me
if gClick1="" then --in other words, the first slot was available (because this is click number 1)
gClick1=myLetter
else -- if the first slot was taken, then this is obviously the second click
gClick2= myLetter
--at this point you also compare them, do what has to be done with right or wrong answers, and reset the globals for the next set of clicks--
if gClick1=gClick2 then
whatever
else
whateverElse
end if
gClick1=""
gClick2=""
end if
end
There are a million ways of doing it, but I like this one.
Re: Lingo to match two clicked on sprites - like a matching game by shulamis Durden on Feb 10, 2009 at 10:39:06 pm
William,
thanks for your response.
I have some questions. I am a beginner with Lingo.
Would the script for each pair of sprites be considered a movie script?
Do I have to make a different script for each pair of sprites?
Can this script apply to letters that are from a different langauge?
Are the values for gClick1 and gClick 2 set in each global?
How do I write the globals for gClick1 and gClick2?
For the comparison part of the script do I need to use the whateverElse option? I was going to write as follows:
if gClick1=gClick2 then
play "correct"
else play "incorrect"
end if
What do I reset these values to or do I leave them as they are?
gClick1=""
gClick2=""
Re: Lingo to match two clicked on sprites - like a matching game by William McGrath on Feb 11, 2009 at 5:40:43 pm
>>Would the script for each pair of sprites be considered a movie script?
No, it's a behavior. Don't use movie scripts for this particular thing. To change from one to the other, click on the information icon.
>>Do I have to make a different script for each pair of sprites?
Yes, there are ways of doing it all in one single script, but the programming can be a lot more involved. Basically this way you'll be duplicating the same script several times, and just changing the name of the script and the value of the property.
>>Can this script apply to letters that are from a different langauge?
Yes, because what you're assigning as a value for the property is a string in quotation marks. I don't think it should give you any problems.
>>Are the values for gClick1 and gClick 2 set in each global?
They are just two globals all the time. What changes is the value. That's the difference between the global variables and the properties (every script has it's own myLetter property independent from all the rest. But gClick1 and 2 are just two variables, the same two for all the sprites.
>>How do I write the globals for gClick1 and gClick2?
Put this line at the very top:
global gClick1, gClick2
Now, when you function with those names in any part of the script, Lingo knows that you're referring to global (and not local) variables.
>>For the comparison part of the script do I need to use the whateverElse option? I was going to write as follows:
if gClick1=gClick2 then
play "correct"
else play "incorrect"
end if
<<<
Yes, I think that should work nicely
Well, else and then a new line for play "incorrect"
>>What do I reset these values to or do I leave them as they are?
gClick1=""
gClick2=""
Once you've compared them, reset them to "", yes. Otherwise, the next time you click on something, the value of the first variable will still be carried over from the previous time (remember it's a GLOBAL variable). So it could never work as it should. The idea is: the first time you click, it knows it should modify the value of the first variable, and the second time it should work with the second time. How? With the first click it looks at the first variable and says: ¿is this taken? No=OK, then this is the first click. Yes=OK, then it's the second click.
Re: scripting error for matching game by shulamis Durden on Feb 12, 2009 at 3:41:13 am
William,
I tried the following script but I am getting a script error. I am using Adobe Director 11. Is there a way to attach a file in this forum?
property myAleph
on beginSprite me
myAleph="א"
end
on mouseUp me
if gClick1="א" then -this is where the error is- variable used before assigned value
gClick=myAleph
else
gClick2=myAleph
if gClick1=gClick2 then
play "correct"
else
play "incorrect"
end if
gClick1=""
gClick2=""
end if
end
Re: scripting error for matching game by William McGrath on Feb 13, 2009 at 7:40:05 pm
property myAleph
global gClick1, gClick2 --careful with this
on beginSprite me
gClick1=""
gClick2=""
--It was giving me a problem here, so to start with let's give
--the globals a value of "" (empty)
myAleph="א"
--It doesn't have to be myBeth, myShin or whatever:
--use the same property name for all. That way you can just
--copy and paste most of this, just assign a different letter
--to the property on this line and all the rest will work fine
--Another thing: I'm not sure about non-latin characters.
--Try it, but you might have to spell them out: "aleph", "beth"...
end
on mouseUp me
--First you assign the click to one of two globals
--What this means is: the sprite that was just clicked
--is the first or the second of a pair of clicks,
--so it says that in this case, gClick1= "א" or gClick2= "א" :
if gClick1="" then
gClick1=myAleph
else
gClick2=myAleph
--If it was gClick2 means you have a pair
-- (that's why you do this within the else clause)
--so you're ready to compare them to each other
-- to see if they're both the same
--(and either way you reset them for the next set of clicks:
if gClick1=gClick2 then
play "correct"
else
play "incorrect"
end if
gClick1=""
gClick2=""
end if
end mouseUp
--
And I think that's it. It works, as far as I've tested it, except for the Hebrew characters, but I don't know if that's my system or Director.
One other problem that you might want to address is what happens if you click twice on the same sprite. As it is now it'll always get a correct answer.
You might want to add a different pair of globals just to track this:
gSpriteClicked1, gSpriteClicked2
Then on the mouseDown you have to assign it the sprite number of the sprite clicked:
gSpriteClicked1=me.spriteNum
It would be the same procedure: set them to "", then if gSpriteClicked1="" assign the spriteNum to gSpriteClicked1, else assign it to gSpriteClicked2
Then, on the else clause of the above, right before you compare the gClick globals, compare these: if they're the same, play "error" or whatever.
Again, there are a million ways of doing this, I'm just suggesting one.
Re: function error for matching game by shulamis Durden on Feb 15, 2009 at 7:02:30 pm
William,
I changed the code as you suggested. Here it is:
property myAleph
global gClick1, gClick2
on beginSprite me
gClick1=""
gClick2=""
myAleph="aleph"
end
on mouseUp me
if gClick1="aleph" then
gClick=myAleph
else
gClick2=myAleph
if gClick1=gClick2 then
play "correct"
else
play "incorrect"
end if
gClick1=""
gClick2=""
end if
end mouseUp
There is no scripting error, but when I click once it immediately takes me to incorrect. I can't click twice.
I was to suppose to add this script as behavior on the pair, correct?
Was I suppose to make gClick1,gClick2 a seperate cast member? I did the following: global gClick1, gClick2 and created a global script.
Re: function error for matching game by William McGrath on Feb 16, 2009 at 8:34:14 am
No, don't add it, substitute it. The one I sent you works. You might have to tweak some minor points, but basically it's sound.
Global variables should be invoked in the scripts where they are used (you've got great help in Director itself, maybe you should look it up). An no, there's no such thing as a global script. Therre are move scripts, but you don't need one for this.
clarification- function error for matching game by shulamis Durden on Feb 16, 2009 at 2:00:38 pm
William,
I think my question that I wrote earlier is not clear. For one set of sprites I am suppose to add this behavior. Correct? I will then substitute for a different pair of sprites. This what you were answering me. The problem is-I used this behavior on one set only to test- I was only able to click once and it went imediately to my incorrect page. What do I do to fix this problem?
Thanks
Shulamis