Page 1 of 1

Loops problem with send

Posted: Mon Jun 23, 2014 9:49 am
by MaxV
Hello,
I wanted to create a Frogger game, user move a graphic (frog) avoiding cars.
I wanted to keep simple the code, so I wrote the code just for one car (a graphic), then I copied and pasted six cars.
This is the car code, when the game starts every cars receive the messages gioco (a loop to move car) and controint (a loop to check intersection with frog):
#####
on gioco
move me relative 400,0 in 3 sec
move me relative -400,0 in 1 tick
if the giostat of this card is "runni" then
send gioco to me in 0.1 sec
end if
end gioco

on controint
if intersect(graphic frog, me) then
set the giostat of this card to "stop"
stop moving me
answer "Aaaaahh! The frog died!"
end if
if the giostat of this card is "runni" then
send controint to me in 0.1 sec
end if
end controint
#####

My problem is that with more than one car, loops random freeze and restart. :shock:
I know that I could create a single message that move all cars and check intersection, but with my way I can change game configuration just with dragging, coping, pasting.
I suspect that problem is in send message, do you know a better way to move many graphic with every graphic containing is own move message?

Re: Loops problem with send

Posted: Mon Jun 23, 2014 3:37 pm
by dunbarx
Hi.

You might check out the "lockMoves" command. This will allow you to queue the moves for several cars and release them all at once when you "unlock moves"

Craig Newman

Re: Loops problem with send

Posted: Mon Jun 23, 2014 8:37 pm
by Simon
Hi Maxv, Craig,
Is this where one uses a "private command"?
private command gioco
move me relative 400,0 in 3 sec
move me relative -400,0 in 1 tick
etc.

Simon

Re: Loops problem with send

Posted: Tue Jun 24, 2014 9:59 am
by MaxV
This is the script: https://www.dropbox.com/s/fadmmsnyvsx35 ... r.livecode

Push start and see how every pink car should loop without stopping, but they random freeze and restart. You can move the frog with arrow keys. Something wrong happen in pendingmessages or in timing. The correct game has always 6 cars on the screen.
My goal was to have all independent cars, each car with its own code, its own speed and so on, without recording how many cars are and their names.
At the present I suppose that I have to record all car names and create a clock function that each second (or less) give a move input to all cars.

Re: Loops problem with send

Posted: Tue Jun 24, 2014 11:12 pm
by bn
Hi Max,

I changed the code a bit and it seems to work.
I changed the car movement to make use of the "moveStopped" message.

This would probably a use case for a behavior script: all cars have basically the same script and only little variation in speed and direction of movement.

Unfortunately I killed a lot of frogs trying to steer them across the street. :)

I changed the fileformat to 5.5, hence the name.

Kind regards

Bernd

Re: Loops problem with send

Posted: Wed Jun 25, 2014 9:37 am
by bn
Hi Max,

I have put the scripts of the cars into one behavior button. That way it is easier to change code. You do it in one place and not in 6 places.
It is a lot easier to fine-tune speed and so on.

Also I have changed how far the cars move, I added the width of the car to the movement. This way they move into the card and don't pop up at the edges.

It is a fun little game.

Kind regards

Bernd

Re: Loops problem with send

Posted: Wed Jun 25, 2014 2:20 pm
by MaxV
Thank you Bernd! :D

I have just one question on the first script, why do you add the exit on gioco?

Re: Loops problem with send

Posted: Wed Jun 25, 2014 2:27 pm
by bn
Hi Max,
why do you add the exit on gioco?
it was a leftover from my attempts to modify your script. You were stacking moves in your original scripts and I wanted to get out of that. It is not necessary in the first script and I took that line out in the second version. In case it is still in the second script just delete those "exit gioco".

Kind regards
Bernd