how to spawn an object when the e.phase == "ended"

Hey,

I’m having problem with code you’ll see:

[lua]local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

function spawnOrb()
local orb = display.newImage(“orb.png”);
orb.x = 60; orb.y = 400
physics.addBody(orb, “dynamic”)

end

end

function touchOrb(e)
if e.phase == “began” then
print(“orb began”)
display.getCurrentStage():setFocus(orb)

elseif e.phase == “ended” then
timer.performWithDelay(0, spawnOrb, 1);
print(“orb ended”)
orb:applyLinearImpulse(((e.xStart - e.x)/2), ((e.yStart - e.y)/2), orb.x, orb.y)
display.getCurrentStage():setFocus(nil)

end
end[/lua]

the problem is that I want when I throw the orb, I want another orb to show instantly,

I used 

[lua] timer.performWithDelay(0, spawnOrb, 1);
[/lua]

after the e.phase == “ended” then

but it doesn’t work.

please help.

Do you get any response from your print statements (i.e. “orb began” or “orb ended”) to indicate that Lua is seeing the function in the proper scope and then calling the spawn function?

Brent

yes, the function works perfectly, but there’s no spawning ! 

Hmmm… my first suggestion would be to place your spawn function outside of the “createScene” function (i.e. put it above and outside, after line 6 or so. And then put in a print statement to make sure it’s actually executing.

Best regards,

Brent

it didn’t work, but thank you very much mr. Brent , I found the solution 

the first problem is that i didn’t put 

  1. timer.performWithDelay(0, spawnOrb, 1)
  1. after the spawnOrb function,

so there was no orb to access the touchOrb function, and second or showed up but the touchOrb function didn’t work on the second orb,

so I put the touchOrb function in the spawnOrb function

 

and the “orb:addEventListener(“touch”, touchOrb)” under it, and now everything work fine,

 

thanks for trying to help me, Brent.

Do you get any response from your print statements (i.e. “orb began” or “orb ended”) to indicate that Lua is seeing the function in the proper scope and then calling the spawn function?

Brent

yes, the function works perfectly, but there’s no spawning ! 

Hmmm… my first suggestion would be to place your spawn function outside of the “createScene” function (i.e. put it above and outside, after line 6 or so. And then put in a print statement to make sure it’s actually executing.

Best regards,

Brent

it didn’t work, but thank you very much mr. Brent , I found the solution 

the first problem is that i didn’t put 

  1. timer.performWithDelay(0, spawnOrb, 1)
  1. after the spawnOrb function,

so there was no orb to access the touchOrb function, and second or showed up but the touchOrb function didn’t work on the second orb,

so I put the touchOrb function in the spawnOrb function

 

and the “orb:addEventListener(“touch”, touchOrb)” under it, and now everything work fine,

 

thanks for trying to help me, Brent.