Hi everyone, Im new to Corona but have been going through the tutorials. Im having trouble with the math.random function and spawning new rocks for the game.
Basically, what I want to happen is for new rocks to spawn off screen and fall with gravity. The player must tap the rock to make it disappear before it hits the bottom of the screen.
Ive successfully managed to get 1 rock on the screen to drop by playing around with code from different games, but Im having trouble spawning the others 20…
Ive been looking at the following video on YouTube ( http://www.youtube.com/watch?v=6TsDdLY7VXk ) to get some ideas about spawning new rocks, but so far no luck with getting anything on the screen. Can you take a look at this and let me know what Im missing?
Its probably something simple but Ive been playing with this all night and am still stumped.
Heres the "Spawn Rock" trial code Im working on…
Thanks in advance!
-WF
============================
– forward declarations and other locals
local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5
– VALUES for timers and Math related functions
mRand = math.random
– rocks
o = 0
time_remaining =10
time_up = false
total_rocks = 20 – Rocks for this game
ready = false
– SOUND LIBRARY ----
local soundtrack = audio.loadStream(“media/ Lady Gaga - Poker Face.mp3”)
– Sound Effects
local winsound = audio.loadSound(“tapsound.wav”)
local failsound = audio.loadSound(“youwin.wav”)
– DISPLAY TEXT -----
– Static Text Displays
local display_txt = display.newText(“Wait”,0,0, native.systemFont, 16+20)
display_txt.xScale = .5 display_txt.yScale = .5
display_txt:setReferencePoint(display.BottomLeftReferencePoint)
display_txt.x = 20 display_txt.y = _H-20
– Countdown Timer Display
local countdowntext = display.newText(time_remaining,0,0, native.systemFont, 16+20)
countdowntext.xScale = .5 countdowntext.yScale = .5
countdowntext:setReferencePoint(display.BottomRightReferencePoint)
countdowntext.x = _W-20 countdowntext.y = _H-20
– TIMER FUNCTIONS ----
– Create WIN / LOSE Condition
local function winLose(condition)
if(condition == “win”)then
display_txt.text = “WIN!”
elseif(condition == “fail”)then
display_txt.text = “FAIL”
end
– CREATE trackRocks Function
local function trackRocks(obj)
– Removes the Rocks
obj:removeSelf()
– decrease the total rocks in the dsplay
o = o-1
– Check to Make Sure GameTimer is not already at 0
if(time_up ~= true)then
– … then check if ALL rocks are removed from the display
if(o == 0)then
– Play a WIN Sound
audio.play(winsound)
– Stop The Game timer
timer.cancel(gametmr)
–call “WIN” Condition from WinLose
winLose(“win”)
end
end
end
– CREATE countdown FUNCTION
local function countdown(e)
if(time_remaining == 10)then
– Set Ready variable to TRUE
ready = true
– Change the countdown Text to Go!
display_txt.txt = “Go!”
– Play Soundtrack
audio.play(soundtrack,{loops=-1})
end
time_remaining = time_remaining-1
countdowntext = time_remaining
if(time_remaining == 0)then
time_up = true
– Check to see if there are any more rocks on the display, if not call “Fail” condition
if(o ~= 0)then
audio.play(failsound)
display_txt.text = “FAIL!”
ready = false
end
end
end
– CREATE spawnRock FUNCTION
local function spawnRock()
local rock = display.newImageRect(“asteroid_ss.png”, 45, 45)
rock.setReferencePoint(display.CenterReferencePoint)
rock.x = mRand(50, _W-50) rock.y = mRand(50, _H-50)
– Conditional Statement for Rocks Being Touched
function rock:touch(e)
if(ready == true)then
if(time_up ~= true)then
– process the Touch
if(e.phase == “ended”) then
– Play a sound
audio.play(winsound)
– Remove the Rocks
trackRocks(self)
end
end
return true
end
return true
end
– Increment Rocks when Spawned
o = o+1
– Rock Touch Event Listener
rock:addEventListener(“touch”, rock)
– If all Rocks created, start the game timer (*Change*)
if (o == total_rocks) then
gametmr = timer.performWithDelay(1000, countDown, 10)
else
ready = false
end
tmr1 = timer.performWithdelay(20, spawnRock, total_rocks)
end
===================== [import]uid: 22261 topic_id: 23806 reply_id: 323806[/import]