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 )
when I do put it where I think it should go, I get
Copyright © 2009-2010 A n s c a , I n c .
Version: 2.0.0
Build: 2011.484
The file sandbox for this project is located at the following folder:
(/Users/Matt/Library/Application Support/Corona Simulator/TEST-3291734D9FB81066F5DA7CC6B505E232)
Runtime error
/Users/Matt/Desktop/TEST/game.lua:72: attempt to call method ‘insert’ (a nil value)
stack traceback:
[C]: in function ‘insert’
/Users/Matt/Desktop/TEST/game.lua:72: in function ‘_listener’
?: in function <?:441>
?: in function <?:214>
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 )
localGroup:insert( food )
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
Thanks
[import]uid: 14967 topic_id: 9209 reply_id: 309209[/import]