multiple objects and functions- the logic?

I am trying to make an object instances in a function with a timer . I declare some methods for these objects like physics and transitions etc. But by making an object instances in a function, i can not call these instance object in other functions.

Below what i am trying to do

  
function makeobject1()  
 local object1 = display.newImage ("object1.png");  
 object1.xScale = 1  
 object1.yScale = 1  
 object1.alpha = 1  
 object1:setReferencePoint(display.CenterReferencePoint);  
 object1.x = 20  
 object1.y = display.contentHeight - 50  
 object1.myName = "object1"  
 object1.isBullet = true  
 physics.addBody(object1,"dynamic", {density=0.1, friction=0.8, bounce=.4,radius=30})  
 local trans1 = transition.to( object1, { 3000, x=display.contentWidth, y=object1.y,onComplete=killobje} )  
return object1  
end   
  
function makeobject2()  
 local object2 = display.newImage ("object2.png");  
 object2.xScale = 1  
 object2.yScale = 1  
 object2.alpha = 1  
 object1:setReferencePoint(display.CenterReferencePoint);  
 object2.x = display.contentWidth  
 object2.y = display.contentHeight - 50  
 object2.myName = "object2"  
 object2.isBullet = true  
 physics.addBody(object2,"dynamic", {density=0.1, friction=0.8, bounce=.4,radius=30})  
 local trans2 = transition.to( object2, { 3000, x=0, y=object2.y,onComplete=killobje} )  
return object2  
end   
  
function jump()  
 local dif = object2.x - object1.x  
 if (dif \< 50) then  
 object1:applyLinearImpulse ( 0, -.3, object1.x, object1.y)  
  
end  
  
function killobje(k)  
 k:removeSelf()  
end  
tmr1 = timer.performWithDelay (1000,makeobject1,10)   
tmr2 = timer.performWithDelay (1000,makeobject2,20)   
Runtime:addEventListener("enterFrame", jump)  
  

So no problem for creating new object instances but trying to interact with them i can not figure it out how. If i create two object not inside a function and give them both transitions and physics and it is ok but creating an object instances in a function and after function ends it gives a nil value. So what is the logic here? This is only two objects, if i want to increase more i will be stuck. Thank you in advance for your help.
[import]uid: 74718 topic_id: 12677 reply_id: 312677[/import]

so i put a local var to see the object1 and object2.x value, I see that i can not get the true x coordinate of transition or force applied object? Any method to get this? [import]uid: 74718 topic_id: 12677 reply_id: 46488[/import]

I believe if you declare them first it should fix the problem.

Check out the Ghosts VS Monsters sample code; that’s got an example of what I’m talking about :slight_smile:

Peach [import]uid: 52491 topic_id: 12677 reply_id: 46620[/import]

thank you i end up with something below;

[code]
i=0
j=0
object1table = {}

function makeobject1()
i=i+1
local object1 = display.newImage (“object1.png”);
object1.xScale = 1
object1.yScale = 1
object1.alpha = 1
object1:setReferencePoint(display.CenterReferencePoint);
object1.x = 20
object1.y = display.contentHeight - 50
object1.myName = “object1”
object1.isBullet = true
physics.addBody(object1,“dynamic”, {density=0.1, friction=0.8, bounce=.4,radius=30})
object1table[i]=object1
return object1
end

object2table = {}
function makeobject2()
j=j+1
local object2 = display.newImage (“object2.png”);
object2.xScale = 1
object2.yScale = 1
object2.alpha = 1
object1:setReferencePoint(display.CenterReferencePoint);
object2.x = display.contentWidth
object2.y = display.contentHeight - 50
object2.myName = “object2”
object2.isBullet = true
physics.addBody(object2,“dynamic”, {density=0.1, friction=0.8, bounce=.4,radius=30})
object2table[j]=object2
return object2
end

function jump()
for k=1,#object1table do
for m=1,#object2table do
local dif = object1table[k].x - object2table[m].x
–can get x coordinate every objects in the tables
print()
end
end
end

function start()
Runtime:addEventListener(“enterFrame”, jump)
end

tmr1 = timer.performWithDelay (1000,makeobject1,10)
tmr2 = timer.performWithDelay (1000,makeobject2,20)
tmr3 = timer.performWithDelay (1100,start)

[/code] [import]uid: 74718 topic_id: 12677 reply_id: 46628[/import]