2011-12-21 89 views
5

我正在嘗試爲使用Moai SDK的遊戲創建網格。網格中的每個瓦片都應該具有填充顏色的能力。Moai網格與瓷磚顏色

所以其實我有兩個問題:

  • 什麼是建立使用摩艾
  • 我如何可以單獨填充每個瓷磚顏色

The grid

一格的最佳方式

謝謝

回答

5

什麼是建立使用摩艾

一格的最佳方式

Moai有創建網格的對象:MOAIGrid。使用框架的術語,你創建一個網格並給它一個牌組。然後將其附加到道具上,並將道具添加到圖層。 (這層也需要其安裝 到窗口的視口。)

如何可以單獨填充每個瓦片與彩色

Moai deck是一個或多個圖像的集合。如果你想要你的 瓷磚是不同的顏色,那麼你會創建一個與這些顏色的 正方形圖像的套牌。

此代碼將創建一個窗口,一個4x4網格:

-- Open the window and create a viewport 
MOAISim.openWindow("Example", 512, 512) 
viewport = MOAIViewport.new() 
viewport:setSize(512, 512) 
viewport:setScale(512, 512) 

-- Create a layer 
layer = MOAILayer2D.new() 
layer:setViewport(viewport) 
MOAISim.pushRenderPass(layer) 

-- Create a 4x4 grid of 64x64px squares 
grid = MOAIGrid.new() 
grid:initGrid(4, 4, 64) 
grid:setRow(1, 1, 1, 1, 1) 
grid:setRow(2, 1, 1, 1, 1) 
grid:setRow(3, 1, 1, 1, 1) 
grid:setRow(4, 1, 1, 1, 1) 

-- Load the image file 
deck = MOAITileDeck2D.new() 
deck:setTexture("squares.png") 
deck:setSize(2, 2) 

-- Make a prop with that grid and image set 
prop = MOAIProp2D.new() 
prop:setDeck(deck) 
prop:setGrid(grid) 
prop:setLoc(-256, -256) 

-- Add it to the layer so it will be rendered 
layer:insertProp(prop) 

之後,如果你想改變一個特定的單元格的顏色,使用 setTile方法選擇哪種單元格使用的套牌中的項目。

-- Change the color of cell 1,1 to the second item in the deck 
grid:setTile(1, 1, 2) 
+0

在v1.4p0(不確定它改變的是什麼版本)線'grid:initGrid(4,4,64)'會是'grid:initRectGrid(4,4,64,64)' – devnate 2014-01-25 19:09:52

2

編輯整個c頌。

MOAISim.openWindow ("test", 320, 480) 

viewport = MOAIViewport.new() 
viewport:setSize (320, 480) 
viewport:setScale (320, -480) 
viewport:setOffset(-1, 1) 

layer = MOAILayer2D.new() 
layer:setViewport (viewport) 
MOAISim.pushRenderPass (layer) 


function createRect(x1,y1,x2,y2, R,G,B) 
    local function onDraw() 
     MOAIGfxDevice.setPenColor(R,G,B) 
     MOAIDraw.fillRect(x1,y1,x1+x2,y1+y2) --This is the rect drawing line. 
    end 

    local gfxQuad = MOAIScriptDeck.new() 
    gfxQuad:setRect(x1,y1,x2,y2) 
    gfxQuad:setDrawCallback(onDraw) 

    local prop = MOAIProp2D.new() 
    prop:setDeck(gfxQuad) 
    layer:insertProp (prop) 
    return prop 
end 

mapmaxx = 10 
mapmaxy = 10 
map={} --array to store map 
for x = 1, mapmaxx do 
    map[x] ={} 
    for y = 1, mapmaxy do 
     map[x][y] = createRect(x*20, y*20, 10, 10, x,y,x/y) 
    end 
end` 

你應該看看Rapanui,高水平的API摩艾(在這裏我轉述這個代碼)

1

構建網格真的只是畫方塊堆在網格形成。 我不知道摩艾的api;但我希望你可以drawSquare(X,Y,寬度,高度,顏色)

所以你必須:

local width = 800 
local height = 600 
local color = { red=1, blue=1, green=1 } 
for x=1 , 100 do 
    for y=1, 100 do 
     screen:drawSquare((x-1)*width,(y-1)*height,width,height,color) 
    end 
end 
+0

也許我不清楚我的問題。我將我的Corona遊戲移植到Moai,所以邏輯就在那裏,但我找不到正確的函數來繪製一個正方形並填充一個顏色。另外我不知道我是否應該手動創建每個拼貼或者如果MOAIGrid是一個選項。 – 2011-12-24 12:10:51