Help Creating A touch event

Hi I’m new (2 months) toCorona Sdk , I’m basically trying to convert the FatFreddy Demo to Composer with a menu etc.I have the menu screen up , but right now I’m stuck on the touch event so I can drag the player( its just a square). My attachment shows the error that pops up and also the part of the code it has a problem with. I placed the code in the function scene: create (event) section of the composer template. I tested the code out in a simple main.lua file without Composer and everything works fine, but when I plug the code into my actual game with composer it produces the error below.

Im using the Corona Simulator  2014.2393

Error Message" game.lua 100 attempt to index global player1 (a nil value)

I know that the Fatfreddy demo game is old(outdated code) and I am basically using it as a point of reference for my game, but any advice on the simplest way to transfer a game with only one lua file to a actually complete game would help.

FatFreddy: http://www.cutemachine.com/corona-sdk-tutorial/a-complete-game-made-with-corona-sdk-tutorial-part-4/

First, screen shots of code isn’t a very good way to post code to the forums.  Its much better to select your code using your mouse and do a “copy” (CTRL-C PC or CMD-C on a Mac), then in the forums type :


paste your code (CTRL-V or CMD-V)

(Please leave space after the [ out)

But in your case you have a line 127 that references player1, which doens’t exist there.  I can’t tell what line 127 is.

Rob

--Hide the status bar. display.setStatusBar( display.HiddenStatusBar)   function onCollision( event ) if ( event.phase == "began" ) then composer.gotoScene( "menu" )   end end       -- Actual Scene function scene:create( event )      local sceneGroup = self.view    --Set the background color to white    local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )          background:setFillColor( 1 )          background:setFillColor( 255, 255, 255 )            background.x = display.contentCenterX           background.y = display.contentCenterY           sceneGroup:insert( background )         --Add as score label local score = 0 local scoreLabel = display.newText(  score, 0,0, native.systemFontBold, 24) scoreLabel.x = display.viewableContentWidth / 1.5 scoreLabel.y = display.viewableContentHeight / 15 scoreLabel :setTextColor(0,0,0)   ----Create Player  local player1 = display.newRect( 0, 0, 20, 20 )             player1:setFillColor( 0 )             player1:setStrokeColor( 1, 0, 0 )        player1.x = display.viewableContentWidth / 2        player1.y = display.viewableContentHeight / 2   local function animate( event )        player1.rotation = player1.rotation + 20   end   Runtime:addEventListener( "enterFrame", animate );           end   ---Touch Event function player1:touch( event )           if event.phase == "began" then        self.markX = self.x    self.markY = self.y       elseif event.phase == "moved" then     local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x,self.y = x, y ---moves object based on cal   end         return true end     player1:addEventListener("touch", player1)                           -------------Start   function scene: show (event)    local sceneGroup = self.view    local phase = event.phase      if ( phase == "will" ) then       -- Called when the scene is still off screen (but is about to come on screen).    elseif ( phase == "did" ) then       -- Called when the scene is now on screen.       -- Insert code here to make the scene come alive.       -- Example: start timers, begin animation, play audio, etc.     end end              -- "scene:hide()" add end and this should b bottom     function scene:hide( event )      local sceneGroup = self.view    local phase = event.phase      if ( phase == "will" ) then    Runtime:removeEventListener("collision", onCollision)    Runtime:removeEventListener("collision", onCollision)             elseif ( phase == "did" ) then       -- Called immediately after scene goes off screen.    end end   -- "scene:destroy()" function scene:destroy( event )   local sceneGroup = self.view      -- Called prior to the removal of scene's view ("sceneGroup").    -- Insert code here to clean up the scene.    -- Example: remove display objects, save state, etc. end ---------------------------------------------------------------------------------   -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )               physics.setGravity( 0, 0 )   ---------------------------------------------------------------------------------   return scene        

Thanks for the reply I was wondering how people did that. 

I tried adjusting my code to this video from corona http://www.youtube.com/watch?v=3CpjU0l0wYI ,but got the same problem

What is line 127?

Hi it is line 100 I made a correction to my post same problem though -line 100 is  function player1:touch( event )

You basically have a scope problem.  It’s really hard to spot because your code isn’t indented very well.  You can’t tell where functions begin and end, where if statements begin and in, etc. 

You declare player1 local inside of scene:create(), but your touch handler is outside of the scene:create() function and at that point, player1 is unknown.

--Hide the status bar. display.setStatusBar( display.HiddenStatusBar)   function onCollision( event )     if ( event.phase == "began" ) then         composer.gotoScene( "menu" )     end end -- Actual Scene function scene:create( event )     local sceneGroup = self.view     --Set the background color to white     local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )     background:setFillColor( 1 )     background:setFillColor( 255, 255, 255 )       background.x = display.contentCenterX     background.y = display.contentCenterY     sceneGroup:insert( background )             --Add as score label     local score = 0     local scoreLabel = display.newText(  score, 0,0, native.systemFontBold, 24)     scoreLabel.x = display.viewableContentWidth / 1.5     scoreLabel.y = display.viewableContentHeight / 15     scoreLabel :setTextColor(0,0,0)       ----Create Player       local player1 = display.newRect( 0, 0, 20, 20 )     player1:setFillColor( 0 )     player1:setStrokeColor( 1, 0, 0 )     player1.x = display.viewableContentWidth / 2     player1.y = display.viewableContentHeight / 2       local function animate( event )         player1.rotation = player1.rotation + 20     end       Runtime:addEventListener( "enterFrame", animate ); end   ---Touch Event function player1:touch( event )     if event.phase == "began" then             self.markX = self.x         self.markY = self.y       elseif event.phase == "moved" then         local x = (event.x - event.xStart) + self.markX         local y = (event.y - event.yStart) + self.markY         self.x,self.y = x, y ---moves object based on cal     end     return true end   player1:addEventListener("touch", player1)   -------------Start function scene: show (event)       local sceneGroup = self.view     local phase = event.phase       if ( phase == "will" ) then         -- Called when the scene is still off screen (but is about to come on screen).     elseif ( phase == "did" ) then         -- Called when the scene is now on screen.         -- Insert code here to make the scene come alive.         -- Example: start timers, begin animation, play audio, etc.     end end    -- "scene:hide()" add end and this should b bottom     function scene:hide( event )     local sceneGroup = self.view     local phase = event.phase       if ( phase == "will" ) then         Runtime:removeEventListener("collision", onCollision)         Runtime:removeEventListener("collision", onCollision)     elseif ( phase == "did" ) then         -- Called immediately after scene goes off screen.     end end   -- "scene:destroy()" function scene:destroy( event )     local sceneGroup = self.view       -- Called prior to the removal of scene's view ("sceneGroup").     -- Insert code here to clean up the scene.     -- Example: remove display objects, save state, etc. end ---------------------------------------------------------------------------------   -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )               physics.setGravity( 0, 0 )   ---------------------------------------------------------------------------------   return scene

Sorry for the lack of indents I’ll keep that in mind for future post. I follow what your saying about the scope , but where would you suggest I put the handler for the event? The only reason I placed it there was because of the tutorial.

The simplest thing would be to move that block of code inside your scene:create(). 

Ok, so I moved the code back into the scene: create() and everything is working- Thanks!! I didn’t recognize it was outside of it until you mentioned it.

That’s why the indenting is important.  You can’t tell what’s inside other things without it.

Rob

O ok that makes complete sense , I will adjust my code right now.

First, screen shots of code isn’t a very good way to post code to the forums.  Its much better to select your code using your mouse and do a “copy” (CTRL-C PC or CMD-C on a Mac), then in the forums type :


paste your code (CTRL-V or CMD-V)

(Please leave space after the [ out)

But in your case you have a line 127 that references player1, which doens’t exist there.  I can’t tell what line 127 is.

Rob

--Hide the status bar. display.setStatusBar( display.HiddenStatusBar)   function onCollision( event ) if ( event.phase == "began" ) then composer.gotoScene( "menu" )   end end       -- Actual Scene function scene:create( event )      local sceneGroup = self.view    --Set the background color to white    local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )          background:setFillColor( 1 )          background:setFillColor( 255, 255, 255 )            background.x = display.contentCenterX           background.y = display.contentCenterY           sceneGroup:insert( background )         --Add as score label local score = 0 local scoreLabel = display.newText(  score, 0,0, native.systemFontBold, 24) scoreLabel.x = display.viewableContentWidth / 1.5 scoreLabel.y = display.viewableContentHeight / 15 scoreLabel :setTextColor(0,0,0)   ----Create Player  local player1 = display.newRect( 0, 0, 20, 20 )             player1:setFillColor( 0 )             player1:setStrokeColor( 1, 0, 0 )        player1.x = display.viewableContentWidth / 2        player1.y = display.viewableContentHeight / 2   local function animate( event )        player1.rotation = player1.rotation + 20   end   Runtime:addEventListener( "enterFrame", animate );           end   ---Touch Event function player1:touch( event )           if event.phase == "began" then        self.markX = self.x    self.markY = self.y       elseif event.phase == "moved" then     local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY self.x,self.y = x, y ---moves object based on cal   end         return true end     player1:addEventListener("touch", player1)                           -------------Start   function scene: show (event)    local sceneGroup = self.view    local phase = event.phase      if ( phase == "will" ) then       -- Called when the scene is still off screen (but is about to come on screen).    elseif ( phase == "did" ) then       -- Called when the scene is now on screen.       -- Insert code here to make the scene come alive.       -- Example: start timers, begin animation, play audio, etc.     end end              -- "scene:hide()" add end and this should b bottom     function scene:hide( event )      local sceneGroup = self.view    local phase = event.phase      if ( phase == "will" ) then    Runtime:removeEventListener("collision", onCollision)    Runtime:removeEventListener("collision", onCollision)             elseif ( phase == "did" ) then       -- Called immediately after scene goes off screen.    end end   -- "scene:destroy()" function scene:destroy( event )   local sceneGroup = self.view      -- Called prior to the removal of scene's view ("sceneGroup").    -- Insert code here to clean up the scene.    -- Example: remove display objects, save state, etc. end ---------------------------------------------------------------------------------   -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )               physics.setGravity( 0, 0 )   ---------------------------------------------------------------------------------   return scene        

Thanks for the reply I was wondering how people did that. 

I tried adjusting my code to this video from corona http://www.youtube.com/watch?v=3CpjU0l0wYI ,but got the same problem

What is line 127?

Hi it is line 100 I made a correction to my post same problem though -line 100 is  function player1:touch( event )

You basically have a scope problem.  It’s really hard to spot because your code isn’t indented very well.  You can’t tell where functions begin and end, where if statements begin and in, etc. 

You declare player1 local inside of scene:create(), but your touch handler is outside of the scene:create() function and at that point, player1 is unknown.

--Hide the status bar. display.setStatusBar( display.HiddenStatusBar)   function onCollision( event )     if ( event.phase == "began" ) then         composer.gotoScene( "menu" )     end end -- Actual Scene function scene:create( event )     local sceneGroup = self.view     --Set the background color to white     local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight )     background:setFillColor( 1 )     background:setFillColor( 255, 255, 255 )       background.x = display.contentCenterX     background.y = display.contentCenterY     sceneGroup:insert( background )             --Add as score label     local score = 0     local scoreLabel = display.newText(  score, 0,0, native.systemFontBold, 24)     scoreLabel.x = display.viewableContentWidth / 1.5     scoreLabel.y = display.viewableContentHeight / 15     scoreLabel :setTextColor(0,0,0)       ----Create Player       local player1 = display.newRect( 0, 0, 20, 20 )     player1:setFillColor( 0 )     player1:setStrokeColor( 1, 0, 0 )     player1.x = display.viewableContentWidth / 2     player1.y = display.viewableContentHeight / 2       local function animate( event )         player1.rotation = player1.rotation + 20     end       Runtime:addEventListener( "enterFrame", animate ); end   ---Touch Event function player1:touch( event )     if event.phase == "began" then             self.markX = self.x         self.markY = self.y       elseif event.phase == "moved" then         local x = (event.x - event.xStart) + self.markX         local y = (event.y - event.yStart) + self.markY         self.x,self.y = x, y ---moves object based on cal     end     return true end   player1:addEventListener("touch", player1)   -------------Start function scene: show (event)       local sceneGroup = self.view     local phase = event.phase       if ( phase == "will" ) then         -- Called when the scene is still off screen (but is about to come on screen).     elseif ( phase == "did" ) then         -- Called when the scene is now on screen.         -- Insert code here to make the scene come alive.         -- Example: start timers, begin animation, play audio, etc.     end end    -- "scene:hide()" add end and this should b bottom     function scene:hide( event )     local sceneGroup = self.view     local phase = event.phase       if ( phase == "will" ) then         Runtime:removeEventListener("collision", onCollision)         Runtime:removeEventListener("collision", onCollision)     elseif ( phase == "did" ) then         -- Called immediately after scene goes off screen.     end end   -- "scene:destroy()" function scene:destroy( event )     local sceneGroup = self.view       -- Called prior to the removal of scene's view ("sceneGroup").     -- Insert code here to clean up the scene.     -- Example: remove display objects, save state, etc. end ---------------------------------------------------------------------------------   -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene )               physics.setGravity( 0, 0 )   ---------------------------------------------------------------------------------   return scene

Sorry for the lack of indents I’ll keep that in mind for future post. I follow what your saying about the scope , but where would you suggest I put the handler for the event? The only reason I placed it there was because of the tutorial.

The simplest thing would be to move that block of code inside your scene:create().