Create a method to spawn the objects. (of course you can spawn as many objects as you want)
[
local _W = display.contentWidth
local _H = display.contentHeight
local mRand = math.random
– SPRITE SHEETS (assumes all the sprite sheets are same dimensions)
local optionsObject = { width = 64, height = 64, numFrames = 4, sheetContentWidth = 256, sheetContentHeight = 256 }
local object1_Sheet = graphics.newImageSheet(“Textures/obj1.png”, optionsObject)
local object2_Sheet = graphics.newImageSheet(“Textures/obj2.png”, optionsObject)
local object3_Sheet = graphics.newImageSheet(“Textures/obj3.png”, optionsObject)
sequenceData = { name = “moving”, start = 1, count = 4, time = 100 }
– CREATE A TABLE OF ENEMIES (3 or 100 or as many as you want)
local enemyID = 1
local enemies = {}
for i = 1, 100 do
local index = mRand(1,3)
if index == 1 then
enemies[i] = display.newSprite(object1_Sheet,sequenceData)
elseif index == 2 then
enemies[i] = display.newSprite(object2_Sheet,sequenceData)
elseif index == 3 then
enemies[i] = display.newSprite(object3_Sheet,sequenceData)
end
end
local function spawn( )
local screenEdge = mRand( 1, 4)
if screenEdge == 1 then – top of screen
enemies[enemyID].x = mRand(50, _W-50)
enemies[enemyID].y = -50
elseif screeEdge == 2 then – right side of screen
enemies[enemyID].x = _W+50
enemies[enemyID].y = mRand(50, _H-50)
elseif screeEdge == 3 then – bottom side of screen
enemies[enemyID].x = mRand(50, _W-50)
enemies[enemyID].y = _H+50
elseif screeEdge == 4 then – left side of screen
enemies[enemyID].x = _W-50
enemies[enemyID].y = mRand(50, _H-50)
end
enemyID = enemyID + 1
end
– SPAWN 100 ENEMIES
timer.performWithDelay(1000, spawn, 100)
]
Of course you can change this to create the enemies in the spawn method instead of creating the 100 enemies ahead of time.
In either case, you should have a method call that will handle cleaning up the enemy objects once they are destroyed, no longer needed, and if you want to destroy/save/pause the enemies when you leave the present storyboard scene. But I only had time to slap together this example, just to directly answer your immediate question.
As far as using within storyboard, it makes sense you would do all this in the ‘gameScene’ or ‘levelScene’ whatever you titled the main scene of your app where all this action is to occur.
Good luck!
Hope this helps you with you question.