Hey, I just have a quick question.
I am creating a physics oriented game, where using a ball you hit blocks of ice. The ice starts with 100 hitpoints, and after every hit, it loses 10 "health". I want the ice to "crack" after it has been hit 5 times, or brought down to 50 hitpoints by using a second image, a "cracked" image. here it a sample of my code so far.
-- Creating the ice function
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
--add to scene
stage:addChild(ice)
end
-- loading in the ice blocks
ice(185,200, .4, 0.45, .35, 100)
ice(295,200, .4, 0.45, .35, 100)
ice(240, 150, .3, 0.8, .35, 100)
ice(425, screenH - 105, .3, 0.3, 1.5, 100)
-- the hit function
local function onBeginContact(event)
-- you can get the fixtures and bodies in this contact like:
local fixtureA = event.fixtureA
local fixtureB = event.fixtureB
local bodyA = fixtureA:getBody()
local bodyB = fixtureB:getBody()
if bodyA.type == "ice" and bodyB.type == "ball" then
print("crack")
bodyA.strength = bodyA.strength - 10
print(bodyA.strength)
if bodyA.strength == 50 then
print("crack image")
-- Replace image with "cracked" version of image
end
end
end
the program works to the point where it prints "crack image" when the health of the ice is at 50. I've tried a few different methods of removing the old ice and adding the cracked ice, but I keep getting strange error codes. Im a little bit new to Box2d so im not sure what the rules are for replacing images and what not.
Thanks for the help,
iWatts.
Comments
ice.body.bitmap = ice