Hi everyone,
I’m contacting you cause I’ve got a code issue on “breackout”. I’ve inserted a code that allows the bricks to be indestructible and the code was working correctly on a single brick. After that I’ve inserted a table in the “cicle for” to create more bricks. When I used the table the code stopped working. An error doesn’t appear but the ball slides around the bricks. Where have I gone wrong? I will also post the code for you.
–table
local levels = {}
levels[2] = {{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,1,0,1,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0,0},
{0,0,0,0,0,1,0,0,0,0,0,0},
{1,0,1,0,0,1,0,1,0,0,1,0},}
– change level
function changeLevel()
– Pulizia del gruppo mattoni
mattoni:removeSelf()
mattoni.numChildren = 0
mattoni = display.newGroup()
if vy > 0 then vy = -vy
end
vx = vx + 0.5
vy = vy - 0.5
vLevel = vy
alertBox:removeEventListener(“tap”, changeLevel)
alertDisplayGroup:removeSelf()
alertDisplayGroup = nil
paddle.x = 240; paddle.y = 300
ball.x = 240; ball.y = 275
currentLevel = currentLevel + 1
levelNum = display.newText(currentLevel, 460, 8, “Arial”, 14)
levelNum:setFillColor(255, 255, 255, 255)
if vite == 3 then
lifeNum:removeSelf()
lifeNum = nil
lifeNum = display.newText(vite, 460, 25, “Arial”, 14)
lifeNum:setFillColor(255, 255, 255, 255)
end
gameLevels((levels[2]))
end
–Game Listener
function gameListeners(event)
if event == “add” then
Runtime:addEventListener(“accelerometer”, movePaddle)
– Utilizzo del trascinamento, atttivo solo nel simulatore
paddle:addEventListener(“touch”, dragPaddle)
– Gesione della palla
Runtime:addEventListener(“enterFrame”, updateBall)
paddle:addEventListener(“collision”, bounce)
ball:addEventListener(“collision”, removeBrick)
elseif event == “remove” then
Runtime:removeEventListener(“accelerometer”, movePaddle)
– Utilizzo del trascinamento, atttivo solo nel simulatore
paddle:removeEventListener(“touch”, dragPaddle)
– Gesione della palla
Runtime:removeEventListener(“enterFrame”, updateBall)
paddle:removeEventListener(“collision”, bounce)
ball:removeEventListener(“collision”, removeBrick)
end
end
–LEVEL
function gameLevels(level)
mattoni1:toFront()
local len = table.maxn(level)
for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local mattoneRosso = display.newImage(‘mattone02.png’)
mattoneRosso.name = ‘mattoneRosso’
mattoneRosso.x = BRICK_W * j - OFFSET
mattoneRosso.y = BRICK_H * i
physics.addBody(mattoneRosso, {density = 1, friction = 0, bounce = 0})
mattoneRosso.bodyType = ‘static’
mattoni1:insert( mattoneRosso )
end
end
end
background:addEventListener(“tap”, startGame)
end
function updateBall()
ball.x = ball.x + vx
ball.y = ball.y + vy
–CODE FOR MATTONEROSSO
if (ball.y + ball.height*.5 >= mattoneRosso.y - mattoneRosso.contentHeight*.5 and ball.y - ball.height*.5 <= mattoneRosso.y + mattoneRosso.contentHeight*.5) then
if (ball.x+ball.width*.5 >= mattoneRosso.x - mattoneRosso.width/2 and ball.x - ball.width*.5 <= mattoneRosso.x + mattoneRosso.width/2) then
vy = -vy
end
end
if ball.x < 0 or ball.x + ball.width > display.contentWidth then
vx = -vx
end – Collisione con le pareti laterali
if ball.y < 0 then
vy = -vy
end
if ball.y + ball.height > paddle.y + paddle.height then
gameListeners(“remove”)
ball.x = 240; ball.y = 275 – reset della palla
paddle.x = 240; paddle.y = 300 – reset del paddle
vy = -vy – reset direzione
vite = vite - 1
if vite < 0 then resetGame()
else
lifeNum:removeSelf()
lifeNum = nil
lifeNum = display.newText(vite, 460, 25, “Arial”, 14)
lifeNum:setFillColor(255, 255, 255, 255)
background:addEventListener(“tap”, startGame)
end
end
end