The code is working good till, i change to a different screen then i get a runtime error, I have no idea where to put the
localGroup:insert( food )
[code]module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local physics = require(“physics”)
physics.start()
display.setStatusBar( display.HiddenStatusBar )
–
local background = display.newImageRect( “water.png”, 320, 480 )
background:setReferencePoint( display.CenterLeftReferencePoint )
background.x = 0; background.y = 240
localGroup:insert( background )
–
local fish = display.newImageRect( “fish.png”, 71, 138 )
fish:setReferencePoint( display.CenterLeftReferencePoint )
fish.x = 160; fish.y = 360
physics.addBody( fish, “kinematic” )
localGroup:insert( fish )
–
local gulp = audio.loadSound( “gulp.caf” )
local function playGulp()
audio.play(gulp);
end
fish:addEventListener ( “collision”, playGulp )
–
local removeBody = function( event )
local t = event.target
local phase = event.phase
if “began” == phase then
t:removeSelf()
end
return true
end
–
local foods = {}
local randomFood = function()
choiceFood = math.random( 100 )
local food
if ( choiceFood < 80 ) then
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=0.6, friction=0.6} )
else
food = display.newImageRect( “worm.png”, 28, 38 )
food.x = 40 + math.random( 260 ); food.y = -40
physics.addBody( food, { density=2.0, friction=0.6} )
end
foods[#foods + 1] = food
food.collision = onLocalCollision
food:addEventListener( “collision”, removeBody )
end
timer.performWithDelay( 1500, randomFood, 0 )
local function onLocalCollision( self, event )
if ( event.phase == “ended” ) then
local c = event.target
local phase = event.phase
if “ended” == phase then
c:removeSelf()
end
end
return true
end
local function onTouch( event )
local t = event.target
local phase = event.phase
if “began” == phase then
local parent = t.parent
parent:insert( t )
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({bounds= 10, 10, 200, 50}):setFocus( nil )
t.isFocus = false
end
end
return true
end
fish:addEventListener ( “touch”, onTouch )
–
local exitButton = display.newImageRect( “exitButton.png”, 32, 32 )
exitButton:setReferencePoint( display.CenterLeftReferencePoint )
exitButton.x = 285; exitButton.y = 20
exitButton.alpha = .5
localGroup:insert( exitButton )
local function pressExit (event)
if event.phase == “ended” then
director:changeScene (“menu”, “crossfade”)
end
end
exitButton:addEventListener( “touch”, pressExit )
–
local function stopPhysics (event)
if event.phase == “ended” then
physics.pause()
end
end
exitButton:addEventListener( “touch”, stopPhysics )
return localGroup
end
[/code]
Thanks [import]uid: 14967 topic_id: 9185 reply_id: 309185[/import]
[import]uid: 52491 topic_id: 9185 reply_id: 33610[/import]