Live data from Hacker News

Flappy Bird in Swift

github.com

111–120 of 126 posts

Re: Flappy Bird in Swift

#111
post #96

Earlier quoted context omitted.

I think just the fact that you were able to build a game with just 4 hours of learning a new language speaks volumes about Swift's barrier to entry. Well done!

Any up-to-date C# developer already knows Swift.

Any up-to-date Java developer already knows Swift :)

Re: Flappy Bird in Swift

#112

Author here - I didn't expect to see this here this morning. I'd intended to write a longer post :) In any case, here's a few things I learned about swift yesterday building this. Please note that I have about 4 hours swift experience, so feel free to correct anything I say that's wrong. 1. To make properties on a class you simply declare the variable on the class e.g.: class GameScene: SKScene { var bird = SKSpriteN…

A couple more interesting swift features that seemed a bit weird to me after skipping through the reference:

1. Strings, arrays and dictionaries are value types (like structs in C# - stored on stack, not on the heap) with special copying rules for arrays.

2. Custom operators - can be defined as a sequence of "/ = - + * % ! & | ^ . ~" characters and made prefix, infix or postfix. There is an example in the reference that defines a prefix operator "+++" for vectors

Re: Flappy Bird in Swift

#113

Author here - I didn't expect to see this here this morning. I'd intended to write a longer post :) In any case, here's a few things I learned about swift yesterday building this. Please note that I have about 4 hours swift experience, so feel free to correct anything I say that's wrong. 1. To make properties on a class you simply declare the variable on the class e.g.: class GameScene: SKScene { var bird = SKSpriteN…

"I use `var` everywhere and I'm pretty sure I should be using `let` a lot more. I haven't worked with Swift enough to have a strong intuition about when to use either."

I think you should type let everywhere, except when you cannot get away with it.

So, it is var when declaring a loop variable. Elsewhere you almost always can introduce a new name. So, instead of

   X += 1
do

   let newX = x + 1
Those rules are too strict, but not much so, and you will learn the difference faster if you overcompensate.

If you find yourself creating a newerX, a newestX, etc. you may be better of with a var, but chances also are that you are better of writing your code differently (for example can you iterate over a constant array of values instead of doing x+=1 a few times? If your function needs to create 10+ Local variables, can you factor out a function?)

Re: Flappy Bird in Swift

#115
post #91

Earlier quoted context omitted.

And the same, in HTML form: https://developer.apple.com/library/prerelease/ios/documenta...

Note that near the top of "A Swift Tour" there is a link to the playground which you can download and open in Xcode 6.

Wow, that is cool. Learning Swift is going to be fun.

Direct link to the playground file: https://developer.apple.com/library/prerelease/ios/documenta...

Re: Flappy Bird in Swift

#117
post #108

Earlier quoted context omitted.

Heh. Reminds me of when I correct people for if var == true {...} or (and I have actually seen this) if ( == true) { return true; } else { return false; } I sometimes think that simply giving those 5 lines to a person and observing if they make a face like you've handed them a bucket full of day-old fish is a better test for programming aptitude than anything.

I'd argue the second is clearer in many cases. Readability trumps conciseness any day.

Really? In which cases would the previous example be clearer than

    return expression;
What readability does the verbosity add?

Re: Flappy Bird in Swift

#118
post #108

Earlier quoted context omitted.

What about this? var spawn = SKAction.runBlock self.spawnPipes

Heh. Reminds me of when I correct people for if var == true {...} or (and I have actually seen this) if ( == true) { return true; } else { return false; } I sometimes think that simply giving those 5 lines to a person and observing if they make a face like you've handed them a bucket full of day-old fish is a better test for programming aptitude than anything.

  if var == true {...}
In some languages var might be truthy but not equal to true. The falsy values may be more of a problem at times.

However in the strongly typed swift I don't think it will be a problem as I suspect (but haven't read/tested enough yet to confirm) that only false and nil will be falsy and that comparison with true will throw errors if var isn't of bool type.

Re: Flappy Bird in Swift

#120

Earlier quoted context omitted.

I don't have XCode to try it out, but according to the book, you can replace var spawn = SKAction.runBlock({() in self.spawnPipes()}) with var spawn = SKAction.runBlock({self.spawnPipes()}) or even var spawn = SKAction.runBlock() {self.spawnPipes()} or var spawn = SKAction.runBlock {self.spawnPipes()} EDIT: formatting for code

This is awesome and I didn't know this. Thanks for posting it - I don't have much swift experience so I assume there are several things in the code that will be shown to be non-standard.

> I don't have much swift experience…

Considering the language has only been public for <36 hours, I would imagine effectively no one outside of Apple (and its Alpha partners) does.

Post reply on HN