Using Box2d physics, I create several different images, such as bricks, and walls, and ice. Here is a sample of the code:
function brick(x, y, visible)
--create brick bitmap object from brick graphic
local brick = Bitmap.new(Texture.new("./brick.jpg"))
--reference center of the brick for positioning
brick:setAnchorPoint(0.5,0.5)
brick:setVisible(visible)
brick:setPosition(x,y)
--create box2d physical object
local body = world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(brick:getX(), brick:getY())
body:setAngle(brick:getRotation() * math.pi/180)
local poly = b2.PolygonShape.new()
poly:setAsBox(brick:getWidth()/2, brick:getHeight()/2)
local fixture = body:createFixture{shape = poly, density = 1.5,
friction = 0.8, restitution = 0.5} --increased density, friction, less restitution
brick.body = body
brick.body.type = "brick"
--add to scene
stage:addChild(brick)
end
function ice(x, y, scale, xScale, yScale, strength)
--create brick bitmap object from brick graphic
local ice = Bitmap.new(Texture.new("./ice.png"))
--reference center of the brick for positioning
ice:setScale(scale)
ice:setScaleX(xScale)
ice:setScaleY(yScale)
ice:setAnchorPoint(0.5,0.5)
ice:setPosition(x,y)
--create box2d physical object
local body = world:createBody{type = b2.DYNAMIC_BODY}
body:setPosition(ice:getX(), ice:getY())
body:setAngle(ice:getRotation() * math.pi/180)
local poly = b2.PolygonShape.new()
poly:setAsBox(ice:getWidth()/2, ice:getHeight()/2)
local fixture = body:createFixture{shape = poly, density = 0.5,
friction = 0.01, restitution = 0.3} --increased density, friction, less restitution
ice.body = body
ice.body.type = "ice"
strength = 100
ice.body.strength = strength
ice.body.bitmap = ice
--add to scene
stage:addChild(ice)
end
wall(260, 215, 5, 200, true, 90)
brick(180, screenH - 20, true)
ice(180, screenH - 60, .4,0.4,.35)
brick(280, screenH - 20, true)
ice(280, screenH - 60, .4,.4,.35)
ice(185,200, .4, 0.45, .35, 100)
ice(295,200, .4, 0.45, .35, 100) -- set xScale = 0
ice(240, 150, .3, 0.8, .35, 100)
ice(425, screenH - 105, .3, 0.3, 1.5, 100)
That sets up a "scenario" where you use a ball to hit the objects. I want to have it so that when you score 60 points, a new level starts. Do do this, the ice, bricks, wells ect would need to be deleted so that I can create new walls and bricks to create a new situation. Any ideas?
Thanks.
Comments
If box2d world you created are local to the scene, all will be garbage collected eventually (well you can also call collectgarbage on changing scene, but I think scenemanager does it for you too)