How do i Run 2 or more events at the same time?

Creating Games? Developing something for fun?

Moderators: Klaus, FourthWorld, heatherlaine, kevinmiller, robinmiller

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

How do i Run 2 or more events at the same time?

Post by Jarf01 » Wed Mar 02, 2016 1:41 am

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 :|

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 4156
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: How do i Run 2 or more events at the same time?

Post by jacque » Wed Mar 02, 2016 7:13 pm

Try adding "without waiting" to all the "move" commands. See the "move" command in the dictionary for an explanation.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Wed Mar 02, 2016 9:40 pm

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

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: How do i Run 2 or more events at the same time?

Post by Newbie4 » Wed Mar 02, 2016 10:08 pm

Move commands are slow, use the "set loc" and it will go faster
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Wed Mar 02, 2016 10:34 pm

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.

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: How do i Run 2 or more events at the same time?

Post by Newbie4 » Thu Mar 03, 2016 3:35 am

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?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Thu Mar 03, 2016 4:21 am

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!

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: How do i Run 2 or more events at the same time?

Post by Newbie4 » Thu Mar 03, 2016 4:33 am

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.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Thu Mar 03, 2016 4:36 am

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"

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: How do i Run 2 or more events at the same time?

Post by Newbie4 » Thu Mar 03, 2016 5:21 am

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?
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Fri Mar 04, 2016 1:45 am

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 :)

Newbie4
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 261
Joined: Sun Apr 15, 2012 1:17 am
Location: USA
Contact:

Re: How do i Run 2 or more events at the same time?

Post by Newbie4 » Fri Mar 04, 2016 2:39 am

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.
Cyril Pruszko
https://sites.google.com/a/pgcps.org/livecode/
https://sites.google.com/a/setonhs.org/app-and-game-workshop/home
https://learntolivecode.com/

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Fri Mar 04, 2016 2:55 am

okay Then! Ill try to do it :)

Jarf01
Posts: 12
Joined: Wed Mar 02, 2016 1:23 am

Re: How do i Run 2 or more events at the same time?

Post by Jarf01 » Fri Mar 04, 2016 7:24 pm

I do not see the logic problem there D: , please , at least a hint

Simon
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3900
Joined: Sat Mar 24, 2007 2:54 am
Location: Palo Alto

Re: How do i Run 2 or more events at the same time?

Post by Simon » Fri Mar 04, 2016 8:19 pm

Hi Jarf01,
Do you see how "fireable" will always be true?

Simon
I used to be a newbie but then I learned how to spell teh correctly and now I'm a noob!

Post Reply

Return to “Creating Games”