2013-10-22 51 views
1

我创造了我的游戏中的云,我想在20秒后删除。问题是,当我添加代码来删除它们时,云甚至不会出现,看起来它们在创建时就被删除了。Corona SDK:延迟后删除对象

这里有两种方法我试过已经(其中没有工作):

local function removeBody(body) 

    body:removeSelf() 
end 

local function newCloud() 
    local n = cloudNumber 
    while n==cloudNumber do 
    n = math.random(1,5) 
end 
    cloudNumber=n 
    local cloud = display.newImage(imageNames[cloudNumber], screenW+30, screenH*0.2) 
    timer.performWithDelay(6000, newCloud) 
    cloud.myName="cloud" 
    physics.addBody (cloud, {isSensor=true}) 
    cloud:setLinearVelocity(-25,0) 
    cloud.gravityScale=0 
    timer.performWithDelay(20000,removeBody(cloud)) 
end 

local function newCloud() 
    local n = cloudNumber 
    while n==cloudNumber do 
    n = math.random(1,5) 
end 
    cloudNumber=n 
    local cloud = display.newImage(imageNames[cloudNumber], screenW+30, screenH*0.2) 
    timer.performWithDelay(6000, newCloud) 
    cloud.myName="cloud" 
    physics.addBody (cloud, {isSensor=true}) 
    cloud:setLinearVelocity(-25,0) 
    cloud.gravityScale=0 
    --timer.performWithDelay(20000, cloud:removeSelf()) 
end 

我该怎么办? 谢谢!

回答

3

试试下面的代码:

timer.performWithDelay(20000,function() cloud:removeSelf() end) 

相反的:

timer.performWithDelay(20000,removeBody(cloud)) 

保持编码................

+0

完美!谢谢! – dietbacon