【问题标题】:Remove the first second objects in Corona Lua删除 Corona Lua 中的第一个第二个对象
【发布时间】:2014-02-26 08:27:27
【问题描述】:

我想永远创建对象,在创建第 6 个对象之后,我想删除第一个创建的对象。然后在创建第 7 个对象时,我想删除第二个对象。循环是这样的。

j=0

    local  tekrarla = function () 

    local tekerdusur= {}

        j = j+1

    print (j)
    tekerdusur[j]  = display.newSprite( tekeranim, { name="tekergiris2", start=1, count=2, time=800 } )
    tekerdusur[j] .x = math.random (display.contentCenterX -400,display.contentCenterX+200) 
    tekerdusur[j] .y =  math.random (display.contentCenterY -300,display.contentCenterY +100) 
    tekerdusur[j] .bodyType = "dynamic"
    tekerdusur[j] .isBullet = true
    tekerdusur[j] :play()



   physics.addBody( tekerdusur[j] , { density=0.9, friction=0.5, bounce=0.6, radius=38 } )


if (j > 5) then
tekerdusur[j-5]:removeSelf()
tekerdusur [j-5]= nil

end
end

timer.performWithDelay(1000,tekrarla,-1)

谢谢。

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    为以前的精灵创建一个列表。在末尾添加新的。如果列表有 5 个条目,请在添加另一个之前删除最旧的(第一个)。

        local tekerdusur = {}
    
        local function tekrarla()
            local new = display.newSprite( tekeranim, { name="tekergiris2", start=1, count=2, time=800 } )
            new.x = math.random (display.contentCenterX - 400, display.contentCenterX + 200) 
            new.y = math.random (display.contentCenterY - 300, display.contentCenterY + 100) 
            new.bodyType = "dynamic"
            new.isBullet = true
            new:play()
    
            physics.addBody( new, { density=0.9, friction=0.5, bounce=0.6, radius=38 } )
    
            if #tekerdusur == 5 then
                tekerdusur[1]:removeSelf()
                table.remove(tekerdusur, 1)
            end
            table.insert(tekerdusur, new)
        end
    
        timer.performWithDelay(1000, tekrarla, -1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-17
      • 1970-01-01
      • 1970-01-01
      • 2021-01-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多