我在游戏中创建了云,我想在20秒后删除。问题是,当我添加代码以删除它们时,甚至没有出现云,似乎它们在创建时就被删除了。

这是我已经尝试过的2种方法(没有一种有效):

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

我该怎么办?
谢谢!

最佳答案

尝试以下代码:

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

而不是:
timer.performWithDelay(20000,removeBody(cloud))

继续编码.......😃

09-28 06:40