I know that was kind of vague here is the code for the whole level.
[code]
module(…, package.seeall)
–====================================================================–
– SCENE: BALLOON LEVEL
–====================================================================–
–[[
- Version: 0.1
- Made by Ninja Carnival Team @ 2011
******************
–]]
new = function ( params )
– Imports
local ui = require ( “ui” )
– Groups
local localGroup = display.newGroup()
– Variables
local levelTimer = 15
local BALLOON_W = 41
local BALLOON_H = 21
local OFFSET = 23
local W_LEN = 8
local balloons = display.newGroup()
local toRemove = {}
local gameIsActive = true
local currentLevel = 1
– start physics
local physics = require(“physics”)
physics.start()
physics.setGravity(0,0 )
–Pre-load sounds
sounds = {
pop = audio.loadSound(“audio/balloonPopFinal.wav”)
}
–setup levels 1 through 10
local levels = {}
levels[1] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,1,1,1,1,1,1,0},
{0,1,1,1,1,1,1,0},
{0,0,0,1,1,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[2] = {{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,1,0,0,1,0,0},
{0,0,0,0,0,1,0,0},
{0,0,0,0,1,0,0,0},
{0,0,0,1,0,0,0,0},
{0,0,1,0,0,0,0,0},
{0,0,1,1,1,1,0,0},}
levels[3] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,1,0,0,0,0,1,0},
{0,1,1,1,1,1,1,0},
{0,1,0,1,1,0,1,0},
{0,0,0,0,0,0,0,0},
{0,0,0,1,1,0,0,0},
{0,0,0,0,0,0,0,0},}
levels[4] = {{0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1},}
– Display Objects
local background = display.newImage(“images/Lvl_1/Level_1_BG.png”)
local gun = display.newImage(“images/Lvl_1/goldenGun3.png”)
– Player Stats
local statTextSize = 16
local score = 0
local scoreNum = display.newText(0, 0, 0, native.systemFontBold, statTextSize)
local scoreText = display.newText(“SCORE:”, 0, 0, native.systemFontBold, statTextSize)
local gameTimer = 20
local gameTimerText = display.newText(“20”, 0, 0, native.systemFontBold, statTextSize)
local statsBackground = display.newRect(0,0,_W,scoreText.height+2.5)
– BUTTONS
– FUNCTIONS
–function for collision detection
local function onCollision(self, event)
if self.name == “dart” and event.other.name == “balloon” then
score = score + 100
scoreNum.text = score
audio.play(sounds.pop)
table.insert(toRemove, event.other)
balloons.numChildren = balloons.numChildren - 1
end
if balloons.numChildren == 0 then
–destroyLevel()
currentLevel = currentLevel +1
buildLevel(currentLevel)
end
end
–rotate gun
local rotDirection = 1
local function gunRotation(event)
gun.rotation = gun.rotation + rotDirection
if gun.rotation > 90 then
rotDirection = -1
elseif gun.rotation < -80 then
rotDirection = 1
end
end
–function for firing the dart
local function fireDart(event)
local dart = display.newImage(“images/Lvl_1/dart.png”)
dart.name = “dart”
dart:setReferencePoint(display.CenterReferencePoint);
dart.x = gun.x
dart.y = gun.y
dart.rotation = gun.rotation
physics.addBody(dart, “dynamic”, {density=1.0, friction=0.5, bounce=0.0})
local speed = 1000
local velocity = {}
velocity.x = math.cos( math.rad( dart.rotation - 90 ) ) * speed
velocity.y = math.sin( math.rad( dart.rotation - 90 ) ) * speed
dart:applyForce(velocity.x, velocity.y, dart.x, dart.y)
dart.collision = onCollision;
dart:addEventListener(“collision”, dart);
end
–function for creating level
function buildLevel(level)
local len = table.maxn(level)
balloons:toFront()
for i = 1, len do
for j = 1, W_LEN do
if(level[i][j] == 1) then
local balloon = display.newImage(“images/Lvl_1/Blue_Bln_Down.png”)
balloon.name = “balloon”
balloon.x = BALLOON_W * j - OFFSET
balloon.y = BALLOON_H * i
physics.addBody(balloon, {density = .5, friction = 0, bounce = 0})
balloon.bodyType = ‘static’
balloons.insert(balloons, balloon)
end
end
end
end
– function for changing levels
–function that handles endgame details
local function endGame(event)
if gameTimer == 0 then
local gameOverText = display.newText(“Game Over!”, 0, 0, native.systemFont, 35)
gameOverText:setTextColor(100, 255, 100)
gameOverText.x = display.contentWidth / 2
gameOverText.y = display.contentHeight / 2
gameEvent = “lose”
Runtime:removeEventListener( “enterFrame”, gunRotation);
Runtime:removeEventListener(“tap”, fireDart);
Runtime:removeEventListener(“collision”, dart);
end
end
– PARAMETERS
– INITIALIZE
local initVars = function ()
– Inserts
localGroup:insert(background)
localGroup:insert(statsBackground)
localGroup:insert(scoreText)
localGroup:insert(gameTimerText)
– Positions
– score positions
scoreText.x = (scoreText.width * 0.5) + 10
scoreText.y = (scoreText.height * 0.5) + 5
scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
scoreNum.x = scoreText.width + 5
scoreNum.y = scoreText.y
– timer position
gameTimerText.x = _W * 0.5
gameTimerText.y = scoreText.y
– gun position
gun:setReferencePoint(display.centerReferencePoint);
gun.x = display.contentWidth / 2
gun.y = display.contentHeight - 30
– Colors
–background:setFillColor(255,255,255)
statsBackground:setFillColor(50,0,0)
scoreText:setTextColor(255,0,0)
scoreNum:setTextColor(255,0,0)
gameTimerText:setTextColor(255,0,0)
– Listeners
Runtime:addEventListener( “enterFrame”, gunRotation);
Runtime:addEventListener(“tap”, fireDart);
buildLevel(levels[1]);
end
– Initiate variables
initVars()
– Update
local update = function ( event )
for i = 1, #toRemove do
toRemove[i].parent:remove(toRemove[i])
toRemove[i] = nil
end
end
Runtime:addEventListener(“enterFrame”, endGame)
Runtime:addEventListener(“enterFrame”, update);
local updateTimer = function( event )
gameTimer = gameTimer - 1
if gameTimer <= 0 then gameTimer = 0
end
gameTimerText.text = gameTimer
scoreNum:setReferencePoint(display.CenterLeftReferencePoint);
scoreNum.x = scoreText.width + 5
scoreNum.y = scoreText.y
end
timer.performWithDelay(1000, updateTimer, 60)
– Clear objects
local clear = function ()
end
– MUST return a display.newGroup()
return localGroup
end
[code] [import]uid: 49863 topic_id: 15764 reply_id: 58232[/import]