Need help with "disabling" a button!

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Need help with "disabling" a button!

Post by karasyrup » Fri Jan 06, 2017 5:13 am

Hi! I am extremely new to LiveCode and am working on creating a program that will do instructional trials. A student clicks the "Ready" button, and four letters appear on the screen and an audio file plays saying "Touch the Letter A". After the student clicks on one of the letters, feedback will be given based on whether the person touches the correct letter or not, then the process repeats for the next "trial", if you will. Everything works perfectly, except the "Ready" button, which is what populates the fields for the next trial, can be clicked at any point during the feedback. The feedback continues on as normal, but as soon as the feedback is finished, the next trial populates immediately. I would like for the student to have to wait for the feedback to finish before being able to click the "Ready" button, so that I know they are actually ready and are not just clicking indiscriminately. I've tried everything I can think of and everything I've been able to find by Googling - covering with another button then hiding and showing the extra button, using enable and disable functions for the Ready button, etc., and nothing seems to work. Does anyone have any suggestions? (Please pardon me if this is an incredibly dumb question, or if my code is a complete mess - I'm very very new to this and have cobbled together what I can, but I'm sure to more experienced eyes it's pretty rough).

Code: Select all

on openCard
   put field "MasterList" into field "questionBank"
  hide field "MasterList"
  hide field "questionBank"
  show button "Ready"
end openCard

on pickQuestion
   put number of lines in fld "questionBank" into lineCount
   if lineCount = 0 then
      go to card ResultsCard
      exit to top
      end if
      put random(lineCount) into currentQues
   put line currentQues of fld "questionBank" into qLine
   delete line currentQues of field "questionBank"
   put item 1 of qLine into fld "Question"
   put "answer1 answer2 answer3 answer4" into fldList
   repeat with i = 2 to 5
      get random(the number of words in fldList)
      put word it of fldList into fldName
      delete word it of fldList
      put item i of qLine into fld fldName
      if i = 2 then
         put item i of qLine into corrTarget
         set the corrAnswer of group "groupLetters" to corrTarget
      end if
   end repeat
    put 278,205 into location1
   put 732,205 into location2
   put 278,534 into location3
   put 732,534 into location4
   put fld "answer1" into currentTarget
   set the loc of group currentTarget to location1
   put fld "answer2" into currentTarget
   set the loc of group currentTarget to location2
   put fld "answer3" into currentTarget
   set the loc of group currentTarget to location3
   put fld "answer4" into currentTarget
   set the loc of group currentTarget to location4
   show group groupLetters
   show group "Animal 1"
   show group "Animal 2"
   show group "Animal 3"
   show group "Animal 4"
   hide button "Ready"
   if corrTarget = "Animal 1" then
      play audioclip "touchGo.wav"
   end if
   if corrTarget = "Animal 2" then
      play audioclip "touchRun.wav"
      end if
end pickQuestion


on checkAnswer
   put fld "Question" into CurrentTrialNumber
   put the corrAnswer of group "groupLetters" into checkWork
   put checkWork into fld "checkWork"
  if target = fld checkWork then 
     put "J" into fld "Feedback"
     put "Correct" into fld CurrentTrialNumber on card ResultsCard
     play audioclip "Praise.wav"
     hide group "groupLetters"
     show fld "Feedback"
     wait 4 seconds
     put empty into fld "Feedback"
     hide fld "Feedback"
  else
     put "Wrong" into fld CurrentTrialNumber on card ResultsCard
     if checkWork = "Animal 1" then
        play audioclip "CorrectionGo.wav"
        hide group "Animal 4"
        hide group "Animal 3"
        hide group "Animal 2"
        show group "Animal 1"
        wait 4 seconds
   end if
   if checkWork = "Animal 2" then
      play audioclip "CorrectionRun.wav"
      hide group "Animal 1"
      hide group "Animal 3"
      hide group "Animal 4"
      show group "Animal 2"
      wait 4 seconds
      end if
  end if
  hide group "groupLetters"
 
    put empty into field "Question"
   put empty into field "Answer1"
   put empty into field "Answer2"
   put empty into field "Answer3"
   put empty into field "Answer4"
   wait 1 seconds
   show button "Ready"
end checkAnswer
The code on button "Ready" is simply to do pickQuestion on mouseUp.

Many thanks in advance!!!

ClipArtGuy
Posts: 253
Joined: Wed Aug 19, 2015 4:29 pm

Re: Need help with "disabling" a button!

Post by ClipArtGuy » Fri Jan 06, 2017 6:19 am

I didn't look through your code too carefully, but to disable button "ready":

Code: Select all

set the disabled of btn "ready" to true 
to enable again just set it back to false. Hope that helps.

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Need help with "disabling" a button!

Post by Klaus » Fri Jan 06, 2017 11:05 am

Hi karasyrup,

1. welcome to the forum! :D

2. Sice we are talking about Livecode, you can also simply:
...
disable btn "your btn here..."
...
enable "you button here..."
...
:D


Best

Klaus

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Need help with "disabling" a button!

Post by dunbarx » Fri Jan 06, 2017 2:59 pm

I do not understand.

You say you have disabled the troublesome button in question. You have hidden it and "covered" it. Why have any of these techniques failed to hold off those finger-happy students?

Craig Newman

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Fri Jan 06, 2017 4:27 pm

Thanks for your suggestions! dunbarx is correct - I've disabled the troublesome button and also attempted to hide or cover it. Yet, somehow, the system is registering a click on the button even when the button is disabled, and saving it up, so to speak. Then as soon as the button is enabled, the click is processed (without the user clicking the button after it becomes enabled). Same with when another button covers it - for whatever reason, clicks on the "cover" button aren't recorded while the checkAnswer handler is running, but as soon as the "cover" button goes away at the end of that handler, the click that happened during the disabled period goes through to the Ready button.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Need help with "disabling" a button!

Post by dunbarx » Fri Jan 06, 2017 4:38 pm

Hi.

You are registering a "mouseUp" event (or whatever) on a disabled button?

And these events are recalled and reconstituted once the button is enabled??

Are you sure this is not coming from some other source?

Craig

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Fri Jan 06, 2017 4:44 pm

Essentially, yes. However, the action that the button does on mouseUp doesn't start until the button becomes enabled again. So if the button is disabled for 1 minute, and you click on that button during the 1 minute, nothing happens immediately, but as soon as the button is enabled after that 1 minute, the action that the button takes occurs immediately (without a click).

Klaus
Posts: 13806
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Need help with "disabling" a button!

Post by Klaus » Fri Jan 06, 2017 5:06 pm

karasyrup wrote:Essentially, yes. However, the action that the button does on mouseUp doesn't start until the button becomes enabled again. So if the button is disabled for 1 minute, and you click on that button during the 1 minute, nothing happens immediately, but as soon as the button is enabled after that 1 minute, the action that the button takes occurs immediately (without a click).
Nope, that is not intended and built-in behavior!
Must be something else causing this!

Some general hints:
Always use the official OF when describing "ownership" of objects!
Bad: word 2 of line 3 IN fld "xyz" ON cd "your card
Good: word 2 of line 3 OF fld "xyz" OF cd "your card

This does work now, but the engine is becoming less "forgiving" with every new version!

Use a lock/unlock screen when updating a lot of visual porperties of objets like location height etc...
This way the user will not be able to actually SEE the changes but only the immediate change of the complete screen!
Like this in your (shortened) "pickQuestion" handler:

Code: Select all

on pickQuestion
   
   lock screen
   
   put random(lineCount) into currentQues
   put line currentQues of fld "questionBank" into qLine
   delete line currentQues of field "questionBank"
   put item 1 of qLine into fld "Question"
   put "answer1 answer2 answer3 answer4" into fldList
   repeat with i = 2 to 5
      get random(the number of words in fldList)
      put word it of fldList into fldName
      delete word it of fldList
      put item i of qLine into fld fldName
      if i = 2 then
         put item i of qLine into corrTarget
         set the corrAnswer of group "groupLetters" to corrTarget
      end if
   end repeat
   put 278,205 into location1
   put 732,205 into location2
   put 278,534 into location3
   put 732,534 into location4
   put fld "answer1" into currentTarget
   set the loc of group currentTarget to location1
   put fld "answer2" into currentTarget
   set the loc of group currentTarget to location2
   put fld "answer3" into currentTarget
   set the loc of group currentTarget to location3
   put fld "answer4" into currentTarget
   set the loc of group currentTarget to location4
   show group groupLetters
   show group "Animal 1"
   show group "Animal 2"
   show group "Animal 3"
   show group "Animal 4"
   hide button "Ready"
   
   ## all screenupdates done, now:
   unlock screen
   
   if corrTarget = "Animal 1" then
      play audioclip "touchGo.wav"
   end if
   if corrTarget = "Animal 2" then
      play audioclip "touchRun.wav"
   end if
end pickQuestion

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Need help with "disabling" a button!

Post by dunbarx » Fri Jan 06, 2017 9:00 pm

, but as soon as the button is enabled after that 1 minute, the action that the button takes occurs immediately (without a click).
Ghostly, and never seen before. Are you certain that the handler in question resides in the button script, and not, say, in the card? This still would not explain the delay, unless the disabled of the button set the handler functionality. Not likely, I bet you will say.

But are you on board with the fact that either your stack is haunted, or something else is going on? Have you tried using the message watcher?

Craig

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Fri Jan 06, 2017 9:29 pm

Klaus, thank you for the suggestions! The lock screen function works great and is much cleaner in presentation.

Craig, I agree, it seems like something is haunted! I created a new button with a new name in a different part of the screen and deleted the former "Ready" button in case there was something lingering with respect to the button or the actual location, and the problem persists. I haven't tried using the message watcher previously. I just ran it, and it appears that it records mouseEnter and mouseDown after the button becomes enabled, not while the button is disabled - so if I click on the button at 3:18:22 during that wait period I described previously when the button is disabled, and the button becomes enabled at 3:18:45, the message watcher records mouseEnter, mouseDown, and mouseUp at 3:18:45, not the actual time that I clicked. It does say that it runs cREVGetsUpdate, cREVTable, and cREVGeneral on mouseDown; could those be the ghost? I'm not sure what those mean, and googling the terms isn't shedding much light on the situation for me.

SparkOut
Posts: 2839
Joined: Sun Sep 23, 2007 4:58 pm

Re: Need help with "disabling" a button!

Post by SparkOut » Fri Jan 06, 2017 10:54 pm

Without checking the script I wonder whether "flushEvents" is something to look at here. Sorry for brevity, typing on phone.

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Fri Jan 06, 2017 11:06 pm

SparkOut, you're amazing!! That worked!! I put:

Code: Select all

put flushEvents("mouseUp") into temp 


into the handler right before the button is enabled, and it functions correctly now. Thanks so much for your help, I'm so grateful!!

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Sat Jan 07, 2017 6:35 pm

Spoke too soon - the app works correctly now in LiveCode, but when I put the app onto a tester device or test in the simulator, the flushevents doesn't work. I also tried using mobileClearTouches, but that doesn't seem to work in my version of LiveCode (it's in the dictionary, but when I type it into my card script, it is recognized as a handler and not as a command. Any other possible solutions, or reasons why it would work in LiveCode but not on an iPad?

Thanks!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9578
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Need help with "disabling" a button!

Post by dunbarx » Sun Jan 08, 2017 1:40 am

Flushing events is a great way to fix many ills.

But it does not explain why these events are being triggered by a disabled button. I bet if you made a new stack with a button, and tried to duplicate this effect, you would fail.

So what is it about the stack in question? That is the question.

I do not believe in ghosts, though I am quite positive I heard one once.

Craig

karasyrup
Posts: 8
Joined: Fri Jan 06, 2017 4:57 am

Re: Need help with "disabling" a button!

Post by karasyrup » Sun Jan 08, 2017 2:41 am

I tried to see if I could recreate the phenomenon in a whole new project, and the answer was yes. I have 2 buttons: ABC and XYZ. Button ABC does pickQuestion on mouseUp, and button XYZ does submitAnswer on mouseUp. The entirety of the card script is:

Code: Select all

on pickQuestion
   disable button "ABC"
   show button "XYZ"
end pickQuestion

on submitAnswer
   hide button "XYZ"
   wait 3 seconds
   enable button "ABC"
end submitAnswer
And yet, if I click button ABC during the 3 second wait that occurs during the submitAnswer handler, the click is saved up somehow and I get the "ghost click" as soon as button ABC is enabled. The button is graying out at the right times (when it's supposed to be disabled), but the mystery clicks are still occurring. Flushevents works in the LiveCode program, but doesn't work in the simulator. I'm starting to think I'm going a little crazy or something. Again, it's possible that I'm missing something fundamental - I'm just a teacher trying to put together an app to help my kids with autism work a little more independently, so none of this is intuitive for me!

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”