Reputation: 93
I'm trying to draw a chain of symbols by using a loop. I'm doing this way, but it always draw x
circles...
1 to: x do: [
(self lastWasSquare)
ifTrue: [ self drawCircle]
ifFalse: [ self drawSquare]
]
I also tried:
x timesRepeat: [
(self lastWasSquare)
ifTrue: [ self drawCircle]
ifFalse: [ self drawSquare]
].
But still drawing circles. I also tryed to do it by adding a :n |
variable to the loop and asking if even, but again, it's always executing the circle code.
What am I doing wrong?
Thank you
Upvotes: 4
Views: 1063
Reputation: 4958
It looks like your call to self lastWasSquare
keeps returning true
so that your #ifTrue:ifFalse:
keeps going into the block that calls self drawCircle
. You can either:
drawCircle
and drawSquare
methods properly set your lastWasSquare
instance variable (at least I'm assuming that's just a getter method).The first way is better if you need the lastWasSquare
value anywhere outside the method you're working on. The second way is better if it's the only place where you're drawing circles or squares (keep the scope as small as it needs to be) and could look something like this:
| lastWasSquare |
lastWasSquare := false.
x timesRepeat: [
lastWasSquare
ifTrue: [ self drawCircle ]
ifFalse: [ self drawSquare ].
lastWasSquare := lastWasSquare not
].
So you're continually toggling the lastWasSquare
between true
and false
, and it will draw alternating shapes. (I'm assuming that's what you're trying to achieve when you say "draw a chain of symbols"...)
If neither of these apply, then, as Uko said in the comments, you'd need to post more of your code in order for us to be able to help you.
Upvotes: 3