1

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

以下是我已经尝试过的两种方法(都没有奏效):

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

我应该怎么办?谢谢!

4

1 回答 1

3

试试下面的代码:

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

代替 :

timer.performWithDelay(20000,removeBody(cloud))

继续编码......

于 2013-10-23T09:20:10.250 回答