Page 1 of 1
on keyDown problem
Posted: Fri May 13, 2011 12:17 pm
by leo974
Hi again

,
I have another script problem I really don't understand and do my project falling behind.
Code: Select all
on keyDown myKey
answer "test"
end keyDown
This doesn't always work...
I make a game in wich buttons appear randomly in a group and are deleted if the user click on them. Sometimes after this click, the script above stop to work and the modal displaying "test" doesn't appear anymore when I press a key; and it work again if I click on a button again...
Because my code is very long I just want to know if someone know an usual problem happen with the keyDown utilisation (with mouseUp interaction, use of send to me loops, etc...).
Thanks, Léo
Re: on keyDown problem
Posted: Fri May 13, 2011 12:55 pm
by Klaus
Hi Leo,
I think we need more info about other involved handlers etc.
"on keydown" does work in general
Best
Klaus
Re: on keyDown problem
Posted: Fri May 13, 2011 1:27 pm
by leo974
Hmm, I notice something can help you.
The problem happen when I delete the button being clicked.
There are two way to delete a button:
Code: Select all
on mouseUp
if the short name of the target contains "button" then
delete the target
end if
end mouseUp
####################################
on destroyButton
lock screen
repeat with x = the number of controls in group "groupe button" down to 1
put the cTTL of control x of group "groupe button" into current_TTL -- get the time to live of the button
if current_TTL <= 0 then
delete control x of group "groupe button"
end if
end repeat
unlock screen
send "destroyButton" to me in 100 milliseconds
put the result into destroy_timer
end destroySmiley
If the first part is not commented, the problem happen half the time and with the second one the problem looks happen when I click on the button just before he gonna be deleted ('cause of cTTL = 0).
Hope it gonna help you.
Re: on keyDown problem
Posted: Fri May 13, 2011 2:59 pm
by Klaus
Hi Leo,
leo974 wrote:...Hope it gonna help you.
not much
Why do you have two "delete" handlers here?
Does it help if you change "keydown" to "keyup"?
Just guessing...
Best
Klaus
Re: on keyDown problem
Posted: Fri May 13, 2011 4:12 pm
by leo974
Why do you have two "delete" handlers here?
One for delete the buttons when they are clicked and the other for delete them after a maximum displaying time.
Does it help if you change "keydown" to "keyup"?
No I've already tried it
After some tests I'm sure the "delete" command is the cause (directly or not) of my problem. I've used the command "hide" instead in my precedents scripts and it works correctly (but I would prefer delete them!).
Re: on keyDown problem
Posted: Sat May 14, 2011 3:17 pm
by SparkOut
one thing:
Your command starts: "on destroyButton" but closes with "end destroySmiley" (so I would imagine it doesn't even compile properly...)
another thing: (if that is not the root of the problem)
You may, as you said, be finding the problem is because of clicking with the mouse just before the time to live has expired. If the delete command is being issued because of the keypress, but the button has just been deleted by the timer, then the delete command in the keydown handler will not find a target to delete, thus generating an error. You could maybe
Code: Select all
if there is a button (the target) then
delete button id (the short id of the target)
end if
I'm not sure if there are any other issues with a handler trying to delete the object that triggers it while running.
another thing: (this is a general thought)
If there are tight repeat loops, then the engine may have a hard time coping with getting a loop done before the next loop is hard on its heels. If you put a "wait 0 milliseconds with messages" statement inside the repeat loop, it should give the engine just enough breathing space to take a gasp and deal with other things besides the repeat loop statements, ie screen redraw, keyboard polling, etc.
Re: on keyDown problem
Posted: Sat May 14, 2011 4:11 pm
by Mark
Hi,
Code: Select all
if the short name of the target contains "button" then
should be
Code: Select all
if the name of the target contains "button" then
If you delete a target whose script is running, then you get an error, because either an object whose script is running cannot be deleted or because a the script of a deleted object can't run. You will need something like this:
Code: Select all
on mouseUp
if the name of the target contains "button" then
put the short name of the target into myBtn
send "deleteTarget myBtn" to me in 0 millisecs
end if
end mouseUp
on deleteTarget theBtn
if there is a btn theBtn then delete btn theBtn
end deleteTarget
Putting the result into a variable after calling the send command doesn't do any good.
As others have mentioned already, if you start a handler with "on destroyButton" then you need to finish with "end destroyButton".
Best,
Mark
Re: on keyDown problem
Posted: Sun May 15, 2011 12:13 pm
by leo974
Hi, thanks for your answers
Your command starts: "on destroyButton" but closes with "end destroySmiley" (so I would imagine it doesn't even compile properly...)
Sorry it's a rewriting problem.
You may, as you said, be finding the problem is because of clicking with the mouse just before the time to live has expired. If the delete command is being issued because of the keypress, but the button has just been deleted by the timer, then the delete command in the keydown handler will not find a target to delete, thus generating an error.
Not really. I don't want the keyDown command delete my buttons, the 2 previous scripts are the only ones to do that, I want to do another action (for the moment just display a message with answer command as a test).
So I don't think add the code ligne "if there is a button (the target) then" could solve the problem (in fact I tried and it don't change anything).
And if we forget the second script and just keep
Code: Select all
on mouseUp
if the name of the target contains "button" then
delete the target
end if
end mouseUp
It still don't work half a time! (I click on a button -> the keyDown handler works, I click on a button again -> it stop to works).
Buttons are created with the command < create button "target" in group "groupe" > with a random position but in view of my test that's not a source of the problem.
Re: on keyDown problem
Posted: Sun May 15, 2011 2:58 pm
by Mark
leo974,
I don't understand where the keydown handler comes in. We've been talking about deleting buttons from a mouseUp script. How is this connected to keyDown??
Mark
Re: on keyDown problem
Posted: Sun May 15, 2011 3:48 pm
by leo974
How is this connected to keyDown??
It's not... I just said the keyDown command doesn't work sometimes when I click on a button (the result is delete it) until I click on a button again.