Ive put this together its untested but it should work, I think. You have the right idea but you need to create a system for creating your levels using objects which can be reused. If you can get the level creation working well then the rest of the game will be simple.
Hope it makes sense. let me know how you get on and ill try to help some more.
[code]
–
– main.lua
– Your code here
local background = display.newImageRect( “background.png”, display.contentWidth, display.contentHeight )
background:setReferencePoint( display.TopLeftReferencePoint )
background.x, background.y = 0, 0
local ground = display.newImage( “ground.png” )
ground:setReferencePoint( display.BottomLeftReferencePoint )
ground.x, ground.y = 0, 530
ground.name = “ground”
physics.addBody( ground, “static”, { friction=1.0, density=1.0, bounce=0 } )
local statue = display.newImageRect( “statue.png”, 74, 104 )
statue.x, statue.y = 160, 300
physics.addBody( statue, { density=1.0, friction=0.3, bounce=0.3 } )
function statue:collision(event)
print(“here”)
self.alive = false
if(event.phase == “began”)then
if(event.other.name == “ground”)then
print(“Game Over”)
event.other:setFillColor(255,0,0)
EndGame()
end
end
end
statue:addEventListener(“collision”, statue)
– create a table to make each type of crate objects
local woodcrateProperties = {}
woodcrateProperties.id = “destroyable”
woodcrateProperties.height = 40
woodcrateProperties.width = 220
woodcrateProperties.image = “cement.png”
woodcrateProperties.bounce = 0.3
–add more as you require
local cementcrateProperties = {}
cementcrateProperties.id = “indestructible”
cementcrateProperties.height = 60
cementcrateProperties.width = 50
cementcrateProperties.image = “wood.png”
cementcrateProperties.bounce = 0.3
local crateObjects = {woodcrateProperties,cementcrateProperties} --1,2
– this use of table better for larger amount of data for big levels etc can do same as above if you prefer
local levelConfig = {}
levelConfig[1] = {1,2,1} – crateObject
levelConfig[2] = {260,160,60} – Crate x
levelConfig[3] = {430,380,430} – Crate y
local function createCrate(param)
local crate = display.newImageRect( param.image, param.width, param.height )
physics.addBody( crate, { density=1.0, friction=0.3, bounce=param.height } )
return crate
end
local woodenNumber = 0
–check number of crates in levelconfig index 1, which is 3 . cycle through, use “i”
for i = 1, #levelConfig[1], 1 do
local param = crateObjects[levelConfig[1][i]] – pick corresponding object
local crate = createCrate(param)
crate.x = levelConfig[2][i]
crate.y = levelConfig[3][i]
crate:addEventListener(“touch”, crate)
group:insert(crate) – insert into group
if crate.id == “destroyable” then
woodenNumber = woodenNumber + 1 – count how many wooden
end
end
local winWhen = 0 – set how many need to be left to win
function checkRemainingCrates()
woodenNumber = woodenNumber - 1
if woodenNumber == winWhen then
print(“startCountdown()”) – cool your countdown
end
end
function crate:touch(event)
if event.id == “destroyable” then
print(“Destroy”)
if (event.phase == “began”) then
timer.performWithDelay(1, function()
self:removeEventListener(“touch”,self)
self:removeSelf()
checkRemainingCrates()
end)
end
end
end
– all display objects must be inserted into group
group:insert( background )
group:insert( ground)
end
[/code] [import]uid: 118379 topic_id: 24532 reply_id: 99705[/import]