Spawn objects on another?

In my game I have an object falling, what I’m looking to do is when you tap object1, five object2’s will spawn from object1. I only know how to spawn objects at a specific point, but since object1 is moving I have no clue how to go about doing this, can anyone help? :}

[code]
function obj1Spawn()
rand = math.random( 90 )
if(rand < 100 ) then
obj1 = display.newImage(“obj1.png”)
obj1.x = math.random(5, 280)
obj1.y = -60
physics.addBody( obj1, {density = 2.0, friction = 1.3})
obj1.myName = “obj1”
obj1.collision = onobj1Collision
obj1:addEventListener(“collision”, obj1 )
end
end

local spawnObj1 = timer.performWithDelay( 5786, obj1Spawn, 100)

function onObj1Collision( self, event )
if (event.phase == “began” ) then
if(self.myName == “obj1”) then
if(event.other.myName == “player”) then
score = score + 10
scoreText.text = tostring(score)
self:removeSelf()
elseif (event.other.myName == “ground”) then
self:removeSelf()
end
end
end
end [import]uid: 114389 topic_id: 22931 reply_id: 322931[/import]

_Disclaimer: I’m a noob so I might not know what I’m talking about. But maybe I can get you started in the right direction

Looking at your code, it looks like you’re spawning a new object every second. The problem with your current setup is that as soon as you spawn a new object, the old object looses its reference. There is no way of distinguishing which object the user is touching, or if the user is touching any object at all.

What you need to do instead, is to keep track of the spawned objects that you make. The best way to do this (or maybe the only way?) is to store the objects in a table.

[code]
objectTable {} – creates a table to store the objects

function obj1Spawn()
rand = math.random( 90 )
if(rand < 100 ) then

local obj1 = display.newImage(“obj1.png”)
objectTable[#objectTable+1] = obj1

obj1.x = math.random(5, 280)
obj1.y = -60
physics.addBody( obj1, {density = 2.0, friction = 1.3})
obj1.myName = “obj1”
obj1.collision = onobj1Collision
obj1:addEventListener(“collision”, obj1 )

end
end
[/code] [import]uid: 78150 topic_id: 22931 reply_id: 91608[/import]_