A blog about one man's journey through code… and some pictures of the Peak District

Tag Archives: iPhone

In this post, we covered how to move an object on the screen; this time, we’re going to control it. However, before we do, let’s change the game orientation to landscape only, as that’s really the only thing that makes sense on an iPhone for this type of game.

Landscape

The first thing to do is to change the code in the GameViewController to force the orienetation to landscape:

When you run the simulator, the game will now appear on its side; to rectify that, select Hardware -> Rotate Left*:

Control

As you may have noticed from the previous post, we do have a modicum of control over the rectangle; let’s now change that so that we can press to the left and have it move left, or on the right and have it move right.

All we’ve actually done here is to create a new rectangle, and sourced it right at the centre of the player. We’ve added it to the self construct, so the automatic rendering will pick it up; next, we need to move it:

The purpose of this post is to take the code that we created here and run it in the simulator.

Leaving the Playground

To a .Net programmer, this seems like an alien concept but, depending on the mode that you run Xcode in, it results in a different IDE. If, instead of selecting Plaground, we select “Create an Xcode Project”, the whole app experience changes:

There are a lot more options available now; but let’s stick to the most basic:

The created Single View App should look like this in structure:

The target of this post is simply to take the few lines of code that we wrote in part one, and have them run on a simulator.

The first thing it does is launch the ViewController, so let’s take the code that we created in the first part, and add it directly to the ViewController; the new controller will look like this:

I’ve never programmed with XCode before; I’ve never even owned a Mac, so since I now have access to one, I thought I’d see if I could create a basic game. In order to motivate me to complete this, I thought I’d document my efforts, and try to put at least one post up every two weeks. As I knew absolutely nothing about this to begin with, I’m going to start from there.

The Basics

The first thing that you’ll need is an Apple account. You can use one that you might have set-up for your iPhone; otherwise, you can set one up here. It’s free to register, and there’s no need to sign up for a developer account immediately; although, should you wish to, it will cost you $99.

The next stage is to install Xcode; which can be found here. Xcode is the IDE, so it’s the equivalent of Visual Studio – it is not a language in its own right. The language we’re dealing with here is called Swift.

Your first program

When you initially run Xcode, you’ll be faced with a screen like this:

Since we don’t know what we’re doing, let’s select “Get started with a playground”:

And let’s select a Blank playground.

We then get an (almost) blank text editor:

The examples given seem to introduce a new level of complexity by including controllers; however, this is the simplest example of a working “Hello, world” program that I could come up with:

The UILabel object has to live within a frame; otherwise it will not appear on the screen. The default colour is black, but as I’ve specified the colour of the view, I thought it made sense to specify all colours.

This sets up the view; which seems to map to a form in WinForms, or a view in WPF. Again, the view needs a frame; and, in this case, it does need a colour (as the default is black).

The x, y, height and width variables are just guesses – so these may need changing later. Finally, we set the current Live View on the playground so that it knows which view to display.

Running the Code

If you run that initially, you’ll see that, whilst it compiles, nothing actually happens; the trick is here:

Summary and Target

Once you’ve established the basic syntax, the code suddenly seems much more familiar. There are some things that appear foreign to the eyes of a .Net developer; for example, “let” establishes a constant. In the case that it’s used here, it’s a constant pointer.

I recently upgraded my iPhone 5c to an SE. Everything seems a little smoother on the SE (although I’d swear blind it gave me an electrical shock while scanning my fingerprint!).

When I upgraded, so that I didn’t lose anything, I simply restored the latest backup from the iCloud account linked to the 5C. However, when I got on the train and tried tethering my laptop to the phone, it failed. In fact, it looked like the WiFi connection to the phone was actually broken, as it kept flashing connected and would then disconnect!

The fix for this turned out to be relatively straightforward, you simply rename the phone:

In a divergence from my usual software development ramblings, I thought I’d post my opinions on my latest phone (an iPhone 5c), along with how it stacks up against my previous Nokia Lumia 800

Why Windows Phone

If you have a look through my previous posts, you’ll see the reason for my choosing a Windows Phone last time. I spend a lot of time programming for mobile devices, and I have one Windows Phone 8 app published. Of course I can’t run that on the Lumia… and here’s where Microsoft first started to lose me.

Before getting the phone I looked around at some other phones. I’ve had Apple products before, and have generally been satisfied. I’m not a mad Apple fan, but I have a couple of iPods and an iPad. They work okay. And all the apps that are likely to be written have been written 15 times over.

Windows Phone was new, and it had a very small app base – there was the opportunity! Also, I’d had a Nokia phone before (5120 I think), and it was indestructible!

In general, the Nokia did mostly what I expected – it was very sturdy; I frequently dropped it, scuffed it, and generally treated it the same way that I treated my previous Nokia. The touch screen was excellent and reacted as expected. The Windows 7.1 phone interface was nice. The iPhone feels flimsy and breakable by comparison (as evidenced by the number of people that everyone knows using an iPhone with a smashed screen).

Another excellent feature was the tethering. Given that this was the first phone I’d had that I could tether to, I didn’t realise how good it was until I got the iPhone. Here’s my laptop network screen sat next to the iPhone with Personal Hotspot switched on:

To get the laptop to recognise the iPhone I have to turn the personal hotspot of and on again a few times. Windows Phone always worked first time; I’m very aware that I’m pointing out that Windows works with Windows, but if you tether a lot, this can be a factor.

Bad Stuff

There were a number of annoying factors, but the absolute worse was the heat. I couldn’t keep the phone in my pocket, because it literally burned me. I imagine a related factor was the battery life, which was abysmal (I basically needed to charge it 2 – 3 times a day just for basic use).

Zune was also a let-down. I couldn’t fit all my music on the device, so I tried to use playlists, as I’d previously done with the iPhone, but Zune insisted on clearing and re-filling the phone every time; meaning that syncing it took prohibitively long. The playback on the phone was also a poor experience: I’d be listening to a the latest edition of DotNetRocks, pause it, and then if I continued to use the phone, it would forget the position I’d listened to and start again. Of all the bad features, this was the absolute worse.

My last gripe about Windows Phone it what Microsoft did when they introduced Windows Phone 8. As I said earlier, I bought this to do development, and after having it for 6 months, it felt like a giant two finger solute from Microsoft, saying that Phone 8 was out and old phones would not upgrade. I know apple and Android both do this, but they can afford to; they have large and loyal user bases.

Happy with the iPhone?

Like I said, it’s flimsy, and overpriced; however:
– It has an excellent GPS.
– I can carry it in my pocket without having to be treated for second degree burns.
– It has a mature and large app base.
– iTunes is not perfect, but it does work.
– The stuff I play on it remembers where it was stopped.

I did consider getting another Windows Phone when it was time to renew, but ultimately, I couldn’t justify it. The Nokia Phones that are available for 8 didn’t strike me as particularly good this time around (they don’t even look that sturdy now), and ultimately, I couldn’t risk MS releasing Windows Phone 9 and leaving me behind again.