Page 1 of 2
How do i Run 2 or more events at the same time?
Posted: Wed Mar 02, 2016 1:41 am
by Jarf01
Hello Livecode Community. Hope you are having a nice day. i have a doubt and it is about running 2 events at same time. I am trying to create a game like Touhou, it is a shooter, and it consist in : A Space Ship that moves around on the card with the keybord keys W , A , S And D And Space for Shooting bullets. I got the two las things in my code an all of that goes well But when i Press Space And makes The shooting animation The SpaceShip Stop moving Around the card until i pressThe keys that do that. So i want to keep moving The spaceShip And ShootA bullet at Thesame Time. Code Example :
Code: Select all
Command fire
local Shippos
put the loc of img "Na" into Shippos
set the loc of img "Shoot" to Shippos
Show Img "Shoot"
Move Img "Shoot" relative 0 , -400 in 500 milliseconds
Hide Img "Shoot"
end fire
#=========================================
Hide Image "Shoot"
#==========================================
On keyDown Pletter
If Pletter = "W" Then
Move Img "NA" relative 0 , -5
if The top of img "NA" < 0 Then
Set the top of img "Na" To 0
End if
End if
#=========================================
If Pletter = "S" Then
Move Img "NA" relative 0 , 5
if The Bottom of img "NA" > 400 Then
Set the Bottom of img "Na" To 400
End if
End if
#=========================================
If Pletter = "A" Then
Move Img "NA" relative -5 , 0
if The Left of img "NA" < 0 Then
Set the Left of img "Na" To 0
End if
End if
#========================================
If Pletter = "D" Then
Move Img "NA" relative 5 , 0
if The Right of img "NA" > 400 Then
Set the Right of img "Na" To 400
End if
End if
#========================================
If Pletter is Space Then
Fire
End if
end keyDown
Thanks For the help

Re: How do i Run 2 or more events at the same time?
Posted: Wed Mar 02, 2016 7:13 pm
by jacque
Try adding "without waiting" to all the "move" commands. See the "move" command in the dictionary for an explanation.
Re: How do i Run 2 or more events at the same time?
Posted: Wed Mar 02, 2016 9:40 pm
by Jarf01
Ok , i tried that. But The results were not the expected. The code Was not moving the SpaceShip , and Can not Shot . So the code was not working :/. Anyways i'll Try it again
Re: How do i Run 2 or more events at the same time?
Posted: Wed Mar 02, 2016 10:08 pm
by Newbie4
Move commands are slow, use the "set loc" and it will go faster
Re: How do i Run 2 or more events at the same time?
Posted: Wed Mar 02, 2016 10:34 pm
by Jarf01
Can You make an example? Please. In my opinion you can set the time of move commands so actualle they can or can't Be slow if you want, In my opinion.
Re: How do i Run 2 or more events at the same time?
Posted: Thu Mar 03, 2016 3:35 am
by Newbie4
Set up a simple game loop to move the dart.
Code: Select all
on moveDart
set the left of img "shoot" to the left of img "shoot" -3
send "moveDart" to me in 20 millisecs
end moveDart
This allows other events to occur on the screen in between "moveDart" messages.
This will move the image "shoot" to the left. In the loop, you can check that the dart does not go off the screen, check for intersects, etc. You can change the speed by changing the number of pixels(3) and the frequency (20).
Does this make sense?
Re: How do i Run 2 or more events at the same time?
Posted: Thu Mar 03, 2016 4:21 am
by Jarf01
Yeah it makes sense and actually worked good, but got another bug, it will be simple to solve. but im new at livecode. i just know the basic things, dont even know how tuuse sen Etc. Well when i do the loop The loop does not finsih.
Let me explain better. When i press Space, for do the loop and do the things The loop finishes and start over and over , yes i can move the spaceship But i just only want the shoot trigers on the keydown of space, (when Space is pressed) and if it is released then the loop end. So how do i do that ; With something like an exit repeat or somewhat similar?
Thanks for your help Btw!
Re: How do i Run 2 or more events at the same time?
Posted: Thu Mar 03, 2016 4:33 am
by Newbie4
use a variable as a flag - set it to "true" or "false" and check it when/where you need to.
For instance, one way to do this is to set it to true on keyDown and false on keyUp. Then in the loop, before you send the message, check that variable and only send the message if it is true.
Re: How do i Run 2 or more events at the same time?
Posted: Thu Mar 03, 2016 4:36 am
by Jarf01
Ok so to set True on KeyDown and KeyUp its something like this
Code: Select all
On KeyDown Pletter
IF pletter is Space then
Put true into Variable
End if
OR
Code: Select all
On Keydown Pletter
If pletter is space then
Put True into variable
Else
Put false into variable
End if
When i do like the second way on the loop it was like from were i started did not work :/ . Or its like "Set the value of variable to true/false"
Re: How do i Run 2 or more events at the same time?
Posted: Thu Mar 03, 2016 5:21 am
by Newbie4
I am not sure how you want your dart to move.
1. If you want it to move only while you are holding down the space key, then you are on the right track. The first example of your code is the one to use, except give your variable a name. Next add code on the keyUp message - if it is a space, set that variable to false
and check it before you send another "moveDart" message
e. g.
Code: Select all
on moveDart
set the left of img "shoot" to the left of img "shoot" -3
if (your variable) is true then
send "moveDart" to me in 20 millisecs
end if
end moveDart
2. if you want it to keep moving on it's own until it goes off the card (or hits the spaceship) then you only send the message "moveDart" as long as it is on the card.
Either way, you need a flag (a variable) that is set to true or false. You need to check it before sending another "moveDart" message. You have to decide where to set it true and where to set it false.
Does that make sense to you?
Re: How do i Run 2 or more events at the same time?
Posted: Fri Mar 04, 2016 1:45 am
by Jarf01
It is working perfectly, Just got a Bug so i need to work in that. The message finish and does the loop well, and that is going very well

. The problem: Is triggered Well But the cycle does not end even with the flag variable that is fireable. so im going to put the code in here To see what is wrong and Solve the problem .
Code: Select all
Local FTTOF
local fireable
On Fire
Set the loc of img "Shoot" to the loc of img "NA"
Show img "Shoot"
Put True into fireable
if Fireable is true Then
Move img "Shoot" Relative 0 , -400 in 500 milliseconds
Send "Fire" to me in 20 milliseconds
Put false into Fireable
Exit fire
End if
end Fire
#=========================================
Hide Image "Shoot"
#==========================================
On keyDown Pletter
If Pletter = "W" Then
Move Img "NA" relative 0 , -5
if The top of img "NA" < 0 Then
Set the top of img "Na" To 0
End if
End if
#=========================================
If Pletter = "S" Then
Move Img "NA" relative 0 , 5
if The Bottom of img "NA" > 400 Then
Set the Bottom of img "Na" To 400
End if
End if
#=========================================
If Pletter = "A" Then
Move Img "NA" relative -5 , 0
if The Left of img "NA" < 0 Then
Set the Left of img "Na" To 0
End if
End if
#========================================
If Pletter = "D" Then
Move Img "NA" relative 5 , 0
if The Right of img "NA" > 400 Then
Set the Right of img "Na" To 400
End if
End if
#========================================
If Pletter is Space Then
#FTTOF means Fire to true or false
put True into FTTOF
If FTTOF is True then
Fire
End if
Put False Into FTTOF
End if
end keyDown
i dont localizate my error, I think or its in the fire or it si inkeydown space.thanks for helping me out and having patience

Re: How do i Run 2 or more events at the same time?
Posted: Fri Mar 04, 2016 2:39 am
by Newbie4
This would be good practice for you to figure out the problem. It is not that hard to see
Spend some time tracing through your code as if you were the computer. Work through it line by line. See if you can find the the flaw in the logic yourself.
Re: How do i Run 2 or more events at the same time?
Posted: Fri Mar 04, 2016 2:55 am
by Jarf01
okay Then! Ill try to do it

Re: How do i Run 2 or more events at the same time?
Posted: Fri Mar 04, 2016 7:24 pm
by Jarf01
I do not see the logic problem there D: , please , at least a hint
Re: How do i Run 2 or more events at the same time?
Posted: Fri Mar 04, 2016 8:19 pm
by Simon
Hi Jarf01,
Do you see how "fireable" will always be true?
Simon