repeat

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

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

repeat

Post by kelyanok » Wed Apr 01, 2020 3:17 pm

hello
so i basically have a game where you gain money (yes thats gross but you dont need to know more)
to gain money, you just need to wait. ive made an object that when you press it, you gain 1 coin every second, by doing:

Code: Select all

on mouseUp
      repeat x = 10     //this number is only to test
      wait 1 sec
      add 1 to aMoney
      put aMoney into field "moneyField"
   end repeat
end mouseUp
(this code is working, but stopping at 10)
but this needs to repeat indefinitely, and you need to do other things at the same time, but the problem is that if you run a repeat, you cant press buttons or do anything. is there another way to add money regularly without using repeat? thanks
kelyan

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: repeat

Post by bogs » Wed Apr 01, 2020 3:32 pm

Your (probably) looking for the 'repeat until' loop form, and 'With messages'.
Image

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

Re: repeat

Post by Klaus » Wed Apr 01, 2020 4:14 pm

Hi kelyanok,

important, the correct syntax in your example is:

Code: Select all

...
repeat with X = 1 to 10

## or just: repeat 10
...
I wonder why LC did not complain with your syntax!? :-)

Please provide a bit more info:
1. Does the user need to keep the mouse pressed (= down) to get the money every one second?
2. Or click once and then gain money every second?

If 1. then you should use SEND and not repeat!
Put this into the script of your "money giving" object:

Code: Select all

on mouseDown 
   ## If you want to give money immedaitely:
   add 1 to fld "moneyfield"
      
   send "give_me_your_money" to me in 1 secs
end mouseDown

command give_me_your_money
   
   ## Mouse is not DOWN anymore!
   if the mouse = "up" then
      exit to top
   end if
   
   ## Mouse is still DOWN.
   ## We can add something directly to a field, as long as the field contains a number!
   add 1 to fld "moneyfield"
   send "give_me_your_money" to me in 1 secs
end give_me_your_money
Tested and works! :-)


Best

Klaus

snowfun
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 16
Joined: Tue May 29, 2007 7:22 am
Location: Edinburgh

Re: repeat

Post by snowfun » Thu Apr 02, 2020 8:40 am

I, too, am struggling with managing a repeat loop and checking for additional input.

Specifically, I have a text field which I want to scroll (as in a teleprompter). The use clicks the "start scroll" button which simply scrolls the field. That bit I can cope with!

However, I want to be able to increase/decrease the scroll speed using the arrow buttons (the arrow buttons enable me to use a remote control using designed for Keynote/Powerpoint)

Is is feasible to check the status of the arrow keys from within a loop?

More generally, is there a good tutorial on how to "do things" from within a repeating loop?
Thanks,
Tim

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

Re: repeat

Post by kelyanok » Thu Apr 02, 2020 9:37 am

hello klaus!
its the option 2. the player presses a button, waits 1 sec and earns 1 coin, waits 1 sec, earns 1 coin ect... i still didnt found my answer, ill maybe modify my game or something. thanks!

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: repeat

Post by Thierry » Thu Apr 02, 2020 9:44 am

snowfun wrote:
However, I want to be able to increase/decrease the scroll speed using the arrow buttons (the arrow buttons enable me to use a remote control using designed for Keynote/Powerpoint)
More generally, is there a good tutorial on how to "do things" from within a repeating loop?
Hi Tim,

I have no idea if any tutorials on this matter,
but one way to learn is to read some code from LiveCode team.

From the IDE, select menu "About LiveCode" and you'll see a scrolling text
Then, look at the script and you'll know one way to do it.
The stack is named "revAbout.rev", the script is in field "Credits".

Hope this helps and good luck,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: repeat

Post by Klaus » Thu Apr 02, 2020 1:37 pm

Hi kelyanok,
kelyanok wrote:
Thu Apr 02, 2020 9:37 am
hello klaus!
its the option 2. the player presses a button, waits 1 sec and earns 1 coin, waits 1 sec, earns 1 coin ect... i still didnt found my answer, ill maybe modify my game or something. thanks!
oh, easy money! :-)
OK, in that case just omit the mouse = up part, but memorize the ID of the SEND message, so you can CANCEL it when the stack closes!

Code: Select all

global give_money_id

on mouseDown 
   ## If you want to give money immedaitely:
   add 1 to fld "moneyfield"     
   send "give_me_your_money" to me in 1 secs
end mouseDown

command give_me_your_money
 
   ## Mouse is still DOWN.
   ## We can add something directly to a field, as long as the field contains a number!
   add 1 to fld "moneyfield"
   send "give_me_your_money" to me in 1 secs
   put the result into give_money_id
end give_me_your_money
Then in the stack script:

Code: Select all

global give_money_id

on closestack
  cancel give_money_id
  ## you closestack stuff here if any
end closestack
Lookup PENDINGMESSAGES in the dictionary to see an explanation for this.

Best

Klaus

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

Re: repeat

Post by kelyanok » Thu Apr 02, 2020 2:00 pm

hello klaus!
you are a god! this works perfectly well! ill try to understand how this works otherwise thats cheating :D

thank you!!

oh and how do you stop it? :?

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

Re: repeat

Post by Klaus » Thu Apr 02, 2020 2:27 pm

Hi kelyanok.
kelyanok wrote:
Thu Apr 02, 2020 2:00 pm
hello klaus!
you are a god!
no, I'm a german! :-D
kelyanok wrote:
Thu Apr 02, 2020 2:00 pm
ill try to understand how this works otherwise thats cheating :D
Please ask if something is not clear!
kelyanok wrote:
Thu Apr 02, 2020 2:00 pm
oh and how do you stop it? :?
Why stop a money giving machine? :shock:

Hehe, you can use the CANCEL part any time whenever you want to stop the sending of the message like this in a mouseup:

Code: Select all

global give_money_id

on mouseup
  cancel give_money_id
end mouseup
Best

Klaus

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: repeat

Post by jmburnod » Thu Apr 02, 2020 3:12 pm

Hi Tim,
Is is feasible to check the status of the arrow keys from within a loop?
I tried this

Code: Select all

on mouseup
   put empty into fld "fDev2"
   repeat with i = 1 to 100
      if the optionkey is down then exit repeat
      put "***" & the ticks into tDev
      put tDev into fld "fDev1" 
      wait 10 milliseconds with messages
      put the keysdown into tKD
      if tKD <> empty then -- exit repeat works
         answer (tKD = empty) -- answer dialog doesn't appear
         put tKD into fld "fDev2" --  fld "fDev2" is empty 
         exit repeat
      else
         put "tKDEmpty" & the ticks  into fld "fDev2"
      end if
   end repeat
end mouseup
Curiously I can stop a loop with arrowkey (works with others) but the keysdown seems empty. If that is the case, why loop is stopped :shock:

Edit: wait 10 milliseconds instead 1000 milliseconds is better
Thanks for your lights
Best regards

Jean-Marc
Last edited by jmburnod on Thu Apr 02, 2020 4:17 pm, edited 1 time in total.
https://alternatic.ch

snowfun
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 16
Joined: Tue May 29, 2007 7:22 am
Location: Edinburgh

Re: repeat

Post by snowfun » Thu Apr 02, 2020 4:13 pm

Hi Jean-Marc,

I'll try that and variations thereof to see if I can figure something out!
Is your loop stopping simply because of the i=100?

Best wishes,
Tim

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: repeat

Post by jmburnod » Thu Apr 02, 2020 4:53 pm

Is your loop stopping simply because of the i=100?
Yes if nothing happends but you can stop the loop if the option key is down or if one keyboard key is down (except arrowkeys)
https://alternatic.ch

snowfun
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 16
Joined: Tue May 29, 2007 7:22 am
Location: Edinburgh

Re: repeat

Post by snowfun » Thu Apr 02, 2020 4:59 pm

The following slightly edited version of your script seems to "work". It isn't very smooth and I guess there are areas to improve but it does do the basics (ie changes the speed of scrolling using the up and down arrow keys). One issue is that I need to cancel the effect of long key presses which have a cumulative (and undesirable) effect.

on mouseup
put 1 into vIncrement
put vIncrement into fld "ftest"
put "" into fld "fkeydown"
repeat
if the optionkey is down then exit repeat
set the vscroll of fld "fscrolltext" to the vscroll of fld "fscrolltext" + vIncrement --this can possibly be improved
wait 10 milliseconds with messages --still testing the effect of the delay
put the keysdown into tKD
if tKD <> empty then put tKD into fld "fkeydown" --check the key pressed
if tKD = 65362 then
put vIncrement + 1 into vIncrement
put vIncrement into fld "ftest" --works
end if
if tKD = 65364 then
put vIncrement - 1 into vIncrement
put vIncrement into fld "ftest" --works
end if
end repeat
end mouseup

Thanks you for your assistance.
Tim

kelyanok
Posts: 22
Joined: Sun Feb 23, 2020 8:48 am
Location: Belgium

Re: repeat

Post by kelyanok » Thu Apr 02, 2020 6:25 pm

Hallo Klaus!

in my game, there is a level system. when you have 10 money, the btn levelUpFa is enabling. you click it, you loose 10 money, and you earn +1 money each second. i did it with my old code, but with yours i dont really know where to check my money ect..

can you help me?
thanks
kelyan

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

Re: repeat

Post by Klaus » Thu Apr 02, 2020 6:41 pm

Hi Tim,
snowfun wrote:
Thu Apr 02, 2020 4:59 pm
The following slightly ...
could you please create a new thread for your question next time and not hijack another one like this?
Thank you!


Best

Klaus

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”