如何在 smalltalk 中更改变形的位置?二维网格

问题描述 投票:0回答:1

我无法更改某些变形的位置。虽然可以通过以下方式将它们从 Inspector 中移出:

self position: 50 @ 50

例如。

我写了一个函数,它应该设置一个二维变形集合的位置。 Cell 是简单 switchmorph 的子类。拥有这个功能的类是 bordered morph 的子类。

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 ] ]

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

我希望有人能帮助我。

解决方案:
the solution

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 ]
smalltalk pharo
1个回答
4
投票

您没有更新变量

xPos
row
yPos
col
。所以,而不是

            xPos + cellSize.
            row + 1 ].

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

你应该说

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

    row := 1.
    yPos := yPos + cellSize.
    col := col + 1].
© www.soinside.com 2019 - 2024. All rights reserved.