Step 3: Creating Objects

spawnTarget Function

Lets go ahead and add stuff to our scene now.
As a first step lets spawn some targets to shoot at with a new function called spawnTarget.
In this function you can create a new object with SceneManager.createObject by passing it the filename of the object to create.
The createObject function will return a handle to the object.
With this handle you can set the position with setPosition to a random point 20 meters above the ground.
You can also give it the keyword “target” to make it easier to find later on.

 

Loops

You can use a simple for loop to call the spawnTarget function 6 times in the onSceneStart function.
After you’ve done this you can hit test and see the new targets appear.

 


    -- onSceneStart is called first to start the game
function onSceneStart()
 
-- Output to the debug console
print ("Hello World")
        -- Add camera controls
    pushFPSCamera()
 
        -- Set initial camera position
    Camera.setEye(vec(0,10,5))
    Camera.setFocus(vec(0,0,2))
   
        -- Create six targets
    for i=1,6 do
        spawnTarget()
    end
end
    -- Create a new target
function spawnTarget()
 
        -- Create the target and position it randomly
    local target = SceneManager.createObject("asmedberg/bobblePenguin")
    target:setPosition(vec(math.random(-10,10),math.random(-10,10),20))
   
        -- Apply the keyword target
    target:addKeywords("target")
 
end