lua - Inserting spawned objects into a group and removing the scene -
i have 2 main questions based on code block below:
(1) in both spawning functions, scenegroup:insert() method returns nil though have named scenegroup , have inserted spawned objects new group. why nil value returned?
(2) when go next scene because player dies, how destroy current scene? i've tried obtaining current scene , removing way, hasn't worked.
local function spawnobjects() local bananas = display.newgroup() scenegroup:insert(bananas) bananas = display.newimagerect("object_bananas.png", 30, 20); physics.addbody(bananas, "dynamic", {bounce = 0.3, radius = 9.5}); bananas.x = math.random(0, 320); bananas.y = -40; transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310)+1 , y = 600, }); -- function handle collision on bananas function bananas:collision(e) -- perform logic when bananas colliding monkey if (e.other.class == "monkey") updatescore() -- cannot remove objects during collision, wait short moment end timer.performwithdelay(100, function() -- remove bananas display.remove(self) end, 1) end -- return true because have handled collision return true end -- attach collision listener bananas bananas:addeventlistener("collision",bananas) return bananas end local total_bananas = 15 tmr = timer.performwithdelay(2000, spawnobjects, total_bananas); local function spawnobjects() local coconut = display.newgroup() scenegroup:insert(coconut) coconut = display.newimagerect("coconut.png", 30, 35) physics.addbody(coconut, "dynamic", {bounce = 0.5, radius = 11.5}); coconut.x = math.random(0, 320); coconut.y = -40; transition.to( coconut, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, }); -- function handle collision on bananas function coconut:collision(e) -- perform logic when coconut colliding monkey if (e.other.class == "monkey") -- cannot remove objects during collision, wait short moment end timer.performwithdelay(1, function() -- remove coconut display.remove(self) end, 1) composer.gotoscene("scene_gameover") end -- return true because have handled collision return true end -- attach collision listener bananas coconut:addeventlistener("collision", coconut) return coconut end local total_coconut = 15 tmr = timer.performwithdelay(2000, spawnobjects, total_coconut);
forget scenegroup moment. scene object has display.newgroup() part of object. it's member known "view". is, "scene.view" group.
because can advanced things composer have multiple scenes in 1 group (not recommend it) scene's event functions scene:create() , such handled in object oriented manner. using colon operator (:) between scene , create, passing implicit parameter called "self" scene object triggered event. in cases self , scene same thing.
as convenience developers make local reference scene.view (self.view) inside event functions make easy use. if need access scene's view group outside of 1 of these event functions do:
scene.view:insert( displayobjectofyourchoice )
or localize own scenegroup variable:
local scenegroup = scene.view
and continue using scenegroup comfortable doing.
rob
Comments
Post a Comment