2015-04-17 32 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 

正如我所說的,真正的基本實現,但應該得到的伎倆爲您層的工作。

爲實施開放式問題:

  • 您可能沒有動物層,作爲你的索引可能不是在你的生成步驟下降。
  • 您的單位可能會重疊,導致一隻動物
  • 您的索引可能只處於開始或結束狀態,改變/毆打您的遊戲。

但我會留給你來處理這些檢查。