About spawning and get access to them

Hello!

I wonder, do i need to put spawned objects in tables to be able to access them? I only need one spawned object at the same time so didn’t thought a table was necessary?

this is what i got:
[lua]local spawnStar = function(event)

if (starOnScreen == true) then

local star = display.newImage(“star.png”, true)

star.x = math.random(1, 700)
star.y = math.random(1, 1000)

physics.addBody( star, { density = 1.0, friction = 0, bounce = 0.3, radius = 25 } )
star.bodyType = “static”
print(star.x)

starOnScreen = false

end

end

Runtime:addEventListener(“enterFrame”, spawnStar)
local starCollection = function(event)

if (event.phase == “began”) then

star:removeSelf()

starOnScreen = true

end

end

star:addEventListener(“collision”, starCollection)[/lua]

Thought this would do to spawn a star and then that star collide with something it would remove it self? But nothing happens i don’t know if i need to return the star maybe? but tried return star and didn’t either work:(
Thanks in advance=)

[import]uid: 90942 topic_id: 23542 reply_id: 323542[/import]

You can’t access ‘star’ because it’s set up as local object within your spawnStar function - therefore only spawnStar can reference it.

put this at the top of your code:

[lua]local star[/lua]

and change your star creation line to:

[lua]star = display.newImage(“star.png”, true)[/lua] [import]uid: 93133 topic_id: 23542 reply_id: 94504[/import]

This should help you move along

[code]
local physics = require(“physics”)

physics.start()

Random = math.random

local screenW = display.contentWidth
local screenH = display.contentHeight
local screenHW = screenW *.5
local screenHH = screenH *.5

local function MySimulation()
local maxObjects = 200

–physics.setDrawMode(“hybrid”)

local gameStage = display.newGroup()
local gameBounds = display.newGroup()
gameStage:insert(gameBounds)
local gameObjCol = display.newGroup()
gameStage:insert(gameObjCol)

local function MakeCollisionBounds()
local tRectTop = display.newRect(gameBounds, 0, 0, screenW, 10)
physics.addBody(tRectTop, “static”, {density = 1.0, friction = 1.0})
local tRectBottom = display.newRect(gameBounds, 0, screenH - 10, screenW, 10)
physics.addBody(tRectBottom, “static”, {density = 1.0, friction = 1.0})
local tRectLeft = display.newRect(gameBounds, 0, 0, 10, screenH)
physics.addBody(tRectLeft, “static”, {density = 1.0, friction = 1.0})
local tRectRight = display.newRect(gameBounds, screenW - 10, 0, 10, screenH)
physics.addBody(tRectRight, “static”, {density = 1.0, friction = 1.0})
end
MakeCollisionBounds()

local function ObjCollision(self, event)
if(event.phase == “began”)then
if(self.isAlive)then
self.isAlive = false
self:setFillColor(255,0,0,255)

–The 1000 ms delay is just for showing the Red(collided) color for awhile, but you do need
–a delay of 1 ms or 2 if removing within an Objects collision
timer.performWithDelay(1000, function() self:removeEventListener(“collision”, self) self:removeSelf() self = nil end)
end
end
end

local function FrameLoop()
if(gameObjCol.numChildren < maxObjects)then
–local Obj = display.newRect(gameObjCol, Random(20, 700), Random(20, 1000), 15, 15)
–or
local Obj = display.newRect(gameObjCol, screenHW, screenHH, 15, 15)
Obj.isAlive = true
Obj:setFillColor(0,0,255,255)

physics.addBody(Obj, “dynamic”, {density = 1.0, friction = 1.0, bounce = 0.5})

Obj.collision = ObjCollision
Obj:addEventListener(“collision”, Obj)

Obj:applyLinearImpulse(Random(-20,20), Random(-20,20), Obj.x, Obj.y)
end
end

Runtime:addEventListener(“enterFrame”, FrameLoop)
end
MySimulation()

[/code] [import]uid: 21331 topic_id: 23542 reply_id: 94497[/import]

That’s no fun. :wink: I was trying to give more to help move along understanding of other mechanisms as well.

Inkoqnito, I hope you enjoy the popcorn machine. :slight_smile: [import]uid: 21331 topic_id: 23542 reply_id: 94509[/import]

Hello Tony and Nick!

Thanks for the replies=)

Tony: It was pretty much to start with but will take a look at it=) Just have to understand everything in it=)

Nick: I tried what you said but it still won’t work:( it complains that attempt to index local ‘star’ a nil value so it don’t seems like the creating of the star won’t really use the local star that i have in the beginning now:( The star get created so i can see it in the simulator but the ufo that i also have won’t collide with the star its like the star aint added to the physics engine?

Hope i can get some more assistance=)
You guys really are great=)

[import]uid: 90942 topic_id: 23542 reply_id: 94533[/import]

There are quite a few things fundamentally wrong with your code block, I emplore you to run the sample I provided you and study it to glean the information you require. It has everything that you asked for (and a little extra). It is not as complex as you might think if you just look it over.

That being said, the simplest way I can get your code to run is:

[code]
local physics = require(“physics”)
physics.start()

local star = nil
local starOnScreen = false

local screenW = display.contentWidth
local screenH = display.contentHeight

local tRectBottom = display.newRect(0, screenH - 10, screenW, 10)
physics.addBody(tRectBottom, “static”, {density = 1.0, friction = 1.0})

function starCollision(self, event)
if (event.phase == “began”) then
print(“Collided”)
timer.performWithDelay(1, function()
self:removeEventListener(“collision”, self)
self:removeSelf()
starOnScreen = false
end)
end
end

local function spawnStar()
if (starOnScreen == false) then
–local star = display.newImage(“star.png”, true)
star = display.newCircle(50,0,25)

physics.addBody(star, “dynamic”, { density = 1.0, friction = 0, bounce = 0.3, radius = 25 } )
star.collision = starCollision
star:addEventListener(“collision”, star)

starOnScreen = true
end
end

Runtime:addEventListener(“enterFrame”, spawnStar)
[/code] [import]uid: 21331 topic_id: 23542 reply_id: 94543[/import]