macromedia.director.lingo
[Top] [All Lists]

Re: Text Input - Entering and Checking

Subject: Re: Text Input - Entering and Checking
From: Dave C <no@xxxxxx>
Date: Fri, 25 Jul 2008 11:11:17 -0400
Newsgroups: macromedia.director.lingo


I thought you might be interested in learning a more efficient method of doing this. What you are doing will work but it has some disadvantages. It appears you are storing the correct answer in a cast member. This means you will need a unique cast member for each answer. Also your script is using hard coded member names, which means you will need a unique script for each and every question. What you will find is that each script for the questions is almost identical, except for the member names.

With Lingo you have the ability to define your own custom "properties" that allow you to reuse the same behavior on every question. It won't matter if you have 1 question or 100, you really only need one behavior.

The trick is to use a function called "getPropertyDescriptionList". This allows you to define any number of properties that you need for a sprite. When you apply the behavior to a sprite, Director will prompt you with a dialog box that gives you the opportunity to assign the unique values to the properties, such as the correct answer for this particular question. You can change the value of any parameter later through the property inspectors behavior tab if you need to. It also allows you to define default values for your properties. So for example your behavior can play a sound called "wellDone" for 99 questions by default, but could play a sound called "greatJob" for one particular question.

Trust me, once you understand how to do this you will wonder how you ever got along without it. It is not difficult at all either. So here a example, implementing the functionality you are trying to create. Using just the two behaviors below, one "check answer" button cast member and one text member for user input, you can create a quiz with any number of questions.

So here is the behavior you attach to your check answer button. When Director prompts you, set the properties or accept the defaults. Warning, long lines in this code will get word wrapped in your news reader or browser. When you paste it into Director you will have to figure out where these line breals got inserted and remove them.

---
property correctInput, correctSoundMember, incorrectSoundMember, inputTextMember, soundChannelNum

on mouseUp me
  if member(inputTextMember).text = correctInput then
    sound(soundChannelNum).play(member(correctSoundMember))
  else
    sound(soundChannelNum).play(member(incorrectSoundMember))
  end if
end

on getPropertyDescriptionList me
  description = [:]
addProp description,#correctInput, [#default:"default", #format:#string, #comment:"Expected user input"] addProp description,#correctSoundMember, [#default:"wellDone", #format:#string, #comment:"Sound member name to play if correct"] addProp description,#incorrectSoundMember, [#default:"wrongChoice", #format:#string, #comment:"Sound member name to play if correct"] addProp description,#inputTextMember, [#default:"inputTextMember", #format:#string, #comment:"Name of the input text member"] addProp description,#soundChannelNum, [#default:1, #format:#integer, #comment:"The sound channel to play"]
  return description
end
---

You can reuse the the same text input member for each question, but will need a simple behavior to clear it out between questions.

---
property spriteNum

on beginSprite me
  sprite(spriteNum).member.text = ""
end

on endSprite me
  sprite(spriteNum).member.text = ""
end
---

If you prefer I can email you a simple director movie demonstrating this. I guarantee you that doing it this way will save you a lot of time and effort.


<Prev in Thread] Current Thread [Next in Thread>