Skip to content

Instantly share code, notes, and snippets.

@tariqkhatib
Created October 26, 2014 06:07
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save tariqkhatib/eae25c51daf75e9fa7fc to your computer and use it in GitHub Desktop.
Save tariqkhatib/eae25c51daf75e9fa7fc to your computer and use it in GitHub Desktop.
Simulate Space in less than 30 lines of Code
starCtr = 0
stars = {}
currTransSpeed = 500
displayHeight = display.contentHeight
math.randomseed( os.time() )
local function removeMe() --remove stars after reaching end of screen
for i=1,1100 do
if stars[i] ~= nil then
if stars[i].y >= displayHeight then
display.remove( stars[i] )
stars[i] = nil
end
end
end
if starCtr >= 1000 then
starCtr = 0
end
end
local function loadSpace() -- create starts and move them
starCtr = starCtr + 1
local currX = math.random( 1,display.contentWidth)
local currY = math.random( 1,display.contentHeight)
local currRadius = math.random( 1,2)
stars[starCtr] = display.newCircle( currX, currY, currRadius )
transition.to( stars[starCtr], {y=display.contentHeight + currRadius,time = currTransSpeed,onComplete = removeMe} )
end
local function startSpace()
timer.performWithDelay( 10, loadSpace , -1 )
-- timer.performWithDelay( 5000, loadPlanet , -1 )
end
startSpace()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment