Calling an object from another function

Hi greetings,

I created a function associated with transition of the object. And very simply it worked. The next step was to call the obj.x and y to another function, but I couldn’t. It is giving a nil value. Why? How can I call the object?

You need to read up on lua scope.

https://docs.coronalabs.com/tutorial/basics/scope/index.html

Also, show us the code.

I think you’re talking about doing something , but I’m just guessing.

Totally random example showing a ping-pong-motion + accessing fields on object after transition complete, plus swapping onComplete listeners.

local obj = display.newCircle( 10, 10, 10 ) function obj.onComplete1( self ) self.y = 10 self.onComplete = self.onComplete2 transition.to( self, { x = 100, onComplete = self } ) end function obj.onComplete2( self ) self.y = 50 self.onComplete = self.onComplete1 transition.to( self, { x = 10, onComplete = self } ) end obj:onComplete1()

interesting Loop. Very Nice

last line; obj:oncomplete1() will be obj:onComplete1()

local function onComplete (obj) obj:removeSelf() end local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete} ) enemies[eidx] = enemy enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" end end return true end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) local hero = display.newCircle( 10, 10, 10 ) local function moveHere(event) transition.to( hero, {time = 2000, x = event.x, y = event.y} ) end Runtime:addEventListener("tap", moveHere)

I want to call the enemies inside the moveHere or vice versa so that I will be able to create collision events through setting an area related to their x and y. The result after collision the red ball turns white.

function hero.tap( self, event) transition.cancel( self ) transition.to( self, {time = 2000, x = event.x, y = event.y} ) end hero:addEventListener("tap")
local function onComplete (obj) obj:removeSelf() end local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) print(hero.x .. hero.y) -- I want to print the heroes coordinates while transition happens if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete} ) enemies[eidx] = enemy enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" end end return true end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) hero = display.newCircle( 10, 10, 10 ) function hero.tap( self, event) transition.cancel( self ) transition.to( self, {time = 2000, x = event.x, y = event.y} ) end hero:addEventListener("tap")

it isn’t working

hero:addEventListener(“tap”)

this function calls when we tap on the hero. Even if I gave a function to it:

hero:addEventListener(“tap”, moveHere)

necessarily I have to  tap the hero right? but we want the hero to move anywhere tapped on the screen thats why I have used runtime. 

Correct me if am wrong.

_G.hero = display.newCircle( 10, 10, 10 )

now it is listening in all functions the hero, while it updates. Cheers! 

Thanks Sirs, Nick and Roaming Gamer…

local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) local function updateEnemyTable (enemy) enemy:removeSelf() enemy = nil table.remove(enemies, enemies.eidx) end transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete = updateEnemyTable} ) enemies[eidx] = enemy enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" end local function testCollisions() for i = 1, #enemies do if ( (hero.x \> (enemies[i].x-25)) and (hero.x \< (enemies[i].x+25)) ) and ( (hero.y \> (enemies[i].y-25)) and (hero.y \< (enemies[i].y+25)) ) then kHave = true print("closer to enemy") enemies[i]:setFillColor(0,1,0) --print(enemies[1].x) end end return kHave end Runtime:addEventListener("enterFrame", testCollisions) end return enemies end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) \_G.hero = display.newCircle( 10, 10, 10 ) function moveHere(event) transition.to( hero, {time = 2000, x = event.x, y = event.y} ) return hero end Runtime:addEventListener("tap", moveHere)

I am getting an error while computing x of enemies, but am able to change the colours. Is it because I couldn’t remove the objects fully from table? or the table is confused?

The problem is I am not able to remove the object from table properly. Getting a nil value. The function was found to be working properly as I intended when updateEnemyTable was diabled. Need a solution to remove properly.

Don’t use numeric indexes.  Index the table with the enemy reference.

 local function updateEnemyTable (enemy) enemies[enemy] = nil display.remove( enemy ) end transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete = updateEnemyTable} ) enemies[enemy] = enemy enemy.eidx = eidx enemy.myName = "enemy" end

but then the collision will be in trouble right?

local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) local function updateEnemyTable (enemy) enemy:removeSelf() enemy = nil --display.remove( enemy ) --table.remove(enemies,enemy.eidx) print(#enemies) end if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) enemies[eidx] = enemy transition.to( enemies[eidx], {time = 10000, x = 300--[[display.contentWidth + 200]], onComplete = updateEnemyTable} ) enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" --enemies[enemy] = enemy --enemy.eidx = eidx --enemy.myName = "enemy" end local function testCollisions() for i = 1, #enemies do if enemies[i].x == 300 or enemies[i].x == nil then --enemies[i].x = 20 --print("reached") --enemies[i]:removeSelf() --enemies[i] = nil --table.remove(enemies, i) else if ( (hero.x \> (enemies[i].x-25)) and (hero.x \< (enemies[i].x+25)) ) and ( (hero.y \> (enemies[i].y-25)) and (hero.y \< (enemies[i].y+25)) ) then kHave = true print("closer to enemy") enemies[i]:setFillColor(0,1,0) --print(enemies[1].x) end end end return kHave end Runtime:addEventListener("enterFrame", testCollisions) end return enemies end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) \_G.hero = display.newCircle( 10, 10, 10 ) function moveHere(event) transition.to( hero, {time = 2000, x = event.x, y = event.y} ) return hero end Runtime:addEventListener("tap", moveHere)

removing from table through calling index value not working. Why???

Rest all working. I am able to remove from the screen and keep nil but I am not able to remove from the table entirely. Why? Isn’t possible?

for i = 1, #enemies do -- is for numerically indexed tables display.remove(enemies[i]) -- object reference end for k, v in pairs( enemies ) do -- is for non-numerically indexed tables enemies[k] = nil -- k is the key display.remove( v ) -- v is the object reference end

Thank you so much ‘roaminggamer’. 

You need to read up on lua scope.

https://docs.coronalabs.com/tutorial/basics/scope/index.html

Also, show us the code.

I think you’re talking about doing something , but I’m just guessing.

Totally random example showing a ping-pong-motion + accessing fields on object after transition complete, plus swapping onComplete listeners.

local obj = display.newCircle( 10, 10, 10 ) function obj.onComplete1( self ) self.y = 10 self.onComplete = self.onComplete2 transition.to( self, { x = 100, onComplete = self } ) end function obj.onComplete2( self ) self.y = 50 self.onComplete = self.onComplete1 transition.to( self, { x = 10, onComplete = self } ) end obj:onComplete1()

interesting Loop. Very Nice

last line; obj:oncomplete1() will be obj:onComplete1()

local function onComplete (obj) obj:removeSelf() end local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete} ) enemies[eidx] = enemy enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" end end return true end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) local hero = display.newCircle( 10, 10, 10 ) local function moveHere(event) transition.to( hero, {time = 2000, x = event.x, y = event.y} ) end Runtime:addEventListener("tap", moveHere)

I want to call the enemies inside the moveHere or vice versa so that I will be able to create collision events through setting an area related to their x and y. The result after collision the red ball turns white.

function hero.tap( self, event) transition.cancel( self ) transition.to( self, {time = 2000, x = event.x, y = event.y} ) end hero:addEventListener("tap")
local function onComplete (obj) obj:removeSelf() end local eidx= 0 local kHave = true local enemies = {} local function createEnemies(en) print(hero.x .. hero.y) -- I want to print the heroes coordinates while transition happens if kHave then for x = 1, en do eidx = eidx + 1 local enemy = display.newCircle(0, 0, 10) enemy:setFillColor(1,0,0) enemy.x = -200 enemy.y = math.random(150,(display.contentHeight)) transition.to( enemy, {time = 10000, x = display.contentWidth + 200, onComplete} ) enemies[eidx] = enemy enemies[eidx].eidx = eidx enemies[eidx].myName = "enemy" end end return true end local function updateEnemies() createEnemies(1) end timer.performWithDelay( math.random(1000, 3000), updateEnemies, -1 ) hero = display.newCircle( 10, 10, 10 ) function hero.tap( self, event) transition.cancel( self ) transition.to( self, {time = 2000, x = event.x, y = event.y} ) end hero:addEventListener("tap")

it isn’t working