attempt to index upvalue tempbody(a nil value)


– level1.lua


local composer = require( “composer” )

local scene = composer.newScene()

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()

physics.setGravity( 0, 0 )


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

function scene:create( event )

– Called when the scene’s view does not exist.

– 

– INSERT code here to initialize the scene

– e.g. add display objects to ‘sceneGroup’, add touch listeners, etc.

local sceneGroup = self.view

– create a grey rectangle as the backdrop

local background = display.newRect( 0, 0, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

background:setFillColor( .5 )

– make a crate (off-screen), position it, and rotate slightly

crate = display.newImageRect( “crate.png”, 30, 30 )

crate.x, crate.y = 100, 150

crate.rotation = 15

– add physics to the crate

physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } )

– create a grass object and add physics (with custom shape)

local grass = display.newImageRect( “grass.png”, screenW, 82 )

grass.anchorX = 0

grass.anchorY = 1

grass.x, grass.y = 0, display.contentHeight

– define a shape that’s slightly shorter than image bounds (set draw mode to “hybrid” or “debug” to see)

local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 }

physics.addBody( grass, “static”, { friction=0.3, shape=grassShape } )

– all display objects must be inserted into group

sceneGroup:insert( background )

sceneGroup:insert( grass)

sceneGroup:insert( crate )

end

function scene:show( event )

local sceneGroup = self.view

local phase = event.phase

if phase == “will” then

– Called when the scene is still off screen and is about to move on screen

elseif phase == “did” then

– Called when the scene is now on screen

– 

– INSERT code here to make the scene come alive

– e.g. start timers, begin animation, play audio, etc.

physics.start()

end

end

function createCrate(  )

– body

if(crate.x>display.contentWidth)then

print( “crate gone” )

end

end

function scene:hide( event )

local sceneGroup = self.view

local phase = event.phase

if event.phase == “will” then

– Called when the scene is on screen and is about to move off screen

– INSERT code here to pause the scene

– e.g. stop timers, stop animation, unload sounds, etc.)

physics.stop()

elseif phase == “did” then

– Called when the scene is now off screen

composer.removeScene( “level1” )

end

end

function scene:destroy( event )

– Called prior to the removal of scene’s “view” (sceneGroup)

– 

– INSERT code here to cleanup the scene

– e.g. remove display objects, remove touch listeners, save state, etc.

local sceneGroup = self.view

package.loaded[physics] = nil

physics = nil

end

local tempBody

function touchListener(event)

local phase=event.phase

if phase == “began” then

print( phase )

tempBody = display.newCircle(event.x, event.y, 30)

tempBody.isVisible=false

physics.addBody(tempBody)

tempBody.isSensor = true

tempBody.joint = physics.newJoint(“touch”, tempBody, event.x, event.y)

function tempBody.enterFrame(event)

vx, vy = tempBody:getLinearVelocity()

crate:setLinearVelocity(vx, vy)

crate.angularVelocity = tempBody.angularVelocity

end

Runtime:addEventListener(“enterFrame”, tempBody)

elseif phase == “moved” then

tempBody.joint:setTarget(event.x, event.y)

elseif phase == “cancelled” or phase == “ended” then

Runtime:removeEventListener(“enterFrame”, tempBody)

tempBody.joint:removeSelf()

display.remove(tempBody)

end

end

local function finishGame( )

– body

Runtime:removeEventListener(“touch”,touchListener)

composer.removeScene( “level1” )

composer.gotoScene( “menu” )

end

Runtime:addEventListener(“touch”, touchListener)

Runtime:addEventListener(“enterFrame”, createCrate)


– Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

timer.performWithDelay( 5000, finishGame ,1 )


return scene

Hi @vimaleee1994. Welcome to Corona’s forums.  There are a few things we need you to understand before we can help you.  First please read:

https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

In this case, you’ve got a pretty good title, but then you just dumped unreadble code and didn’t provide any additional context as to what you’re doing.

Posting code: Code needs to be formatted in the post. The easiest way to do this is to click the blue <> button in the post editor. Paste your code into the window that pops up. It will make it much easier for community members to read.

Post your exact error message : You should have a command/terminal window that pops up behind the simulator. Modern versions of Corona SDK have a new console window. Older versions (i.e. last public build) on Windows will open up a command windows. On OS X you have to run the Corona Terminal entry instead of Corona Simulator. In addition to the popup window with the error, there is generally more information, including the complete stack backtrace that provides us more context to the error. Copy/Paste that into your message.

Look for Line numbers : There should be a line number where the error occured. That should point you to where the problem is at. We need that info to help.

Thanks

Rob

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() physics.setGravity( 0, 0 ) -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view -- create a grey rectangle as the backdrop local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) -- make a crate (off-screen), position it, and rotate slightly crate = display.newImageRect( "crate.png", 30, 30 ) crate.x, crate.y = 100, 150 crate.rotation = 15 -- add physics to the crate physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } ) -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see) local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) -- all display objects must be inserted into group sceneGroup:insert( background ) sceneGroup:insert( grass) sceneGroup:insert( crate ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function createCrate( ) -- body if(crate.x\>display.contentWidth)then print( "crate gone" ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen composer.removeScene( "level1" ) end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end local tempBody function touchListener(event) local phase=event.phase if phase == "began" then print( phase ) tempBody = display.newCircle(event.x, event.y, 30) tempBody.isVisible=false physics.addBody(tempBody) tempBody.isSensor = true tempBody.joint = physics.newJoint("touch", tempBody, event.x, event.y) function tempBody.enterFrame(event) vx, vy = tempBody:getLinearVelocity() crate:setLinearVelocity(vx, vy) crate.angularVelocity = tempBody.angularVelocity end Runtime:addEventListener("enterFrame", tempBody) elseif phase == "moved" then tempBody.joint:setTarget(event.x, event.y) elseif phase == "cancelled" or phase == "ended" then Runtime:removeEventListener("enterFrame", tempBody) tempBody.joint:removeSelf() display.remove(tempBody) end end local function finishGame( ) -- body Runtime:removeEventListener("touch",touchListener) composer.removeScene( "level1" ) composer.gotoScene( "menu" ) end Runtime:addEventListener("touch", touchListener) Runtime:addEventListener("enterFrame", createCrate) --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) timer.performWithDelay( 5000, finishGame ,1 ) ----------------------------------------------------------------------------------------- return scene

I apologize for last post

My problem is I’m getting the error mentioned below inside the touchListener function block

Runtime error C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:132: attempt to index upvalue 'tempBody' (a nil value) stack traceback: C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:132: in function \<C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:113\> ?: in function \<?:221\>

what is the mistake and how can i solve it.

And also getting this error when i restart this scene

Where do you ever create “tempBody”? 

I cleared the mistake. thnx for your willing to help

Hi @vimaleee1994. Welcome to Corona’s forums.  There are a few things we need you to understand before we can help you.  First please read:

https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

In this case, you’ve got a pretty good title, but then you just dumped unreadble code and didn’t provide any additional context as to what you’re doing.

Posting code: Code needs to be formatted in the post. The easiest way to do this is to click the blue <> button in the post editor. Paste your code into the window that pops up. It will make it much easier for community members to read.

Post your exact error message : You should have a command/terminal window that pops up behind the simulator. Modern versions of Corona SDK have a new console window. Older versions (i.e. last public build) on Windows will open up a command windows. On OS X you have to run the Corona Terminal entry instead of Corona Simulator. In addition to the popup window with the error, there is generally more information, including the complete stack backtrace that provides us more context to the error. Copy/Paste that into your message.

Look for Line numbers : There should be a line number where the error occured. That should point you to where the problem is at. We need that info to help.

Thanks

Rob

----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() physics.setGravity( 0, 0 ) -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view -- create a grey rectangle as the backdrop local background = display.newRect( 0, 0, screenW, screenH ) background.anchorX = 0 background.anchorY = 0 background:setFillColor( .5 ) -- make a crate (off-screen), position it, and rotate slightly crate = display.newImageRect( "crate.png", 30, 30 ) crate.x, crate.y = 100, 150 crate.rotation = 15 -- add physics to the crate physics.addBody( crate, { density=1.0, friction=0.3, bounce=0.3 } ) -- create a grass object and add physics (with custom shape) local grass = display.newImageRect( "grass.png", screenW, 82 ) grass.anchorX = 0 grass.anchorY = 1 grass.x, grass.y = 0, display.contentHeight -- define a shape that's slightly shorter than image bounds (set draw mode to "hybrid" or "debug" to see) local grassShape = { -halfW,-34, halfW,-34, halfW,34, -halfW,34 } physics.addBody( grass, "static", { friction=0.3, shape=grassShape } ) -- all display objects must be inserted into group sceneGroup:insert( background ) sceneGroup:insert( grass) sceneGroup:insert( crate ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function createCrate( ) -- body if(crate.x\>display.contentWidth)then print( "crate gone" ) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) physics.stop() elseif phase == "did" then -- Called when the scene is now off screen composer.removeScene( "level1" ) end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view package.loaded[physics] = nil physics = nil end local tempBody function touchListener(event) local phase=event.phase if phase == "began" then print( phase ) tempBody = display.newCircle(event.x, event.y, 30) tempBody.isVisible=false physics.addBody(tempBody) tempBody.isSensor = true tempBody.joint = physics.newJoint("touch", tempBody, event.x, event.y) function tempBody.enterFrame(event) vx, vy = tempBody:getLinearVelocity() crate:setLinearVelocity(vx, vy) crate.angularVelocity = tempBody.angularVelocity end Runtime:addEventListener("enterFrame", tempBody) elseif phase == "moved" then tempBody.joint:setTarget(event.x, event.y) elseif phase == "cancelled" or phase == "ended" then Runtime:removeEventListener("enterFrame", tempBody) tempBody.joint:removeSelf() display.remove(tempBody) end end local function finishGame( ) -- body Runtime:removeEventListener("touch",touchListener) composer.removeScene( "level1" ) composer.gotoScene( "menu" ) end Runtime:addEventListener("touch", touchListener) Runtime:addEventListener("enterFrame", createCrate) --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) timer.performWithDelay( 5000, finishGame ,1 ) ----------------------------------------------------------------------------------------- return scene

I apologize for last post

My problem is I’m getting the error mentioned below inside the touchListener function block

Runtime error C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:132: attempt to index upvalue 'tempBody' (a nil value) stack traceback: C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:132: in function \<C:\Users\admin\Documents\Corona Projects\onTarget\level1.lua:113\> ?: in function \<?:221\>

what is the mistake and how can i solve it.

And also getting this error when i restart this scene

Where do you ever create “tempBody”? 

I cleared the mistake. thnx for your willing to help