How to create spawners with sprites?

Hi, I’m actually trying to create spawners for different ennemies (which are displayed with sprites).

I tried this way : http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

It does work with a regular image, but can’t make it work with sprites.

Anybody would know how?

Thank you!

Hi @nekozhead,

I recently posted a new tutorial on basic spawning which contains a module that you can download and modify.

http://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

As for using sprites, this should be basically the same as spawning images. If there’s some issue with that, it might be how you’re creating the sprites (another topic to discuss in that case).

Best regards,

Brent

Hi Brent! Thank you so much for your fast answer! :smiley:

Tried the tutorial (really easy to understand by the way). 

I just get this error though when calling spawnController(“start”,spawnParams) : main.lua:61:bad argument #2 to random (number expected, got nil)

stack traceback: [C]: in fonction random

                           main.lua 61: in function spawnItem

                           main.lua 89: in function spawnController

                           main.lua 110: in main chunk (this the line the controller is called)

i really don’t know why…is it because I haven’t set an event listener? I wanted to test it out and make sure it works before creating my other ennemies spawners…(and see if the sprites were working…they should be cause no error regarding them)

Thanks again for your help so far

Nekoz

Here’s my main if you wanna look at it :) 


– main.lua


–Background Display

local background = display.newImage(“Graphics/skin.png”)

background.x = 240

background.y = 170

background:scale (0.6,0.6)


–SpriteSheet Setting

local sheetInfo = require “sprite”

local myImageSheet = graphics.newImageSheet(“Graphics/MarinSheet.png”,sheetInfo:getSheet())

local sequenceData = {

      {name=“MarinAfly”, sheet = myImageSheet, start=sheetInfo:getFrameIndex(“MarinA_vole_01”),count=4,time=600,loopcount=0}

}


–Ennemy Display

–Mosqui = display.newSprite (myImageSheet,sequenceData)

–Mosqui.x = 250

–Mosqui.y = 250

–Mosqui:scale ( 0.6,0.6)

–Mosqui:setSequence(“MarinAfly”)

–Mosqui:play()


–Spawners

local spawnTimer

local spawnedObjects = {}

local spawnParams = {

       xMin = 50,

       xMax = 750,

       yMin = 20,

       yMax = 1100,

       spawnTime = 200,

       spawnOnTimer = 12,

       spawnInitial = 4,

}

local function spawnItem (bounds)

       local item = display.newSprite (myImageSheet,sequenceData)

             item:setSequence(“MarinAfly”)

             item:play()

             item.x = math.random(bounds.xMin, bounds.xMax)

             item.y = math.random(bounds.yMin, bounds.yMax)

             spawnedObjects[#spawnedObjects+1] = item

end

local function spawnController(action, params)

      

      if (spawnTimer and (action == “start” or action == “stop”))then

          timer.cancel(spawnTimer)

      end

      if (action == “start”) then

      

       local spawnBounds = {}

       spawnBounds.xMin = params.xMin or 0

       spawnBounds.xMAx = params.xMax or display.contentWidth

       spawnBounds.yMin = params.yMin or 0

       spawnBounds.yMax = params.yMax or display.contentHeight

  

       local spawnTime = params.spawnTime or 1000

       local spawnOnTimer = params.spawnOnTimer or 50

       local spawnInitial = params.spawnInitial or 0

       if (spawnInitial > 0) then

          for n = 1, spawnInitial do

           spawnItem(spawnBounds)

           end

       end

       if (spawnOnTimer > 0) then

          spawnTimer = timer.performWithDelay (spawnTime,function()spawnItem(spawnBounds); end,

         spawnOnTimer)

       end

   

       elseif (action == “pause”) then

            timer.pause(spawnTimer)

       elseif (action == “resume”) then

            timer.resume(spawnTimer)

       end

 end

spawnController(“start”,spawnParams)

Hi @nekozhead,

I recently posted a new tutorial on basic spawning which contains a module that you can download and modify.

http://coronalabs.com/blog/2014/11/04/tutorial-basic-spawning/

As for using sprites, this should be basically the same as spawning images. If there’s some issue with that, it might be how you’re creating the sprites (another topic to discuss in that case).

Best regards,

Brent

Hi Brent! Thank you so much for your fast answer! :smiley:

Tried the tutorial (really easy to understand by the way). 

I just get this error though when calling spawnController(“start”,spawnParams) : main.lua:61:bad argument #2 to random (number expected, got nil)

stack traceback: [C]: in fonction random

                           main.lua 61: in function spawnItem

                           main.lua 89: in function spawnController

                           main.lua 110: in main chunk (this the line the controller is called)

i really don’t know why…is it because I haven’t set an event listener? I wanted to test it out and make sure it works before creating my other ennemies spawners…(and see if the sprites were working…they should be cause no error regarding them)

Thanks again for your help so far

Nekoz

Here’s my main if you wanna look at it :) 


– main.lua


–Background Display

local background = display.newImage(“Graphics/skin.png”)

background.x = 240

background.y = 170

background:scale (0.6,0.6)


–SpriteSheet Setting

local sheetInfo = require “sprite”

local myImageSheet = graphics.newImageSheet(“Graphics/MarinSheet.png”,sheetInfo:getSheet())

local sequenceData = {

      {name=“MarinAfly”, sheet = myImageSheet, start=sheetInfo:getFrameIndex(“MarinA_vole_01”),count=4,time=600,loopcount=0}

}


–Ennemy Display

–Mosqui = display.newSprite (myImageSheet,sequenceData)

–Mosqui.x = 250

–Mosqui.y = 250

–Mosqui:scale ( 0.6,0.6)

–Mosqui:setSequence(“MarinAfly”)

–Mosqui:play()


–Spawners

local spawnTimer

local spawnedObjects = {}

local spawnParams = {

       xMin = 50,

       xMax = 750,

       yMin = 20,

       yMax = 1100,

       spawnTime = 200,

       spawnOnTimer = 12,

       spawnInitial = 4,

}

local function spawnItem (bounds)

       local item = display.newSprite (myImageSheet,sequenceData)

             item:setSequence(“MarinAfly”)

             item:play()

             item.x = math.random(bounds.xMin, bounds.xMax)

             item.y = math.random(bounds.yMin, bounds.yMax)

             spawnedObjects[#spawnedObjects+1] = item

end

local function spawnController(action, params)

      

      if (spawnTimer and (action == “start” or action == “stop”))then

          timer.cancel(spawnTimer)

      end

      if (action == “start”) then

      

       local spawnBounds = {}

       spawnBounds.xMin = params.xMin or 0

       spawnBounds.xMAx = params.xMax or display.contentWidth

       spawnBounds.yMin = params.yMin or 0

       spawnBounds.yMax = params.yMax or display.contentHeight

  

       local spawnTime = params.spawnTime or 1000

       local spawnOnTimer = params.spawnOnTimer or 50

       local spawnInitial = params.spawnInitial or 0

       if (spawnInitial > 0) then

          for n = 1, spawnInitial do

           spawnItem(spawnBounds)

           end

       end

       if (spawnOnTimer > 0) then

          spawnTimer = timer.performWithDelay (spawnTime,function()spawnItem(spawnBounds); end,

         spawnOnTimer)

       end

   

       elseif (action == “pause”) then

            timer.pause(spawnTimer)

       elseif (action == “resume”) then

            timer.resume(spawnTimer)

       end

 end

spawnController(“start”,spawnParams)