About the Director class and modularity

Hey there,

The gameplay for my game is complete, so I decided to create a menu next. I looked at the Director class sample code that comes with Director, where Ricardo does

[lua]localGroup:insert( background )
localGroup:insert( title )

…[/lua]

in screen1.lua, for every display item he has. Now, my problem is, I create all my display objects in separate modules. And then it gets a bit more complicated…

Let me just post my level1.lua and my car.lua, if you see them you’ll understand better than if I explained it to you. I will leave methods and values that are irrelevant out, to keep the code shorter.

car.lua:
[lua]–MAKE EVERYTHING IN THIS MODULE VISIBLE
module(…,package.seeall)

–INCLUDE FILES
local physics = require(“physics”)
local setup = require(“setup”)
local ped = require(“ped”)

–SETUP PHYSICS

–GLOBALS
–LOCALS

–CAR CREATOR
function new(params)
local newCar
newCar = display.newImage(“car.png”)

–GLOBALS

–LOCALS

–CAR METHODS ( MOVE, DESTROYCAR, ETC.)

–CAR FACTORY
function makeCars(event)

if flag == 1 then
aCar = car.new({x = spawnX, y = spawnY})
aCar.carCat = 1
physics.addBody(aCar)
aCar.isSensor = true
aCar.moveCar()
aCar:addEventListener(“collision”, onLocalCollision)
Runtime:addEventListener(“enterFrame”, destroyCars)
aCar:addEventListener(“tap”, tap)
end

if flag == 2 then
bCar = car.new({x = spawn2X, y = spawn2Y})
bCar.carCat = 2
bCar:rotate(270)
physics.addBody(bCar)
bCar.isSensor = true
bCar.moveCar()
bCar:addEventListener(“collision”, car.onLocalCollision)
Runtime:addEventListener(“enterFrame”, car.destroyCars)
bCar:addEventListener(“tap”, tap)
end

end

–CAR SPAWNER
function spawnCars(event)
math.randomseed(os.time())
local random = math.random(math.abs(10), math.abs(70)) * 10
local random2 = math.random(math.abs(10), math.abs(70)) * 10
if aCar.y > spawnY + random then
flag = 1
makeCars()
end
if bCar.x > spawn2X + random2 then
flag = 2
makeCars()
end
end
--------------------------------------------[/lua]

and level1.lua (so far):

[lua]module(…, package.seeall)

new = function (params)

–INCLUDE FILES
local ui = require(“ui”)
local setup = require(“setup”)
local car = require(“car”)
local physics = require(“physics”)
local stp = require(“stp”)
local ped = require(“ped”)
local director = require(“director”)

–SETUP PHYSICS

–GLOBALS

localGroup = display.newGroup()

displayBackground()
displayScoreLabel({x=screenRight,y=screenTop - 1})
displayScore()
timer.performWithDelay(5, showCoords, 0)
timer.performWithDelay(5, setup.showCoords, 0)

–CREATE GAME OBJECTS
car.flag = 1 --Normally, the flag would be automatically set to spawn cars, but here it has to be set
car:makeCars() --manually to spawn the first car of each lane. The car-spawning function detects when
car.flag = 2 --the previously created car has moved a certain (random each time) number of pixels from
car:makeCars() --its original position and then spawns another car, so for the first cars, this has to be
–done manually.

car.flag = 0 --After creating the first cars, start running the car-crating function normally.
Runtime:addEventListener(“enterFrame”, car.spawnCars)

ped.makePeds()

return localGroup

end[/lua]

As you can see, I have car.new to create a car, but then car.makeCars to give car.new the parameters it needs and make a simple image into a game object (add it to the physics engine, make it a sensor, etc) and then spawnCars to actually call makeCars when I want a new car created.

I obviously can’t use Director the way Ricardo does in his sample code.

So, my questions are:

Where do I include all of my display objects? For example, do, I do localGroup:insert(newCar) after I create newCar, or do I have to do something with the methods making the cars?

Where do I create localGroup? It seems that with my setup, it will need to be global. Will it?

Please try to answer both questions, if you can. Thank you for your help and time :smiley: [import]uid: 106739 topic_id: 20643 reply_id: 320643[/import]

Couple ways you can do it.

  1. pass the localGroup to the car:makeCars() function – car:makeCars(localGroup) – and insert the object into the group in the method; or

  2. have the makeCars() method return the car (return aCar or return bCar) to the calling function, which will make it available to insert into a group, so

car:makeCars()

becomes

localGroup:insert( car:makeCars() )

or

local thisCar = car:makeCars()
local thatCar = car:makeCars()
localGroup:insert(thisCar)
localGroup:insert(thatCar)

localGroup should always be local to the scene, which is a given lua file. You don’t need Director stuff in car.lua since it doesn’t seem to be a scene, just a factory.

Also, by the way, don’t put math.randomseed in your spawnCars() function, you won’t get any randomness if you’re reseeding with the same os.time() value on every frame. Put it in main.lua as near to the start of your app as possible, and do it only once. [import]uid: 44647 topic_id: 20643 reply_id: 88607[/import]