【问题标题】:Moai grid with tile colors带有瓷砖颜色的摩艾网格
【发布时间】:2011-12-21 22:16:37
【问题描述】:

我正在尝试使用 Moai SDK 为游戏创建网格。网格中的每个图块都应该能够填充颜色。

所以实际上我有两个问题:

  • 使用 Moai 构建网格的最佳方法是什么
  • 如何用颜色单独填充每个图块

谢谢

【问题讨论】:

    标签: grid lua moai


    【解决方案1】:

    使用 Moai 构建网格的最佳方法是什么

    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)
    

    【讨论】:

    • 在 v1.4p0 中(不确定具体更改了哪个版本)grid:initGrid(4, 4, 64) 行将是 grid:initRectGrid(4, 4, 64, 64)
    【解决方案2】:

    针对整个代码进行了编辑。

    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,它是 Moai 的高级 API(我从这里转述了这段代码)

    【讨论】:

      【解决方案3】:

      构建网格实际上只是以网格形式绘制一堆正方形。 我不知道 Moai 的 api;但我希望你可以直接 drawSquare(x,y,width,height,color)

      所以你有:

      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
      

      【讨论】:

      • 也许我不清楚我的问题。我正在将我的 Corona 游戏移植到 Moai,所以逻辑就在那里,但我找不到正确的函数来绘制一个正方形并用颜色填充它。另外我不知道是否应该手动创建每个图块,或者 MOAIGrid 是否可以选择。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2022-10-14
      • 2014-01-02
      • 2014-01-17
      相关资源
      最近更新 更多