Can't Get Object by Tag or ID (Demo won't work)

Hey guys I am new to Corona and have been using the Composer GUI to design scenes, but when I try to access the objects in my code though the getObjectByTag method I get a:

Attempt to index global ‘self’ (a nil value)

Runtime error. The error occurs on the line that I call “self:getObjectByTag( “background” )”

I even copied and pasted the code in the Corona Composer Demo (Aliens v. Mutant Mice) and I still got this error. I must have something set up incorrectly. Help would be greatly appreciated!

Thanks :slight_smile:

Try getObjectByName. This was changed recently-sh. See the release notes: https://docs.coronalabs.com/composer/release-notes.html

Additionally, I’ve updated the Aliens vs. Mutant Mice sample to fix it. Thanks for letting us know, and hope that helps.

“self:getObjectByTag” has been changed to “self:getObjectByName”

I tried changing it to self:getObjectByName however I keep getting the same runtime error. I think the problem has to do with the “self” object is not defined as the scene/view for some reason. I have developed a work around, but it would still be great if I could get some help on this.

Hm. That does sound odd. Can you post your code (using formatting)? Someone should be available to help you.

Do you get this problem with Aliens vs Mutant Mice even with getObjectByName?

Here is my code with the correction “getObjectByName”

-------------------------------------------------------------------------------- -- Build Camera -------------------------------------------------------------------------------- local perspective = require( "perspective" ) -- Code Exchange library to manage camera view local camera = perspective.createView(3) -------------------------------------------------------------------------------- local target = display.newImage( "640x1136/target.png" ) target:scale( 0.25, 0.25 ) -- Shoot the object using a visible force vector function shootObject( event ) local t = event.target local phase = event.phase local startRotation --forward declaration if ( phase == "began" ) then display.getCurrentStage():setFocus( t ) t.isFocus = true target.x = t.x target.y = t.y startRotation = function() target.rotation = target.rotation + 4 end Runtime:addEventListener( "enterFrame", startRotation ) local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } ) myLine = nil elseif ( t.isFocus ) then if ( phase == "moved" ) then if ( myLine ) then myLine.parent:remove( myLine ) --erase previous line, if any end myLine = display.newLine( t.x,t.y, event.x,event.y ) myLine:setStrokeColor( 1, 1, 1, 50/255 ) myLine.strokeWidth = 15 elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) t.isFocus = false local stopRotation = function() Runtime:removeEventListener( "enterFrame", startRotation ) end local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } ) if ( myLine ) then myLine.parent:remove( myLine ) end local plasma = display.newImage( "640x1136/AlienHead.png" ) plasma.width = 80 plasma.height = 80 plasma.x = t.x plasma.y = t.y physics.addBody( plasma, { density=3, friction=1.0, bounce=.2, radius=40 } ) plasma:applyForce( 70\*(t.x - event.x), 70\*(t.y - event.y), t.x, t.y ) camera:add( plasma, 1 ) camera.damping = 10 --more fluid tracking local function trackPlasma( event ) if ( plasma.x \> 600 ) then camera:setFocus( plasma ) end end Runtime:addEventListener( 'enterFrame', trackPlasma ) end end return true --stop further propagation of touch event end local background = self:getObjectByName( "background" ) local gameGroup = self:getObjectByName( "gameGroup" ) local ufo = self:getObjectByName( "ufo" ) ufo:addEventListener( "touch", shootObject ) ------------------------------------------------------------ -- Camera code camera:setParallax( 1, 0.6 ) camera:add( background, 2 ) camera:add( gameGroup, 1 ) camera:setBounds( 0, 1480, 320, 320 ) --used to keep our view from going outside the background -- print( camera:layerCount() ) ------------------------------------------------------------

This doesn’t seem to be a valid composer scene. Are you using a scene lua file? Is that the complete file?

It is a scene lua file, but it has a .ccscene file as well. It is the complete file

That is not a correct composer scene file. Look at scene1.lua in Aliens vs. Mutant Mice, which is a lua module.

It may be worth looking at the tutorial here: https://docs.coronalabs.com/composer/tutorial.html (just note the change for getObjectByName).

Try getObjectByName. This was changed recently-sh. See the release notes: https://docs.coronalabs.com/composer/release-notes.html

Additionally, I’ve updated the Aliens vs. Mutant Mice sample to fix it. Thanks for letting us know, and hope that helps.

“self:getObjectByTag” has been changed to “self:getObjectByName”

I tried changing it to self:getObjectByName however I keep getting the same runtime error. I think the problem has to do with the “self” object is not defined as the scene/view for some reason. I have developed a work around, but it would still be great if I could get some help on this.

Hm. That does sound odd. Can you post your code (using formatting)? Someone should be available to help you.

Do you get this problem with Aliens vs Mutant Mice even with getObjectByName?

Here is my code with the correction “getObjectByName”

-------------------------------------------------------------------------------- -- Build Camera -------------------------------------------------------------------------------- local perspective = require( "perspective" ) -- Code Exchange library to manage camera view local camera = perspective.createView(3) -------------------------------------------------------------------------------- local target = display.newImage( "640x1136/target.png" ) target:scale( 0.25, 0.25 ) -- Shoot the object using a visible force vector function shootObject( event ) local t = event.target local phase = event.phase local startRotation --forward declaration if ( phase == "began" ) then display.getCurrentStage():setFocus( t ) t.isFocus = true target.x = t.x target.y = t.y startRotation = function() target.rotation = target.rotation + 4 end Runtime:addEventListener( "enterFrame", startRotation ) local showTarget = transition.to( target, { alpha=0.4, xScale=0.4, yScale=0.4, time=200 } ) myLine = nil elseif ( t.isFocus ) then if ( phase == "moved" ) then if ( myLine ) then myLine.parent:remove( myLine ) --erase previous line, if any end myLine = display.newLine( t.x,t.y, event.x,event.y ) myLine:setStrokeColor( 1, 1, 1, 50/255 ) myLine.strokeWidth = 15 elseif ( phase == "ended" or phase == "cancelled" ) then display.getCurrentStage():setFocus( nil ) t.isFocus = false local stopRotation = function() Runtime:removeEventListener( "enterFrame", startRotation ) end local hideTarget = transition.to( target, { alpha=0, xScale=1.0, yScale=1.0, time=200, onComplete=stopRotation } ) if ( myLine ) then myLine.parent:remove( myLine ) end local plasma = display.newImage( "640x1136/AlienHead.png" ) plasma.width = 80 plasma.height = 80 plasma.x = t.x plasma.y = t.y physics.addBody( plasma, { density=3, friction=1.0, bounce=.2, radius=40 } ) plasma:applyForce( 70\*(t.x - event.x), 70\*(t.y - event.y), t.x, t.y ) camera:add( plasma, 1 ) camera.damping = 10 --more fluid tracking local function trackPlasma( event ) if ( plasma.x \> 600 ) then camera:setFocus( plasma ) end end Runtime:addEventListener( 'enterFrame', trackPlasma ) end end return true --stop further propagation of touch event end local background = self:getObjectByName( "background" ) local gameGroup = self:getObjectByName( "gameGroup" ) local ufo = self:getObjectByName( "ufo" ) ufo:addEventListener( "touch", shootObject ) ------------------------------------------------------------ -- Camera code camera:setParallax( 1, 0.6 ) camera:add( background, 2 ) camera:add( gameGroup, 1 ) camera:setBounds( 0, 1480, 320, 320 ) --used to keep our view from going outside the background -- print( camera:layerCount() ) ------------------------------------------------------------

This doesn’t seem to be a valid composer scene. Are you using a scene lua file? Is that the complete file?

It is a scene lua file, but it has a .ccscene file as well. It is the complete file

That is not a correct composer scene file. Look at scene1.lua in Aliens vs. Mutant Mice, which is a lua module.

It may be worth looking at the tutorial here: https://docs.coronalabs.com/composer/tutorial.html (just note the change for getObjectByName).