Hi there,
I know the ‘pong’ type of game has been done to death but I am just starting out with Corona/Lua I thought its an easy game to get started with? I’ve managed to code most of the game from other tutorials but I am stuck on two things:
1: My onPostCollision function throws the following error:
main.lua:94: attempt to index local ‘e’ (a nil value)
But the game runs and scores are updated
2: I want the ‘topPaddle’ to be controlled by the game but I don’t know how-to code the AI for this
Here’s my code:
[code]
H = display.contentHeight
W = display.contentWidth
display.setStatusBar(display.HiddenStatusBar)
require(“physics”)
physics.start()
physics.setGravity(0, 0)
score1text = display.newText(“0”, W/4, H/2 - 20, “Arial”, 40)
score1text:setTextColor(0,200,0)
score2text = display.newText(“0”, W/4*3, H/2 - 20, “Arial”, 40)
score2text:setTextColor(0,200,0)
local score1 = 0
local score2 = 0
function main()
createPaddles()
createBall()
createWalls()
startGame()
onPostCollision()
end
function createPaddles()
local bottomPaddleWidth = 120
local bottomPaddleHeight = 20
local topPaddleWidth = 120
local topPaddleHeight = 20
local topPaddle = display.newRect(display.contentWidth /2 - topPaddleWidth /2, display.contentHeight - 995, topPaddleWidth, topPaddleHeight)
physics.addBody(topPaddle, “static”, {bounce = 1, friction})
local moveTopPaddle = function(event)
topPaddle.x = event.x
end
local bottomPaddle = display.newRect(display.contentWidth /2 - bottomPaddleWidth /2, display.contentHeight - 50, bottomPaddleWidth, bottomPaddleHeight)
physics.addBody(bottomPaddle, “static”, {bounce = 1, friction =1})
local moveBottomPaddle = function(event)
bottomPaddle.x = event.x
end
Runtime:addEventListener(“touch”, moveBottomPaddle)
Runtime:addEventListener(“touch”, moveTopPaddle)
end
function createBall()
local ballRadius = 15
ball = display.newCircle(display.contentWidth /2, display.contentHeight / 2, ballRadius )
physics.addBody(ball, “dynamic”, {friction = 0, bounce = 1.3, radius=ballRadius})
ball.collision = function(self,event)
if(event.phase == “ended”) then
if(event.other.type == “destructible”) then
event.other:removeSelf( )
self = nil
end
if(event.other.type == “bottomWall” or (event.other.type == “topWall”)) then
self:removeSelf( )
self = nil
local onTimerComplete = function(event)
createBall( )
startGame()
end
timer.performWithDelay( 500, onTimerComplete, 1 )
end
end
end
ball:addEventListener( “collision”, ball)
ball:addEventListener(“postCollision”, onPostCollision)
end
function onPostCollision(e)
if (e.other.type == “topWall”) then
score2 = score2 + 1
score2text.text = score2
elseif (e.other.type == “bottomWall”) then
score1 = score1 + 1
score1text.text = score1
end
end
function startGame()
ball:setLinearVelocity( 75, 150 )
end
function createWalls()
local wallThickness = 10
– left wall
local leftWall = display.newRect(0,0, wallThickness, display.contentHeight)
physics.addBody(leftWall,“static”, {friction = 0, bounce = 1} )
– top wall
topWall = display.newRect(0,0, display.contentWidth, wallThickness)
physics.addBody(topWall,“static”, {friction = 0, bounce = 0})
– right wall
local rightWall = display.newRect(display.contentWidth - wallThickness, 0, wallThickness, display.contentHeight )
physics.addBody(rightWall, “static”, {friction = 0 , bounce = 1})
– bottom wall
bottomWall = display.newRect(0, display.contentHeight - wallThickness, display.contentWidth, wallThickness)
physics.addBody(bottomWall, “static”, {friction = 0, bounce = 0})
bottomWall.type = “bottomWall”
topWall.type = “topWall”
end
main()
Any help will be greatly appreciated.
[import]uid: 167310 topic_id: 30029 reply_id: 330029[/import]