Having troubles following the tutorial.

Hello, completly new to lua (well not really but i have only some basic knowledge) so i was following a tutorial about creating your first project in solar2D
so here is the code that is producing the problem :

physics.start()

physics.addBody( platform, "static" ) <-- i think the problem is here.
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } ) 

this is the error :
E:\Corona Projects\Tutorial\main.lua:12: ERROR: table expected. If this is a function call, you might have used ‘.’ instead of ‘:’
stack traceback:
[C]: in function ‘addBody’
E:\Corona Projects\Tutorial\main.lua:12: in main chunk

i tried modifying physics.addBody to physics:addBody but it gives me another error :

E:\Corona Projects\Tutorial\main.lua:12: bad argument #-2 to ‘addBody’ (Proxy expected, got nil)
stack traceback:
[C]: in function ‘addBody’
E:\Corona Projects\Tutorial\main.lua:12: in main chunk

any ideas?, thanks.

@DragonZen,

Welcome to the community.

  1. Please post a link to the Tutorial you are following so we can see what you mean. (There are a lot of tutorials and guides and I don’t remember where they all are.)

  2. I think the problem is with platform or balloon. I don’t see you making those objects before attempting to add bodies to them.

physics.addBody( object, [bodyType,] [params] ) <<= The obj here is a display object that must have been created earlier.

Can you zip your tutorial project (as it is) and attach it to this post?

  1. Just in case, If you don’t have it, here is a link to the API docs (I pin this to my browser bar): https://docs.coronalabs.com/api/

Again, welcome to the community.

well the tutorial i was following is this: https://docs.coronalabs.com/guide/programming/01/index.html scroll down till you see “Adding physics” from there i wrote the code
also i did the ‘local physics = require(“physics”)’

Did you follow all the prior steps first?

Further down the page is the complete example. I’ll copy and paste it here so you can compare it to yours:

-----------------------------------------------------------------------------------------
--
-- main.lua
--
-----------------------------------------------------------------------------------------
 
local background = display.newImageRect( "background.png", 360, 570 )
background.x = display.contentCenterX
background.y = display.contentCenterY
 
local platform = display.newImageRect( "platform.png", 300, 50 )
platform.x = display.contentCenterX
platform.y = display.contentHeight-25
 
local balloon = display.newImageRect( "balloon.png", 112, 112 )
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.8
 
local physics = require( "physics" )
physics.start()
 
physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } )
 
local function pushBalloon()
    balloon:applyLinearImpulse( 0, -0.75, balloon.x, balloon.y )
end
 
balloon:addEventListener( "tap", pushBalloon )

Please note. This won’t work unless you have the art assets platform.png, balloon.png, background.png, in the same folder and main.lua

The tutorial has a link where you can get all the source files (including completed main.lua) for this project too:
https://github.com/coronalabs/GettingStarted01/archive/master.zip

yeah thanks, i got it working i think its because of the placement of the code so at first it was like this

local tapCount = 0

local physics = require( "physics" )
physics.start()

physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } )

local bg = display.newImageRect("background.png", 360, 580)

bg.x = display.contentCenterX
bg.y = display.contentCenterY

local textScore = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40)
textScore:setFillColor(0, 0, 0)

local platform = display.newImageRect("platform.png", 300, 50)
platform.x = display.contentCenterX
platform.y = display.contentHeight-25

local balloon = display.newImageRect("balloon.png", 112, 120)
balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.85

local function pushBalloon()
    balloon:applyLinearImpulse(0, -0.80, balloon.x, balloon.y)
    tapCount = tapCount + 1
    textScore.text = tapCount
end

balloon:addEventListener( "tap", pushBalloon)

then after i did some modification it looks like this

local tapCount = 0

local bg = display.newImageRect("background.png", 360, 580)

bg.x = display.contentCenterX
bg.y = display.contentCenterY

local textScore = display.newText( tapCount, display.contentCenterX, 20, native.systemFont, 40)
textScore:setFillColor(0, 0, 0)

local platform = display.newImageRect("platform.png", 300, 50)
platform.x = display.contentCenterX
platform.y = display.contentHeight-25

local balloon = display.newImageRect("balloon.png", 112, 120)

balloon.x = display.contentCenterX
balloon.y = display.contentCenterY
balloon.alpha = 0.85

local physics = require( "physics" )
physics.start()

physics.addBody( platform, "static" )
physics.addBody( balloon, "dynamic", { radius=50, bounce=0.3 } )

local function pushBalloon()
    balloon:applyLinearImpulse(0, -0.80, balloon.x, balloon.y)
    tapCount = tapCount + 1
    textScore.text = tapCount
end

balloon:addEventListener( "tap", pushBalloon)

i didn’t really think that solar would care about the order of the code, i was wrong
again thanks for the help!.

You’re welcome.

Order does matter. As well scope and visibility (which are order related) also matter. Most folks stumble over these things, so good job on your first experience with this and working it out.

In your first code snippet, you’re trying to add the bodies before creating the display objects, so the display objects don’t exist yet when the addBody() call is made.

Tip. Please format code posts by enclosing them in triple back quote pairs. I edited your post. Please go back and edit it to see what I changed if ‘enclose code in triple back quote pairs’ wasn’t clear.

yeah am afraid i will get confused since i need keep the order of the code correctly maybe comments in code will help i think. Again i appreciate your help!.

Note: This part can be at the top of the file:

local physics = require( "physics" )
physics.start()
1 Like

Neat idea thanks.