How can I change the position of a morph in smalltalk? 2D Grid

瘦欲@ 提交于 2019-12-10 20:49:53

问题


Hello I have trouble changing the position of some morphs. While it is possible to move them from the Inspector with:

self position: 50 @ 50

for example. I wrote a function wich should set the position of of a 2d collection of morphs. Cell is a subclass of simple switchmorph. And the class owning this function is a subclass of 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 ] ]

I dont get an error and actually all cells are added but all on the same position. When I try to cast them into the world instead the same happens. All on the the same spot.

I hope someone can help me out here. Cheers

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

回答1:


You are not updating the variables xPos, row, yPos and col. So, instead of

            xPos + cellSize.
            row + 1 ].

and

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

you should say

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

and

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


来源:https://stackoverflow.com/questions/48348925/how-can-i-change-the-position-of-a-morph-in-smalltalk-2d-grid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!