[英]How can I change the position of a morph in smalltalk? 2D Grid
您好我無法更改某些變形的位置。 雖然可以從Inspector中移動它們:
self position: 50 @ 50
例如。 我寫了一個函數,它應該設置2d變形集的位置。 Cell是簡單switchmorph的子類。 擁有此函數的類是帶邊變形的子類。
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 ]
您沒有更新變量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].
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.