| 31 | == Smallcode Syntax == |
| 32 | |
| 33 | === How do I leave a method without returning a value, e.g. early return? === |
| 34 | In !Smalltalk `^` behaves similar to return in other languages. If a method in !Smalltalk should return nothing, typically you return `self`. You should avoid to return `nil`. |
| 35 | |
| 36 | === How do I break inside a loop? === |
| 37 | A way to do so is to put the loop into its own method and to add another return. However, you should consider the collection protocol. Usually, there already is a method, that solves your problem. Possible methods could be: `#detect:ifFound:ifNone:` or `#detect:thenDo:ifNone:`. |
| 38 | |
| 39 | === Are there abstract classes in !Smalltalk and how should I name them? === |
| 40 | In !Smalltalk a class is abstract when a method is implemented with "self subclassResponsibility". However, you can still create objects of this class, but this method cannot be called. A typically idom is to extend the name with the word "Abstract". Examples in the image are: AbstracEvent, AbstractFont, or AbstractSound. |
| 41 | |
| 42 | == Idoms and Patterns == |
| 43 | === How to I access class variables or methods? Via classname or self class? === |
| 44 | |
| 45 | |
| 50 | === How do I repeat an image in the background? === |
| 51 | An image can be repeated in the Backround using an !InfiniteMorph as a morph's fillstyle: |
| 52 | {{{ |
| 53 | aForm := Form fromFileNamed: 'picture.png'. |
| 54 | anInfiniteForm := InfiniteForm with: aForm. |
| 55 | Morph new |
| 56 | extent: 500@500; |
| 57 | fillStyle: anInfiniteForm; |
| 58 | openInHand. |
| 59 | }}} |
| 60 | |
| 61 | === How do I react on deleting morphs using halos? === |
| 62 | If a morph is delted using the X in the halo menu, `Morph>>dismissViaHalo`, `Morph>>dismissMorph`, `Morph>>delete` are called. If your morph is a subclass of Morph, you can overwrite one of these. Do not forget to call the super implementation. |
| 63 | |
| 64 | === How do I prevent a Morph from stepping directly after opening? === |
| 65 | |
| 66 | `Morph>>wantsStep` can be overwritten returning false. As a result the morph will not start stepping immediately. Neverthelesse, you can start stepping with `Morph>>startStepping` later on. |
| 67 | |
| 68 | |
| 69 | === How do I implement a parallax scrolling for background images? === |
| 70 | |
| 71 | === How do I change the size of an image independent from the aspect ratio? === |
| 72 | A Form can be scaled using `Form>>#magnify:by:smoothing:`. The scale factor can either by a single value or a point to define the scale factor for both height and width. |
| 73 | The following examples shows how to change the size to 100@50 independent from the original aspect ratio: |
| 74 | {{{ |
| 75 | MenuIcons helpIcon in: [:form | |
| 76 | (form |
| 77 | magnify: form boundingBox |
| 78 | by: (100@50) / form extent |
| 79 | smoothing: 1) asMorph openInHand]. |
| 80 | }}} |
| 81 | |
| 82 | === How do I insert morphs in the background?=== |
| 83 | |
| 84 | Instead of using `addMorph:` you can use the following methods to influence the z-level of morphs: |
| 85 | `#addMorph`, `addMorphFront:`, `#addMorph:behind:`, and `#addMorph:inFrontOf:`. |
| 86 | |