我正在尝试使用Moai为游戏创建一个网格。网格中的每个瓷砖都应该有填充颜色的能力。
所以实际上我有两个问题:

谢谢
发布于 2012-04-19 03:10:38
使用Moai构建网格的最佳方法是什么?
Moai有一个创建网格的对象:MOAIGrid。使用框架的行话,您可以创建一个网格并为其提供一个平台。然后将其附加到一个道具上,并将其添加到一个层中。(该层还需要一个与窗口相连的视口。)
如何用颜色分别填充每个瓷砖?
莫伊甲板是图像或图像集合。如果您希望您的瓷砖是不同的颜色,那么您将创建一个甲板上的图像广场在这些颜色。
示例
此代码将在窗口中创建一个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)发布于 2011-12-22 03:38:02
为整个代码编辑。
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`您应该查看拉帕尼,这是一个用于Moai的高级API (我在这里解释了这段代码)。
发布于 2011-12-23 02:50:16
构建网格实际上就是在网格形成中画一堆正方形。我不知道Moai的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
endhttps://stackoverflow.com/questions/8597138
复制相似问题