Trouble with Spawning new Rocks at Random Position

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]

You could pretend it’s called “Billions of Rocks” and take a look at this video:

http://gamedevnation.com/game-development/billions-of-stars/

I Just put it top yesterday and it walks through a simple example of putting objects in random locations on the screen.

You’re doing more than that in the code above, but it might get you back on track.

Jay

PS - If you edit your post and wrap the code in code tags it will make it *much* easier for people to read. <code> all your code here </code>
[import]uid: 9440 topic_id: 23806 reply_id: 95866[/import]

@J. A. Whye

Thanks so much for the reply and the recommendation about the [code] tags. Had no clue about those. Checking out your video now and will let you know how it goes!
[import]uid: 22261 topic_id: 23806 reply_id: 95892[/import]

@J.A. Whye

Hi J.A.,

Thanks very much for your help last week. The code for billions of stars was EXACTLY what I was looking for in order to get random rocks to spawn on my screen.

The problem I`m having now is actually getting gravity to work with the rocks… I want the rocks to randomly spawn and then fall with gravity to the bottom the screen until they get a collision with the bottom border (after which they will be removed from the screen and the player will lose points).

Ive been playing around with your original billions of stars file, just to see if I can get the gravity to work on it, but no dice so far... Heres the code that I`m using.

local physics = require "physics"  
physics.start(); physics.pause()  
physics.setGravity(0,2)  
physics.setDrawMode("hybrid")  
display.setStatusBar(display.HiddenStatusBar)  
  
local w = display.contentWidth  
local h = display.contentHeight  
local rnd = math.random  
local maxStars = 10  
  
local stars = {"grey star.png", "glow star.png", "yellow star.png", "asteroid\_ss.png"}  
local allStars = {}  
  
local function starTap(e)  
 print(e.target.name)  
 e.target.alpha = 0  
 return true  
end  
  
local function makeAStar(id)  
 local staridx = rnd(1, #stars)  
 local randX = rnd(20, w-20)  
 local randY = rnd(20, h-20)  
 local starsize = rnd(25, 75) / 100  
 local star = display.newImage(stars[staridx])  
 star.x = randX  
 star.y = randY  
 star:scale ( starsize, starsize )  
 star.name = "star" .. tostring(id)  
 star:addEventListener ( "tap", starTap )  
 allStars[#allStars+1] = star  
end  
  
for v = 1, maxStars do  
 makeAStar(v)  
end  
  
-- add physics to the lavaRock  
 --physics:addBody(allStars, { density=0.025, friction=0.3, bounce=0.3, radius=40 } )  
 physics:addBody(stars, { density=0.025, friction=0.3, bounce=0.3, radius=40 } )  
  
  
-- all display objects must be inserted into group  
 --group:insert(allStars)  
 group:insert(stars)  
  

I keep getting the error below, but I cant figure out what it means or why Im getting a nil value. I`m not sure how to clear nil values out yet or what causes them exactly…


Runtime error
/Users/user/Desktop/billionsofstars/main.lua:51: bad argument #-2 to ‘addBody’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘addBody’
/Users/user/Desktop/billionsofstars/main.lua:51: in main chunk
Runtime error: /Users/user/Desktop/billionsofstars/main.lua:51: bad argument #-2 to ‘addBody’ (Proxy expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘addBody’
/Users/user/Desktop/billionsofstars/main.lua:51: in main chunk


Can you please let me know where I`m going wrong with this?
Thanks in advance!

-WF

[import]uid: 22261 topic_id: 23806 reply_id: 97971[/import]

In physics:addBody, you need to add a display object. You are adding a table of strings. You need something like

[lua]star=display.newImage(“foo.png”,100,200)
physics.addBody(star,…)[/lua]
[import]uid: 84768 topic_id: 23806 reply_id: 97976[/import]

What jfb said. So put the physics.addBody inside the makeAStar function, and use star as the parameter (the star you just created), not stars.

Also, you can set radius=starsize/2 to make sure the collision rectangle is the proper size for each individual rock/star.

Jay

[import]uid: 9440 topic_id: 23806 reply_id: 98124[/import]