【发布时间】:2015-10-07 19:44:02
【问题描述】:
根据下面的代码块,我有两个主要问题:
(1) 在这两个生成函数中,sceneGroup:insert() 方法返回 nil,即使我已经命名了 sceneGroup 并将生成的对象插入到一个新组中。为什么返回 nil 值?
(2) 当我因为玩家死亡而进入下一个场景时,如何销毁当前场景?我试过获取当前场景并以这种方式删除它,但它没有奏效。
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 to handle the collision on the bananas
function bananas:collision(e)
-- only perform logic when the bananas are colliding with monkey
if (e.other.class == "monkey") then
updateScore()
-- cannot remove objects during a collision, so wait a short moment for it to end
timer.performWithDelay(100, function()
-- remove the bananas
display.remove(self)
end, 1)
end
-- always return true because we have handled the collision
return true
end
-- attach a collision listener to the 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 to handle the collision on the bananas
function coconut:collision(e)
-- only perform logic when the coconut is colliding with monkey
if (e.other.class == "monkey") then
-- cannot remove objects during a collision, so wait a short moment for it to end
timer.performWithDelay(1, function()
-- remove the coconut
display.remove(self)
end, 1)
composer.gotoScene("scene_gameOver")
end
-- always return true because we have handled the collision
return true
end
-- attach a collision listener to the bananas
coconut:addEventListener("collision", coconut)
return coconut
end
local total_coconut = 15
tmr = timer.performWithDelay(2000, spawnObjects, total_coconut);
【问题讨论】:
标签: lua null coronasdk corona-storyboard