如何在闲聊中更改变形的位置?2D 网格



我无法更改某些变形的位置。虽然可以使用以下命令将它们从检查器中移出:

self position: 50 @ 50

例如。

我写了一个函数,它应该设置一个 2d 变形集合的位置。单元是简单开关形态的子类。拥有此函数的类是边界变形的子类。

setCells
| xPos yPos row col |
xPos := 0.
yPos := 0.
row := 1.
col := 1.
cells := OrderedCollection new.
cols timesRepeat: [ cells add: OrderedCollection new ].
cells do: [ :each | rows timesRepeat: [ each add: (Cell new size: cellSize) ] ].
rows
    timesRepeat: [ cols
            timesRepeat: [ ((cells at: row) at: col) position: xPos @ yPos.
                xPos + cellSize.
                row + 1 ].
        row:=1.
        yPos + cellSize.
        col + 1 ].
cells do: [ :x | x do: [ :y | self addMorph: y ] ]

我没有收到错误,实际上所有单元格都已添加,但都位于同一位置。当我试图将它们抛入世界时,同样的事情也会发生。都在同一个地方。

我希望有人能在这里帮助我。

解决方案:解决方案

calculatePositions
| row col xPos yPos |
row := 1.
col := 1.
xPos := 0.
yPos := 0.
rows
    timesRepeat: [ cols
            timesRepeat: [ ((cells at: row) at: col) position: xPos @ yPos.
                xPos := xPos + cellSize.
                row := row + 1 ].
        row := 1.
        xPos := 0.
        yPos := yPos + cellSize.
        col := col + 1 ]

您没有更新变量xPosrowyPoscol。所以,而不是

            xPos + cellSize.
            row + 1 ].

    row:=1.
    yPos + cellSize.
    col + 1].

你应该说

            xPos := xPos + cellSize.
            row := row + 1].

    row := 1.
    yPos := yPos + cellSize.
    col := col + 1].

最新更新