Generating objects until a condition is met
Posted: 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.
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.
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