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]
