Okay so i’m pretty new to this (all i’ve done is the “Getting started” guides) and so I decided to take a stab at making my own pong-game using composer, This is the scene for the actual game( so far at least):
-- Setting up physics local physics = require( "physics" ) physics.start() physics.setGravity( 0, 0 ) local ball local usplatform local sovplatform function startmatch() -- Gets match going local whoStarts = math.random( 2 ) if ( whoStarts == 1 ) then ball:applyLinearImpulse( 0, 0.75, ball.x, ball.y ) elseif ( whoStarts == 2 ) then ball:applyLinearImpulse( 0, -0.75, ball.x, ball.y ) end end -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen -- Getting Display Groups Up and Running (Background/Moving parts/UI) local backGroup = display.newGroup() sceneGroup:insert( backGroup ) local elementGroup = display.newGroup() sceneGroup:insert( elementGroup ) local uiGroup = display.newGroup() sceneGroup:insert( uiGroup ) -- Loading background local background = display.newImageRect( backGroup, "assets/background.png", 320, 620 ) background.x = display.contentCenterX background.y = display.contentCenterY local usplatform = display.newImageRect( elementGroup, "assets/usponger.png", 110, 50 ) usplatform.x = display.contentCenterX usplatform.y = display.contentCenterY+240 local sovplatform = display.newImageRect( elementGroup, "assets/sovponger.png", 110, 50 ) sovplatform.x = display.contentCenterX sovplatform.y = display.contentCenterY-240 local ball = display.newImageRect( elementGroup, "assets/ball.png", 20, 20 ) ball.x = display.contentCenterX ball.y = display.contentCenterY -- Actually making the game objects \*exist\* physics.addBody( usplatform, { friction=0.3, bounce=0.0 } ) physics.addBody( sovplatform, { friction=0.3, bounce=0.0 } ) physics.addBody( ball, { bounce=1.0 } ) physics.pause() end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen -- Getting match to start physics.start() startmatch() end end
But when I start the game I receive an error that reads:
“2pgame.lua:29: attempt to index upvalue ‘ball’ (a nil value)”
Note: On the menu scene i just display the images and added a button for this scene (i want to add another one for singleplayer later so figured i’d start from there)
Apologies if i’m missing something here, complete newbie to gamedev basically.
Thanks in advance.
