I’ve been working on my first ever game and made a Pong type game that breaks bricks also. I’ve been reading up and learning a lot and want to progress this game. Now the one I have now I could just edit it and make it work how I want but I need to repeat again and again for me to understand and I am changing quite a few things. Now my current code for my blocks I have 3 rows of 6 blocks. I am wondering if there is a better way to make these blocks and destroy them when collided with than I have now. Here is my set up of creating and destroying my top level of blocks.
[lua]---------- Level 1
block1 = display.newRect( 10, 35, 40, 12)
block1:setFillColor(255,25,20)
physics.addBody (block1, “static”, {})
block2 = display.newRect( 60, 35, 40, 12)
block2:setFillColor(255,25,20)
physics.addBody (block2, “static”, {})
block3 = display.newRect( 110, 35, 40, 12)
block3:setFillColor(255,25,20)
physics.addBody (block3, “static”, {})
block4 = display.newRect( 160, 35, 40, 12)
block4:setFillColor(255,25,20)
physics.addBody (block4, “static”, {})
block5 = display.newRect( 210, 35, 40, 12)
block5:setFillColor(255,25,20)
physics.addBody (block5, “static”, {})
block6 = display.newRect( 260, 35, 40, 12)
block6:setFillColor(255,25,20)
physics.addBody (block6, “static”, {})
And now destroy…
------------- Level 1
ball.class = “ball”
block1.class = “b1”
function block1:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block1:removeSelf()
end
–return true
end
block1:addEventListener(“collision”)
ball.class = “ball”
block2.class = “b2”
function block2:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block2:removeSelf()
end
–return true
end
block2:addEventListener(“collision”)
ball.class = “ball”
block3.class = “b3”
function block3:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block3:removeSelf()
end
–return true
end
block3:addEventListener(“collision”)
ball.class = “ball”
block4.class = “b4”
function block4:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block4:removeSelf()
end
–return true
end
block4:addEventListener(“collision”)
ball.class = “ball”
block5.class = “b5”
function block5:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block5:removeSelf()
end
–return true
end
block5:addEventListener(“collision”)
ball.class = “ball”
block6.class = “b6”
function block6:collision(e)
if (e.phase == “began”) then
ydirection = ydirection * -1
cScore.text = cScore.text +3
block6:removeSelf()
end
–return true
end
block6:addEventListener(“collision”)
[import]uid: 20272 topic_id: 34795 reply_id: 334795[/import]