Apple Game Frameworks and Technologies: Wrote binary, meant decimal (p 85)

Hello, David.

An integer literal represents some arbitrary value. You can express this value as a decimal, binary, octal, or hexadecimal constant. Binary literals begin with 0b, octal literals begin with 0o, and hexadecimal literals begin with 0x. Decimal literals have no prefix—they are what they are.

In this case, the physics category is a single 32-bit integer that acts as a mask, and each bit represents a single category, which is why you’re limited to 32 categories per scene. With masking, using powers of two makes things easier—especially when using binary literals.

Now, having said that, what you pointed out wasn’t actually a typo, but the information was certainly misleading. What it should have read–and now does read–is as follows:

When setting your categories, it’s best to do so using powers of two: 1, 2, 4, 8, 16, and so on.

You’ll see this new explanation in the next update.

Also, later in the book, I explain how to create and use a Hashable OptionSet to set up and retrieve the physics category—something that is also mentioned in that same aside. Enums are great, and for simpler games, preferred. However, as your game grows in complexity, Hashable OptionSets provide a bit more flexibility. You’ll see this solution in use when you get to Val’s Revenge.

Thanks again for pointing this out. Have a great day.

2 Likes