Can't Get ' 'addEventListener( "enterFrame", ...' ' Working?

Hi,

Struggling to make a simple animation demo with Corona SDK and Lua.

I can’t seem to get ’ ‘addEventListener( “enterFrame”, …’ ’ working no matter what I try?

Below is code, hopefully someone can help me!

-- "main.lua" TextScale = 1 TextScaleDir = 1 local composer = require( "composer" ) composer.gotoScene( "sceneAnimatedTextTest" )

-- "sceneAnimatedTextTest.lua" local composer = require( "composer" ) local scene = composer.newScene() -- ------------------------------------------------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view TextScale = 1 TextScaleDir = 1 local myRectangle = display.newRect( display.contentCenterX, display.contentCenterY, display.contentWidth, display.contentHeight ) myRectangle.strokeWidth = 5 myRectangle:setFillColor( 0.5 ) myRectangle:setStrokeColor( 0, 1, 0 ) sceneGroup:insert( myRectangle ) local displayText = display.newText( "Hello World", display.contentCenterX, display.contentCenterY, "Font01.ttf", 20 ) displayText:setFillColor( 0, 1, 0 ) sceneGroup:insert( displayText ) end -- ------------------------------------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then local function onEveryFrame( event ) if TextScaleDir == 1 then if TextScale \<= 5 then TextScale = TextScale + .1 else TextScaleDir = 0 end elseif TextScaleDir == 0 then if TextScale \>= 0 then TextScale = TextScale - .1 else TextScaleDir = 1 end end sceneGroup.displayText:scale( TextScale, TextScale ) -- ERROR: Attempt to index "displayText" but is nil value? end Runtime:addEventListener( "enterFrame", onEveryFrame ) end end -- ------------------------------------------------------------------------------------------------------------- function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then end end -- ------------------------------------------------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end -- ------------------------------------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------------------------------------- return scene

There is an error with the following line:

sceneGroup.displayText:scale( TextScale, TextScale ) -- ERROR: Attempt to index "displayText" but is nil value?

Thanks in advance!

JeZxLee

You have scope issues. Try reading this:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Basically you need to predefine the displayText variable outside of the scene:create function if you want to access it in the scene:show function.

Further to that, doing sceneGroup:insert(something) does not result in sceneGroup.something being a reference to that object.

You have scope issues. Try reading this:

https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Basically you need to predefine the displayText variable outside of the scene:create function if you want to access it in the scene:show function.

Further to that, doing sceneGroup:insert(something) does not result in sceneGroup.something being a reference to that object.