understanding spawning from the Martian controls app..

I don’t understand why my objects aren’t spawning… I followed everything that Beebe wrote in his code but I’m not grasping it… I replaced his movie clips with sprite loq…
[lua]display.setDefault(“background”, 255, 255, 255)
display.setStatusBar (display.HiddenStatusBar)

local physics = require( “physics” )
physics.start()

carSpawnRate = 3 --> how many seconds between each spawn

local gameGroup = display.newGroup()
local cGroup = display.newGroup()

gameGroup:insert( cGroup )

– EXTERNALS
local ui = require(“ui”)
local mRand = math.random
local mCeil = math.ceil

– LOGIC VARIABLES
local cCount

– GAME VARIABLES/SETTINGS
local carSpawnFrameCount = 1
local isOnFirstCar = true
local currentComeFrom = mRand( 1, 4 ) --> controls direction in which spawn from
local spawnNewCar = function( params )
local c, x, y, cameFrom

local loqsprite = require(‘loq_sprite’)
local cf = loqsprite.newFactory(‘sheet’)
local c = cf:newSpriteGroup()

local cRectShape = c:getRectShape()

cBody = { friction=0, bounce=0.1, density=0, shape = cRectShape }
physics.addBody(c, “dynamic”, cBody)

c.x = params.x; c.y = params.y

c:play(“greenCar drive”)

cCount = #cGroup

c.framePosition = 1
c.selfCount = cCount
c.frameCount = 1
c.cameFrom = params.cameFrom

local spawnNewCar = function()
– First decide what side of the screen object will come from
local theX, theY
currentComeFrom = currentComeFrom + 1

if currentComeFrom > 4 then
currentComeFrom = 1
end

local comeFrom = currentComeFrom

if comeFrom == 1 then --> left side of screen
if isOnFirstCar then
isOnFirstCar = false
theX = 0
else
theX = 0
end
theY = mRand( 50, 270 )

elseif comeFrom == 2 then --> top of screen
if isOnFirstCar then
isOnFirstCar = false
theY = 0
else
theY = 0
end
theX = mRand( 50, 430 )

elseif comeFrom == 3 then --> right side of screen
if isOnFirstCar then
isOnFirstCar = false
theX = 480
else
theX = 480
end
theY = mRand( 50, 270 )

elseif comeFrom == 4 then --> bottom of screen
if isOnFirstCar then
isOnFirstCar = false
theY = 320
else
theY = 320
end
theX = mRand( 50, 430 )
end

newCar( {
x = theX,
y = theY,
cameFrom = comeFrom
})

end

–===================================================================================


–===================================================================================

local carSpawner = function( event )

local spawnRate = mCeil(carSpawnRate * 60)

if carSpawnFrameCount >= spawnRate then
carSpawnFrameCount = 1 --> reset the counter

– Spawn a new Car
spawnNewCar()
else
carSpawnFrameCount = carSpawnFrameCount + 1
end

Runtime:addEventListener( “enterFrame”, carSpawner )
end
end

– MUST return a display.newGroup()
return gameGroup
end [/lua]

I just want to know why they aren’t showing up [import]uid: 51459 topic_id: 12299 reply_id: 312299[/import]

Have you actually called your spawning functions? If this is the whole file, where do you call carSpawner? [import]uid: 52491 topic_id: 12299 reply_id: 44789[/import]

Yea Its the whole file and thats the thing I don’t know where I should be calling it if its the whole file and I don’t even know if I have the whole thing right because he put this on the bottom of his code
[lua]math.randomseed(os.time())[/lua]

What does os mean?
[import]uid: 51459 topic_id: 12299 reply_id: 44796[/import]

When I add carSpawner() to the bottom of the screen it still doesn’t do anything… [import]uid: 51459 topic_id: 12299 reply_id: 44798[/import]

heres a more simpler code that I found from the sample apps called many creates… It drops many creates from the top of the screen… How do I make them come from the left to the right of the screen
[lua]–
– Abstract: ManyCrates sample project
– Demonstrates simple body construction by generating 100 random physics objects

– Version: 1.1 (revised for Alpha 2)

– Sample code is MIT licensed, see http://developer.anscamobile.com/code/license
– Copyright © 2010 ANSCA Inc. All Rights Reserved.

local physics = require(“physics”)
physics.start()

display.setStatusBar( display.HiddenStatusBar )

local bkg = display.newImage( “bkg_cor.png” )

local grass = display.newImage(“grass.png”)
grass.x = 160; grass.y = 430

local grass2 = display.newImage(“grass2.png”) – non-physical decorative overlay
grass2.x = 160; grass2.y = 440

physics.addBody( grass, “static”, { friction=0.5, bounce=0.3 } )
function newCrate()
rand = math.random( 100 )

if (rand < 60) then
j = display.newImage(“crate.png”);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=0.9, friction=0.3, bounce=0.3} )

elseif (rand < 80) then
j = display.newImage(“crateB.png”);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=1.4, friction=0.3, bounce=0.2} )

else
j = display.newImage(“crateC.png”);
j.x = 60 + math.random( 160 )
j.y = -100
physics.addBody( j, { density=0.3, friction=0.2, bounce=0.5} )

end
end

local dropCrates = timer.performWithDelay( 500, newCrate, 100 )[/lua] [import]uid: 51459 topic_id: 12299 reply_id: 44802[/import]

Hey,

Explanation of os.time is here.

To make the crates go from left to right, change their spawn coordinates to the left rather than the top, then change gravity to point to the right rather than downward using physics.setGravity.

Peach [import]uid: 52491 topic_id: 12299 reply_id: 44966[/import]

If I set the gravity that way can other objects go right to left still? [import]uid: 51459 topic_id: 12299 reply_id: 44987[/import]

Yes, you just need to tell them to with a transition or linearImpulse.

An object just sitting there (if effected by gravity) will go the way you’ve set gravity to move it. [import]uid: 52491 topic_id: 12299 reply_id: 45195[/import]