[code]
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local physics = require(“physics”)
physics.start()
–
local stage = display.getCurrentStage( )
stage:setReferencePoint(display.CenterReferencePoint)
–
local background = display.newImageRect( “images/bg-blur-2.png”, 1024, 768 )
background:setReferencePoint( display.CenterReferencePoint )
background.x = 512; background.y = 384
localGroup:insert( background )
–
local fishCollisionFilter = { categoryBits = 1, maskBits = 30 }
local fishBodyElement = { friction = .5, filter=fishCollisionFilter}
–
local fish = display.newImageRect( “images/fish.png”, 133, 271 )
fish:setReferencePoint( display.CenterReferencePoint )
fish.x = 512; fish.y = 630
physics.addBody( fish, “dynamic”, fishBodyElement )
fish.isFixedRotation = true
localGroup:insert( fish )
–
local leftCollisionFilter = { categoryBits = 8, maskBits = 1 }
local leftBodyElement = { density = 3, friction = 10, bounce = .5, filter=leftCollisionFilter}
–
local sideLeft = display.newRect( 0, 0, 1, 768);
sideLeft:setReferencePoint(display.CenterReferencePoint);
physics.addBody( sideLeft, “static”, leftBodyElement );
sideLeft.alpha = 0
localGroup:insert(sideLeft)
–
local rightCollisionFilter = { categoryBits = 16, maskBits = 1 }
local rightBodyElement = { density = 3, friction = 10, bounce = .5, filter=rightCollisionFilter}
–
local sideRight = display.newRect( 1023, 0, 1, 768);
sideRight:setReferencePoint(display.CenterReferencePoint);
physics.addBody( sideRight, “static”, rightBodyElement );
sideRight.alpha = 0
localGroup:insert(sideRight)
–
local bottomCollisionFilter = { categoryBits = 4, maskBits = 1 }
local bottomBodyElement = { friction = .5, filter=bottomCollisionFilter }
–
local bottom = display.newRect( 0, 767, 1024, 1 );
bottom:setReferencePoint(display.CenterReferencePoint);
physics.addBody( bottom, “static”, bottomBodyElement );
bottom.alpha = 0
localGroup:insert(bottom)
–
local gameText = display.newText( “Gobble Up All The Worms”, 10, 5, native.systemFontBold, 28 )
gameText:setTextColor( 0, 240, 255, 255 )
localGroup:insert(gameText)
–
local gulp = audio.loadSound( “sounds/gulp.caf” )
local function playGulp()
audio.play( gulp, { channel=1 } );
end
–
local removeBody = function( event )
local t = event.target
local phase = event.phase
if “began” == phase then
t:removeSelf()
end
return true
end
–
local foodCollisionFilter = { categoryBits = 2, maskBits = 1 }
local foodBodyElement = { density = 0.6, friction = 0.6, filter=foodCollisionFilter}
–
local foods = {}
local randomFood = function()
choiceFood = math.random( 100 )
local food
if ( choiceFood < 80 ) then
food = display.newImageRect( “images/worm.png”, 78, 62 )
food.x = 40 + math.random( 1000 ); food.y = -40
physics.addBody( food, foodBodyElement )
else
food = display.newImageRect( “images/worm.png”, 78, 62 )
food.x = 40 + math.random( 700 ); food.y = -40
physics.addBody( food, foodBodyElement )
end
foods[#foods + 1] = food
localGroup:insert(food)
food:addEventListener ( “collision”, playGulp )
food:addEventListener( “collision”, removeBody )
end
local tTimer = timer.performWithDelay( 1500, randomFood, 0 )
–
local function removeOffscreenItems()
for i = 1, #foods do
local oneWorm = foods[i]
if (oneWorm.x) then
if oneWorm.x < -100 or oneWorm.x > display.contentWidth + 100 or oneWorm.y < -100 or oneWorm.y > display.contentHeight + 100 then
oneWorm:removeSelf()
end
end
end
end
Runtime:addEventListener( “enterFrame”, removeOffscreenItems )
–
local function onTouch( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
t.x0 = event.x - t.x
elseif t.isFocus then
if “moved” == phase then
t.x = event.x - t.x0
elseif “ended” == phase or “cancelled” == phase then
display.getCurrentStage():setFocus( nil )
t.isFocus = false
end
end
return true
end
fish:addEventListener ( “touch”, onTouch )
–
local exitButton = display.newImageRect( “images/exitButton.png”, 64, 64 )
exitButton:setReferencePoint( display.CenterLeftReferencePoint )
exitButton.x = 955; exitButton.y = 40
exitButton.alpha = .5
localGroup:insert( exitButton )
local function pressExit (event)
if event.phase == “ended” then
director:changeScene (“cFish”, “crossfade”)
end
end
exitButton:addEventListener( “touch”, pressExit )
–
local function stopPhysics (event)
if event.phase == “ended” then
physics.pause()
end
end
exitButton:addEventListener( “touch”, stopPhysics )
–
local exitSound = media.newEventSound( “sounds/delicious.caf” )
local function playExit()
media.playEventSound( exitSound );
end
exitButton:addEventListener( “tap”, playExit )
–
clean1 = function ()
if gulp then
audio.stop( 1 )
audio.dispose( 1 )
gulp = nil
end
end
exitButton:addEventListener( “touch”, clean1 )
–
clean2 = function ()
if tTimer then
timer.cancel(tTimer)
tTimer = nil
end
end
exitButton:addEventListener( “touch”, clean2 )
return localGroup
end
[/code] [import]uid: 14967 topic_id: 12317 reply_id: 44840[/import]