2015-04-17 36 views
0

我试图做一个简单的游戏,但我有OME问题。 我有一个游戏,有一个6个层的盒子,5秒后有另一个6个盒子的层等。 我的问题是,我想从每个图层中删除2个随机框。在第一层中会有某种对象可以跳到盒子上,所以我想在第一层中移除2个盒子,并在其中放置一个对象,例如动物。这是我的代码(这仅适用于箱的第一层,但是当我知道如何做到这一点也将在第二层上工作的第一层)Lua删除板条箱

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 

回答

0

这可能是一个很基本的解决方案,但首先想到的就是生成2个随机数,用于替换箱子。 所以,当你的循环计数器与索引相匹配时,你画一个创建动物instad。

我在想是这样的:

local physics = require("physics") 
local math = require("math") -- not sure if you need to require it. 
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}) 

-- Generates 2 random indeces, falling in the loop boundary 
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 
    -- Draw animal for matching index, otherwise a crate. 
    if (i == index_1 or i == index_w) then 
     -- draw animal 
    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 

正如我所说的,真正的基本实现,但应该得到的伎俩为您层的工作。

为实施开放式问题:

  • 您可能没有动物层,作为你的索引可能不是在你的生成步骤下降。
  • 您的单位可能会重叠,导致一只动物
  • 您的索引可能只处于开始或结束状态,改变/殴打您的游戏。

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