Page 1 of 1

Submarine Movements forward and backwards only

Posted: Wed Nov 20, 2013 6:44 pm
by chris25
The Good News:
1 Taking finger off the right arrow key slows down the sub, the sub keeps moving as I want it to despite removing finger from arrow key, not bringing it to an unrealistic abrupt stop.
2 Also pressing the opposite key to change direction takes some time for the sub to change direction as it slows down (trying to mimic reality) this works nicely

The Bad news:
1 When the sub actually stops to change direction it stops Tooo tooo long before changing direction - about 2 seconds actually it is about 3,3 seconds
2 I still can not discover how to limit the maximum speed despite Mark's kind offer of the min and max function, (which I have studied and read about But Yet still can not find any info really on how to use it within this contextual setting) maybe I am barking up the wrong tree with this and should be looking at something else?

in Button Start

Code: Select all

global subStartX
global subStartY
global subX
global subY
global subSpeedX

on mouseUp
   put initializesub() into Propel
   send RunSilent to me in 3000 milliseconds
end mouseUp
In main card:

Code: Select all

global gsubStartX
global gsubStartY
global gsubX
global gsubY
global gsubSpeedX

on arrowKey theRudder
   if theRudder is "left" then
      put (gsubSpeedX - 0.05) into gsubSpeedX
   end if
   if theRudder is "right" then
      put (gsubSpeedX + 0.05) into gsubSpeedX
   end if
   --not working needs work - put max(gsubSpeedX - 0.1) into gsubSpeedX
   --and put min(gsubSpeedX + 0.1) into gsubSpeedX
   send RunSilent to me in 3000 milliseconds
   --trying to delay the command so that sub slows down gradually
end arrowKey


on RunSilent
   --The Loc has only two co-ordinates item 1 and 2
   --send these messages to object
   put item 1 of the location of button "sub" into gsubX
   put item 2 of the location of button "sub" into gsubY
   put (gsubX + gsubSpeedX) into gsubX
   --10 causes sub to bounce back from edge.  0 means sub just stops.
   if (gsubX <=35) then
      put 35 into gsubX
      put 0 into gsubSpeedX
   end if
   --100 here causes the sub still to stop, as does 25 but it does not bounce back
   --numbers from put command are screen limits
   if (gsubX >=770) then
      put 770 into gsubX
      put 0 into sgubSpeedX
   end if
   set the location of button "sub" to gsubX, gsubY
end RunSilent

function initializesub
   --screen positioning
   put 60 into gsubStartX
   put 200 into gsubStartY
   put gsubStartX into gsubX
   put gsubStartY into gsubY
   set the location of button "sub" to gsubX, gsubY
   --put 0 into gsubSpeedX - redundant code
   --this sets the stationary action of sub on start and also when arrow keys are pressed
end initializesub
Thankyou.

Re: Submarine Movements forward and backwards only

Posted: Fri Nov 22, 2013 12:32 pm
by Mark
Hi Chris,

You need a different strategy. Instead of responding to user input 34 seconds later with

send RunSilent to me in 3000 milliseconds

you need to run an updateScreen handler every 200 milliseconds and make it act dependent on a number of locally or globally defined variables. I don't have much time now so I'll give a very simple example:

Code: Select all

local lDirection
on arrowKey theKey
  put theKey into lDirection
end arrowKey

on updateScreen
  if lDirection is left then put -4 into mySpeedX
  else put 4 into mySpeedX
  put the loc of btn 1 into myLoc
  put min(the width of this cd,max(0,item 1 of myLoc + mySpeedX)) into item 1 of myLoc
  set the loc of btn 1 to myLoc
  send "updateScreen" to me in 200 millisecs
end updateScreen

on startUpdateScreen
  updateScreen
end startUpdateScreen

on stopUpdateScreen
  put the pendingMessages into myMessages
  filter myMessages with "*updateScreen*"
  repeat for each line myMsg in myMessages
    cancel item 1 of myMsg
  end repeat
end stopUpdateScreen
This script always responds after 200 millisecs, which is responsive enough for a pleasant user experience. You can test it with a new stack with one button and this script in the card. Call startUpdateScreen or stopUpdateScreen from the message box.

Kind regards,

Mark

Re: Submarine Movements forward and backwards only

Posted: Fri Nov 22, 2013 3:12 pm
by chris25
Mark your code has done more than just solve a situation, actually I was working on something else totally unrelated and when I read your code I suddenly understood a whole load of previous mysteries unrelated to this. So thankyou.
Kind regards
chris

Re: Submarine Movements forward and backwards only

Posted: Fri Nov 22, 2013 6:31 pm
by Mark
:lol: Happy to help, Chris!

Mark