Lua删除货箱

我正在尝试制作一个简单的游戏,但是我遇到了一些问题。 我有一个游戏,在其中有一层由6个方块组成,5秒后会出现另一层由6个方块组成的图层,以此类推。 我的问题是,我想从每层中随机删除2个方块。在第一层中将会有某种可以在方块上跳跃的对象,所以我想从第一层中删除2个方块,并放置一个动物之类的对象。这是我拥有的代码(仅适用于第一层方块,但当我知道如何处理第一层时它也将适用于第二层)

    local physics = require("physics")
    physics.start()

    display.setDrawMode("hybrid")

    local sky = display.newImage("bkg_clouds.png")
    sky.x = display.contentWidth/2
    sky.y = display.contentHeight/2-60

    local ground = display.newImage("ground.png")
    ground.x = display.contentWidth/2
    ground.y = sky.y + display.contentHeight/2 + 80
    ground.rotation = 0.7
    local image_outline = graphics.newOutline( 2, "ground.png" )
    physics.addBody( ground , "static", {friction = 0.5, bounce = 0.2, outline = image_outline})

    local leftwall = display.newRect(0,0,1,display.contentHeight*2+100)
    local rightwall =           display.newRect(display.contentWidth,0,1,display.contentHeight*2+100)
    physics.addBody(leftwall, "static",{bounce=0.1})
    physics.addBody(rightwall, "static", {bounce = 0.1})

     for i = 27, display.contentWidth-20, display.contentWidth/6 do

        local crate = display.newImage("crate.png")
        crate.x = i
        crate.width = display.contentWidth/6.5
        crate.y = 50
        crate.height = display.contentWidth/6.5
        physics.addBody(crate, {density = 3.0, friction  = 0.5, bounce = 0.1})
     end
点赞
用户518500
用户518500

这可能是一个非常基本的解决方案,但是首先想到的是生成 2 个随机的指标来替换木箱。

所以当循环计数器匹配索引时,你会绘制一个动物代替创建物品。

我想的是这样的:

local physics = require("physics")
local math = require("math") -- 不确定是否需要加载。
physics.start()

display.setDrawMode("hybrid")

local sky = display.newImage("bkg_clouds.png")
sky.x = display.contentWidth/2
sky.y = display.contentHeight/2-60

local ground = display.newImage("ground.png")
ground.x = display.contentWidth/2
ground.y = sky.y + display.contentHeight/2 + 80
ground.rotation = 0.7
local image_outline = graphics.newOutline( 2, "ground.png" )
physics.addBody( ground , "static", {friction = 0.5, bounce = 0.2, outline = image_outline})

local leftwall = display.newRect(0,0,1,display.contentHeight*2+100)
local rightwall = display.newRect(display.contentWidth,0,1,display.contentHeight*2+100)
physics.addBody(leftwall, "static",{bounce=0.1})
physics.addBody(rightwall, "static", {bounce = 0.1})

-- 生成 2 个随机指标,落在循环边界内。
local index_1 = math.random(27, display.contentWidth-20)
local index_2 = math.random(27, display.contentWidth-20)

for i = 27, display.contentWidth-20, display.contentWidth/6 do
    -- 为匹配的索引绘制动物,否则绘制一个木箱。
    if (i == index_1 or i == index_w) then
        -- 绘制动物
    else
        local crate = display.newImage("crate.png")
        crate.x = i
        crate.width = display.contentWidth/6.5
        crate.y = 50
        crate.height = display.contentWidth/6.5
        physics.addBody(crate, {density = 3.0, friction  = 0.5, bounce = 0.1})
    end
end

就像我说的那样,这是一个非常基本的实现,但应该可以让你的层正常工作。

未解决的问题:

  • 你可能会有一个没有动物的图层,因为你的索引可能不在生成的步骤中。
  • 你的指标可能重叠,导致一个动物
  • 你的指标可能只在开头或结尾,从而改变/破坏你的游戏。

但是我会留给你去处理这些检查。

2015-04-17 10:39:26