Coding at Home: July 2nd, Sonic Workshop #3

Come and join us in the Sonic Workshop today at 1pm, Irish time!

Yesterday

Yesterday we spent some time writing some code in a function in a shared file.

This comes in useful, like we said, when you want to do the same tasks over and over again.

Today’s session

In the previous page we had a function pre-written for us. Today we’re going to write our own. Like yesterday’s function, ours will also return a Graphic to place in the scene.

In our createCrystal function we return a Graphic by using a dash and a greater than sign, followed by the type we return. Like so:

public func createCrystal(image: Image, sound: SonicSound) -> Graphic  {
    //...
}

We’ll want to come up with a good, descriptive name for our new function so it makes sense when we use it in code, later.

This requires a bit of thought up front, but it will pay off when we can come back to our code and understand it without too much trouble.

We’ll also get introduced to a new wrinkle in the syntax for closures. Remember, closures are effectively unnamed functions. With functions, we can define parameters you can pass in, as well as a return type.

In the closure we use for the setOnTouchMovedHandler we’ll see in this session we’ll show you how to specify a parameter for a closure and what that does to what a closure looks like.

Like many things with code, there are many ways to write closures. We’ll try to use the most clear way possible to avoid confusion and make our code readable.

Let’s get coding! We’ll see you at 1pm!

Leave a Reply

Your email address will not be published. Required fields are marked *