Page 1 of 1
little hiccup with a movement
Posted: Thu Nov 21, 2013 10:00 pm
by chris25
I have 4 mines each in their own group. I can not figure out why, that when I press the Start button, only Mine number 4 moves into its correct position, the rest stay still.
Code: Select all
function initializeMines
repeat with i = 1 to 4
put "mines" & i into gMinesBegin
end repeat
repeat with i = 1 to 4
put (0.1 + random(10/10)) into dropSpeed
end repeat
repeat with i = 1 to 4
put 150 + (i-1)*60 into gMinesStartX
put gMinesStartX into gMinesX
put 50 into gMinesStartY
put gMinesStartY into gMinesY
set the location of group gMinesBegin to gMinesX, gMinesY
end repeat
--set the loc of grc "mine" to 304,15
--move grc "mine" from 304,15 to 304,340 in 15 seconds
end initializeMines
regards
chris
Re: little hiccup with a movement
Posted: Thu Nov 21, 2013 10:20 pm
by Klaus
Hi Chris,
you are using 2 repeat loops too much!
Code: Select all
...
repeat with i = 1 to 4
put "mines" & i into gMinesBegin
end repeat
...
This will overwrite the variable gMinesBegin FOUR times and in the last repeat its content is -> mines4!!!
Taht's why only that groups moves.
Code: Select all
...
repeat with i = 1 to 4
put (0.1 + random(10/10)) into dropSpeed
end repeat
...
Same here, but you do not use dropSpeed in your handler at all!?
This should do the trick:
Code: Select all
command initializeMines
repeat with i = 1 to 4
## HERE is the correct place:
put "mines" & i into gMinesBegin
# ??
# put 150 + (i-1)*60 into gMinesStartX
# put gMinesStartX into gMinesX
# put 50 into gMinesStartY
# put gMinesStartY into gMinesY
# ???
put 150 + (i-1)*60 into gMinesX
put 50 into gMinesY
set the loc of grp gMinesBegin to gMinesX, gMinesY
end repeat
end initializeMines
AND:
Why make this a FUNCTION?
This does not return anything, but works of course...
Best
Klaus
Re: little hiccup with a movement
Posted: Thu Nov 21, 2013 10:34 pm
by chris25
Hi Klaus, I need time to digest and comeback, since this is all new to me I am practising and trying to understand what I thought I understood without fully understanding what I thought that originally I did understand...
Ok Klaus this is the confusion; I put the number of mines (4 of them) into the variable. The next time I use this variable (above) is when I declare its position, ie, the position of the 4 mines. What I still cannot see, (even though your script is of course working) is how I have repeated the loop so many times. I have started and stared and can not see the logic of so many repeats. All I see is mines numbered (mines1,mines2,etc etc) 4 of them, placed in a variable (gMinesBegin) and only once declared at the end. I am looking at the repeat loop, but I see no repetitions, so I know that I am misunderstanding some essential meaning of some syntax here, this is the only explanation.
(I am so glad I chose only 4, I actually want 30, but decided to begin with just 4 to see if I went wrong)
Regards
chris
Re: little hiccup with a movement
Posted: Fri Nov 22, 2013 1:07 pm
by Klaus
Chris,
Code: Select all
...
repeat with i = 1 to 4
put "mines" & i into gMinesBegin
end repeat
...
take a look at this repeat loop.
"GO" through all of its 4 repeats and note the current value of i and gMinesBegin.
loop 1 -> i = 1 -> gMinesBegin = mines1
loop 2 etc...
Then see what is in gMinesBegin AFTER this repeat loop!
Is this what you wanted? Is that what you exspected? Don't think so.
But you MUST understand what is going on here!
The problem:
You are filling the SAME variable four times (repeat with i = 1 to 4) with
different values. And this way only the value from the LAST repeat (i = 4)
will be in the variable AFTER the repeat loop!
I need time to digest and comeback
Well, the frequency of your postings here tell me that you do not follow your own advice
Best
Klaus
Re: little hiccup with a movement
Posted: Fri Nov 22, 2013 3:06 pm
by chris25
Klaus thank you for taking the time to lay it open for me. I read every posting then I put it into practise and then play around.
Klaus said: ""Well, the frequency of your postings here tell me that you do not follow your own advice""
Firstly there is no text book for livecode that has well laid out structured lessons, examples, no livecode for dummies so to speak, the sort of thing you get with Photoshop, illustrator, pearl, java, ruby, python, adobe products, the list is endless. If there was one I would be buying it. It is difficult to understand without explanation about How or Why something works - So understanding for example exactly how the repeat loop works was only possible through seeing other code. And i often saw this kind of syntax placed within variables more than once:
repeat with i = 1 to 40
put "rocket" & i into rocketName
put 0 into rocketInMotion
set the visible of group rocketName to true
end repeat
repeat with i = 1 to 5
put 730 + (i-1)*15 into rocketStartX
put 80 into rocketStartY
put rocketStartX into rocketX
put rocketStartY into rocketY
end repeat
But I did not understand How it was working, Without further teaching I do not know what else to do other than to begin with what I find and dive in without knowing really, at first, what is actually taking place within those coded lines.
So I end up posting a lot of posts because I am absorbing several problems at once, working on at least three or four different kinds of scriptings at once, doing well on some and awful on others. So I am a little bemused as to what advice I gave myself that I do not follow
Kindest regards
chris 
Re: little hiccup with a movement
Posted: Fri Nov 22, 2013 3:36 pm
by Klaus
Whatever, do you understand what happens in your first "repeat" loop
and why it does not work for you in your script? This is most important!
Don't try to do/understand too many things at once, start small!
Means: take one little problem at a time and "get" it.
Then the next one, then combine them and you will
see this will work out fine.
The learning cures IS VERY steep in Livecode, because it is so powerful,
and you need to master the curve with "baby steps"
And work through the stacks from the scripting conference:
http://www.hyperactivesw.com/revscriptc ... ences.html
Great learning resource!
Re: little hiccup with a movement
Posted: Fri Nov 22, 2013 7:31 pm
by chris25
Yes Klaus, I see what is happening, on putting breaskpoints on both your script and mine, on mine, too many repeats means tat each action is being repeated as opposed to yours where only one repeat means the whole lot is being acted upon in one move. And I see the variable i being populated and understand it. However something new learned in all this - I always thought that the i within a repeat with... statement was a special character,( something that coders understood had special significance, this is what I assumed) I did not realize that it was an actual variable.
Thankyou for the link and the lesson Klaus. all in all much appreciated, much confusion has been cleared up.
Kind regards
chris
EDit: I should also add that this lesson has taught me to use the breakpoints more often in debugging, mostly I have not learned what some of the messages mean, but nevertheless I should start using this more often. So that has been now fully realised.
Re: little hiccup with a movement
Posted: Fri Nov 22, 2013 10:48 pm
by Klaus
The "i" in most repat loops is pure lazyness!
Correct would be -> VariableThatHoldsTheCurrentIteration
