Arrowkey

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

Post Reply
chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Arrowkey

Post by chris25 » Sat Dec 07, 2013 1:56 pm

Code: Select all

on  mouseUp
   arrowKey pKey
   put the loc of btn "mine1" into startLoc
   repeat until intersect (btn "mine1" , btn "start") = true
      get the loc of btn "mine1"
      set the loc of btn "mine1" to (item 1 of it) & "," & (item 2 of it + 1)
      wait 1.5
   end repeat
   set the loc of btn "mine1" to startLoc
end mouseUp

on arrowKey pkey
   if pkey = left then
      put the loc of btn "sub" into subLoc
      repeat
         get the loc of btn "sub"
         set the loc of btn "sub" to (item 1 of it - 1) & "," & (item 2 of it)
      end repeat
   end if
end arrowKey
Please tell me what is wrong. The arrow left does not work, so what is missing? I originally did it without the arrowkey handler in the mouseup handler. That's why it is there now.
chris

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

Re: Arrowkey

Post by SparkOut » Sat Dec 07, 2013 4:17 pm

The on arrowkey handler is probably in the wrong place. If that handler is in the button script where you have the mouseUp handler then it won't be triggered - move the arrowkey handler to the card script and you should find it receives the keyboard messages when arrow keys are pressed. Also use quotes - if pKey = "left" then...

arrowkey is not a command, it is a message to be caught by a handler (that brings the parameter of which arrowkey is pressed along with it) , so you can't fire it off like that statement in the mouseUp handler.

Also the arrowkey handler contains an endless tight repeat loop so if the left arrow key message does get handled, it will lock up the computer...

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Arrowkey

Post by dunbarx » Sat Dec 07, 2013 5:32 pm

Chris.

What Sparkout said.

This is part of your ongoing efforts with the hierarchy. Think about it. The arrowkey message originates from the keyboard. There is no way that LC would be able to trap it if you place that handler in some button script. The natural first entry into the hierarchy for such a message would be the card.

Now that said, realize also that if the cursor is blinking in an editable field, then the arrowkey message is indeed sent to that field, since those four keys also have functionality within such a field. Just another wrinkle, but you are used to that.

But at heart, the concept should stand; a keyboard action is quite unattached to any arbitrary button, and in fact naturally belongs "higher".

Craig

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Arrowkey

Post by chris25 » Sat Dec 07, 2013 5:50 pm

This fails now?

CARD SCRIPT

Code: Select all

--on mousemove
put the mouseloc into msg
--end mousemove
local varA
local posX
local posY

on arrowKey sRudder
   if sRudder is "left" then
      put (varA -1) into varA
   end if
   if sRudder is "right" then
      put (varA +1) into varA
   end if
   send animationPlease to me in 1
end arrowKey

on animationPlease
   put item 1 of the loc of btn "sub" into posX
   put item 2 of the loc of btn "sub" into posY
   put (posX + varA) into posX
   put (posY +varA) into posY --temp suspend
   if (posX <30) then
      put 30 into posX
      put 10 into varA
   end if
   if (posX >= 300) then
      put 300 into posA
      put -1 into varA
   end if
end animationPlease
BUTTON SCRIPT

Code: Select all

on  mouseUp
   animationPlease
   put the loc of btn "mine1" into startLoc
   repeat until intersect (btn "mine1" , btn "start") = true
      get the loc of btn "mine1"
      set the loc of btn "mine1" to (item 1 of it) & "," & (item 2 of it + 1)
      wait 1.5 --without messages locks the computer/programme
   end repeat
   set the loc of btn "mine1" to startLoc  --btn stops upon collision if this line disabled
end mouseUp
I keep trying?..

Craig just saw your comment, yes I know, I knew that it should have been in the card and not a button, have never done that before - I completely and totally missed that because I was so engrossed in practising something.
Thankyou

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Arrowkey

Post by dunbarx » Sat Dec 07, 2013 6:05 pm

Chris.

THAT'S why you missed it??? Too engrossed????

Good. You are hooked.

Is the thing working now?

Craig

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Arrowkey

Post by chris25 » Sat Dec 07, 2013 6:08 pm

NOooo I have to trap the arrowkey message - know what trapping is but can not find tuition about this when searching. trapping is where a message gets fished out of the script and eaten by a handler, question logically is then: what handler? How have no idea yet.
Edit: added pass arrowkey. According to dictionary this should work, that is what the dictionary says......

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10332
Joined: Wed May 06, 2009 2:28 pm

Re: Arrowkey

Post by dunbarx » Sat Dec 07, 2013 8:15 pm

Chris.

Have you stepped through your handlers? Set a breakpoint at the top of the "mouseUp" handler. Make sure you "step into" each line of code. Where does this lead? How might the order of stuff be changed to do what you intended?

This sort of thing happens all the time. When you are more experienced, you will see instantly that things are out of place, as opposed to feeling that you are stuck.

When the message order comes, and the hierarchy is your friend instead of a mystery, you will be over the hump.

Oh yes, and when you can do in ten lines what you now do in 40.

Craig

chris25
Posts: 354
Joined: Tue Oct 08, 2013 9:32 pm

Re: Arrowkey

Post by chris25 » Sat Dec 07, 2013 11:09 pm

Firstly. I have been through every inch of this code with breakpoints. (i found a tiny mistake but this had no influence at all on correcting). The variables were filled and recognised. I saw no problems with values at all. The debug also showed me that the animationPlease custom command was recognised by the arrowKey message script (I placed this at the beginning instead of at the end). I stared at the heirarchy list which I have in front of me ALWAYs - the exhaustive one with substacks and 'start using stack 2 etc" and have read quite a lot about it. But all this does not seem to help me understand.....something obviously.

Second. I wrote this script sometime ago (this one you see below) , it is on a card with two buttons and works fine. I know it is a different script, so I am imagining that it is the syntax of my present script which is causing problems. That is a guess, since I have tried to understand what is wrong, and can see nothing concrete. Obviously there is a mountain of mistakes, I can only do so much to understand and get it right, we make mistakes, we are told what is wrong, we learn from that. Thumbling around for clues and answers all evening does not help me to learn because I can only study and read, practise and get it wrong, then I need to be shown what is wrong. Any failures to get it right after a lot of attempts is not a sign of my lack of diligence and effort, but a sign of lack of good solid teaching material. I have been through terry's stack, it is excellent, and I am going through it again even as I write this, I have just downloaded his update and look forward to cementing and more drilling. I have gone through Jacques hierarchal stack, it is very good. But I can not fix this problem, I only know what I have read, but the learning experience needs 10 times more than the written material, it needs teaching and demonstrations and examples of pitfalls and examples about what NOT to do, and demonstrations about mistakes beginners might make, it needs visuals and diagrams and creative explanations (assuming that LC wants to go world wide as Richard gaskin mentioned in another post). Teaching material that just teaches the rules of the grammar does not help one to speak the language (I should know I have taught one language in college and instructed privately in another) I understand teaching, so LC tutorials teach you grammar, fine, and that is all I have, I have done the grammar, I do not understand how to speak this correctly, but understanding the grammar will help me to really appreciate the answer, and put me out of my sheer misery right now.

Should add this that is just a snippet of course you can guess the rest I know -

Code: Select all

on arrowKey Y
   if Y is up then
      set top of the button "box" to top of the button "box" -10
      -- a sort of bouncy effect from the top, unless set to
      -- a value of 0 instead of the 20
      if top of button "box" < 0 then
         set the top of the button "box" to 20
Kindest regards
chris :)

Post Reply