Two different scripts to achieve same outcome problem

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

Two different scripts to achieve same outcome problem

Post by chris25 » Fri Dec 13, 2013 3:24 pm

Both scripts at card level. Each in its own stack. This first one works fine as is, but I still after so long a time can not understand how to correct any mistakes in this second script. Debugging has not helped in this instance because I am unable to understand all the nuances of what the debug is telling. In this case a lack of information is saying something I am sure but have no idea what to look for. Obviously I have checked the maths of what the variables should hold and what they should do, but can not see anything wrong here. I am lost after a whole week. Kind regards, Chris.

Code: Select all

on arrowkey pKey
   if pKey = "left" then
      set the left of button "buttonsubbb" to the left of button "buttonsubbb" -1
   end if
   if pKey = "right" then
      set the right of button "buttonsubbb" to the right of button "buttonsubbb" +1
   end if
   if pKey = "up" then
      move button "buttonsubbb" relative +1,-1
   end if
   if pKey = "down" then
      move button "buttonsubbb" relative +1,+1
   end if
end arrowkey

Code: Select all

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 0
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
   if (posX <30) then
      put 30 into posX
      --put 10 into varA
   end if
   if (posX >= 450) then
      put 450 into posX
      --put -10 into varA
   end if
   set the loc of btn "sub" to posX,posY
end animationPlease

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

Re: Two different scripts to achieve same outcome problem

Post by dunbarx » Fri Dec 13, 2013 4:54 pm

Chris,

Is your problem that when you change directions, the (possibly) large built-up offsets in VarA are still in play from the last direction chosen? If so, you should be able to fix this.

Craig

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

Re: Two different scripts to achieve same outcome problem

Post by chris25 » Fri Dec 13, 2013 7:13 pm

The problem is that the "sub" button moves left upwards diagonal as if it were scripted with the "relative" co-ordinates. Not only that all 4 arrowkeys move it in exactly the same direction.
I have uploaded screeshot of simple stack layout.
regards
chris
Attachments
Screen shot 2013-12-13 at 18.10.09.png
Screen shot 2013-12-13 at 18.10.09.png (11.79 KiB) Viewed 4373 times

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

Re: Two different scripts to achieve same outcome problem

Post by dunbarx » Fri Dec 13, 2013 7:37 pm

Chris.

You do not explicitly manage all four arrowkeys, so the "up" and "down" keys simply pass through, but they still use the current value of varA. That is why they seem to continue in the same direction. But they do not change the varA value.

As for the left and right keys, the right one moves the sub to the lower right, and the left one to the upper left, just as it is told to. Since varA is added (sometimes negatively) you will always get diagonal motion. I see you are increasing the offset with each press of the same arrowkey. Nice. Is this what you intended?

Why are you having trouble when you step through the handler? By looking at the value of varA you should get a sense of what to do to make this work the way you want.

Craig

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

Re: Two different scripts to achieve same outcome problem

Post by chris25 » Fri Dec 13, 2013 9:09 pm

Hallo Craig, No this direction was not intended. It was only the horizontal left right move that was intended. This is what I do not understand. However I had presumed that not coding for the up and down arrow would have meant that they would not work. Guess I do not understand this message properly. However, could you elaborate upon this please: Since varA is added (sometimes negatively) you will always get diagonal motion. Also I can not understand why VarA starts always with a value of 6? Where does it get this number from. I imagined that VarA value would be either starting with a zero or 1? why 6?
chris

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

Re: Two different scripts to achieve same outcome problem

Post by dunbarx » Fri Dec 13, 2013 10:08 pm

Chris.

Who says it starts with 6? How do you initialize varA? However you do, it will start with what you tell it to. The way you have it now, it simply remembers the last time an arrowKey message was sent. This is the result of using script local variables, correct?

If you are modifying the loc of a control by adding a value to both the X and Y components, then the offset will the same distance in both those axes, and that will give a diagonal motion. Go from "100,100" to "120,120". Which way is that?

If you want to move left when the left arrow key is pressed, then you only want to change the X component, no? Right now you are changing both X and Y.

Keep plugging. This is perfect stuff to agonize over...

Craig

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

Re: Two different scripts to achieve same outcome problem

Post by chris25 » Fri Dec 13, 2013 10:46 pm

You know, your answers are so obvious. I think I must be burned out or something. The 6 comes from the debugging, then I realized afterwards that if you hover the mouse pointer over each varibale it gives its value. So that 6 came from the debugging as I stepped through each individual line. Unfortunately when you press stop, the variables are not emptied as I presumed and I re-start the movement but the previous value is still there (or whatever I have no idea).

Ok Craig, one last question, what is the CONCEPT that is widely learned by programmers of LC whereby two or more things must be going on without interference from each other. This has frustrated me so much over the last 10 days, so much reading and looking at scripts, but still when I try it myself I lose it.

Thankyou
chris

Edit: I just realized, I went wrong because I thought that the left and right key were automatically telling the LC which direction to go. But now I see that they are not, the names of the arrow keys actually do not matter. It's the syntax and variables and maths that determine all this, I thought that the way I had written X and Y simply should keep it horizontal at least. So now I really simply do not know how to write this as you suggest, with only the X co-ordinate. I just tried and it went haywire, Can you please just tell me, I need to see it to understand it this time. Brain wrangling is getting me no where.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”