Generating objects until a condition is met

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

Post Reply
TheApexEffect
Posts: 6
Joined: Mon Nov 18, 2013 8:34 pm

Generating objects until a condition is met

Post by TheApexEffect » Mon Nov 25, 2013 2:31 pm

I am currently working on a game with random enemy generation. At this moment in time I have one of three enemies spawning at one end of the screen, travelling across to the other end of the screen and then respawning in the original position. I would like for it to repeat when a variable is equal to zero. currently at this moment in time what i have is not working.

Code: Select all

Global lives, appearance, position
on openCard
   arrowkey
   startRandomgenerate
   initialise
end openCard

on arrowkey x
   if x is "up" then
      move image "Image" relative 0,-10
   end if
   
   if x is "down" then
      move image "Image" relative 0,10
   end if
end arrowkey

on initialise
   put 3 into lives
   answer "it works"
end initialise

on startRandomgenerate
   Randomgenerate
end startRandomgenerate

on Randomgenerate 
   put random(3) into appearance
   put random (3) into position
   if appearance = 1 then
      set the filename of image "Enemy" to "cartoon_shark.gif"
   end if
   
   if appearance = 2 then
      set the filename of image "Enemy" to "Coral.jpg"
   end if
   
   if appearance = 3 then
      set the filename of image "Enemy" to "Cartoon-Scuba-Diver-Hug.jpg"
   end if
   
     if position = 1 then
      set the loc of image "Enemy" to 400,random(500)
   end if
   
   if position = 2 then
      set the loc of image "Enemy" to 400, random(650)
   end if
   
   if position = 3 then
      set the loc of image "Enemy" to 400, random(500)
   end if
   
     if the right of image("Enemy") < the right of this card then
      set the right of image("Enemy") to the right of this card
   end if
    if the left of image("Enemy") < the left of this card then
      set the left of image("Enemy") to the left of this card
   end if
   
   movement
   
   if the right of the image "Enemy" < the left of this card then
      add -1 to lives
   end if 
   
   repeatmovement
   
end randomgenerate

on repeatmovement
   if lives > 0 then
         if "randomgenerate" is not in the pendingmessages then
               send "randomgenerate" to me in 1 seconds
         end if
   end if
end repeatmovement

on stopRandomgenerate
   doStopPending randomgenerate
end stopRandomgenerate

on doStopPending pMessage
   repeat for each line aLine in the pendingmessages
      if pMessage is in item 1 of tLine then
         cancel item 1 of aLine
      end if
   end repeat
end doStopPending

on movement
   put item 2 of loc of image "Enemy" into tY
   move image "Enemy" to -50,tY in 5 seconds
end movement
The initialise and randomgenerate functions are called by pressing a button which sends the program off on a infinite loop. Any help would be greatly appreciated, thanks in advance.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: Generating objects until a condition is met

Post by Mark » Tue Nov 26, 2013 12:54 am

Hi,

I don't understand what you're asking. Can you rephrase your question in terms of fields, buttons and images instead of enemies? What exactly do you want to repeat and what is the condition and what do you want to happen when the condition is met?

The line

Code: Select all

 if the right of image("Enemy") < the right of this card then
should be

Code: Select all

 if the right of image("Enemy") > the right of this card then
Kind regards,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

TheApexEffect
Posts: 6
Joined: Mon Nov 18, 2013 8:34 pm

Re: Generating objects until a condition is met

Post by TheApexEffect » Tue Nov 26, 2013 11:20 am

Sorry for the uncertainty caused in my description, I've actually found a solution to the problem but thanks for the reply

Post Reply