From ActionScript to iPhone

A couple of weeks ago I was looking at the vast list of unfinished projects and random ideas i’ve accumulated over the last year or so and decided I needed to choose one and finish it. That’s when I decided to concentrate on my next game, Gravity Pods 2. As soon as I started on that, I had the epiphany that it would make a great game for the iPhone.

So, while I’ve been cranking on the Flash version of the game, I started to look into learning Objective C 2.0 and iPhone development. The transition is not an easy one. ActionScript, JavaScript, Java, C, C#, C++, PHP, etc. are all pretty similar in terms of basic syntax. Even Python is not that foreign. Most do stuff like this:

myObject.someMethod(someParam);

or maybe

myObject->someMethod(someParam);

But what the hell is this???

[myObject someMethod:someParam];

OK, that’s probably the worst of it, and you get the hang of it pretty quickly. There’s other basic syntactic weirdness like “-” instead of “public” and “+” instead of “static” and @property and @synthesize for making getters and setters (which is kind of cool – I wish ActionScript had that), and the string syntax @”someString”. All in all, you look at your first Objective C program and you’re like, “WTF???”

Then there’s interface builder. You make an interface by laying out visual components. This creates a xib file. xib is the updated, xml based version of nib files, which have been around in Mac dev forever. It’s a view – think of it as an MXML file. And it’s linked to an actual class, the controller – think code behind maybe. But linking elements from the xib to the controller class is a bit odd. Instead of simply giving your text field an id that becomes accessible as a property in the controller, you have to create an “outlet” in the controller. An outlet is a special variable to link to an element in the xib. Then you have to do this funky drag and drop connect the blue line from the text field to the “file’s owner” icon, which will pop up a list of outlets for you to choose. Same thing for events. You create an IBAction in the controller – which is an event handler – then drag between your button or whatever and the file’s owner and choose the action. Weird. Actually, that’s how all the books and tutorials tell you to do it. But I discovered the connections panel which at least gives you feedback of what outlets are available, what is connected to what, and what remains unconnected.

Anyway, not trying to scare you off. Just pointing out that it’s a very different paradigm of programming and that if you want to learn it, you need a decent resource. I’ve dug through a bunch of tutorials and sites. I got the prerelease Pragmatic book on iPhone development, which is pretty good, but I think walks you through a bunch of stuff without really explaining what and why you are doing too well. “Create the outlet like this, create the label like this, drag from here to here to connect them, save and compile!” Huh? It works, but what the hell did I just do? Could I do it again on my own? Um … maybe, I guess.

But I just got this book yesterday:

Beginning iPhone Development: Exploring the iPhone SDK, by Apress. THIS is the book you want to get if you are … beginning iPhone development. I really love the way this book is written. It takes each little step of the process and tells you exactly what it is for and why you are doing it. It sounds tedious, but I’m sitting there reading it going, “ahhhhh….”, “oh….”, “ah… I see now!” At the end of each project, you not only made a functional app, but you understand each and every line of code you wrote, and what all those draggy-droppy blue line things did. I got through three and a half chapters last night after dinner. Before that I was a bit overwhelmed but optimistic. Now I’m just plain excited to learn more.

I’ll probably be posting some AS3-Objective C comparisons later on, if only because teaching is a great way of learning. In the mean time, Andy Jacobs has already started that kind of thing here: http://andyj.be/blog/. Check it out. And get that book.

This entry was posted in iPhone. Bookmark the permalink.

23 Responses to From ActionScript to iPhone

  1. It’s rewarding to see that the language is actually not alien, but human and logic. 🙂

    I have put aside 3 weeks to learn and build my first iPhone app, and I’m in the middle of this. There’s a lot to learn, a lot of documentation, there the guides on the developer’s website on Apple, there’s the Stanford videos on iTunes, and there’s tons of blogs and free code on GitHub.

    I think this was difficult when it started, two years ago or even one year ago. Now there should be a ton of documentation for anyone really passionate about learning this new stuff.

    The reason I do it is because truth be told, Apple does amazing stuff. And I think tablets will be as popular as phones today, in less than 5 years. I don’t want to live in those times without developing for these devices. And I don’t foresee Apple changing to another language anytime soon.

    It sucks, yes. But it’s the right way to go.

  2. Marcus Booster says:

    This is why I’m looking forward to Android. At least from my perspective going from AS3 to Java is a lot more intuitive than jumping through all these hoops.

    But thanks for all the run-throughs. It’s certainly an interesting device, and I’d love to work with it. Hopefully there will be more diverse options in the future.

  3. Hi Keith,
    I may suggest also the book from Erica Sadun “The iPhone Developer’s Cookbook”, which covers a lot of aspects of the SDK programming by giving quick and deep example on a variary of topics.

    Also, if you know C/C++, there is no need to translate every thing into Objective-C, only the “outer” shell of your application, the one that talk to iPhone OS, needs to be in Obj-C. You can easily develop the logic of your applications in C/C++ (and keep it platform independent….) and intermix with PObj-C only for the drawing and input stuff; XCode support this just fine!

  4. Thanks for your review of the book, now I’ll be ordering it for myself since I’m an Obj-C noob. I probably won’t get a chance to read it until I finish this other RTP book though.

  5. Thank you so much for this post, Keith. I was wondering if it would be hard for a Flash developer to get into iPhone development. I’ll definitely check out the book!

  6. Andy Jacobs says:

    Thanks For the Heads-Up, I also have the book lying next to me. It’s a fantastic book!
    If you have any suggestions for my blog please let me know 😉

  7. Pedram says:

    Yes,
    this is my problem,
    objective-c syntax it’s very weird.
    and i need to book for learning this language.
    very thanks.

  8. liam walsh says:

    Speaking from my own experience (app still ‘in review’ a full five weeks after submission, but that’s it’s own story) the objective c syntax is only one tiny hurdle.

    Apple’s interface builder is another, as is learning the cocoa framework and it’s UI elements. Again this is pretty similar to learning what non-flash guys go through when learning actionscript. The real kicker is learning that the performance of this stuff kinda sucks if you want to do anything really ‘flashy’ at any sort of decent framerate. The animation classes in quartz are lovely, but limited.

    After learning and becoming frustrated with first the cocoa elements (uiimage, nsarray) etc I tried the quartz drawing API. Their performance limitations seemed crazy to me, how was all the cool stuff being done?

    OpenGLES.

    If you’re doing anything ‘gamey’ you’re better off getting the basics of objective c down and then diving straight into doing all your rendering through an openGLES layer. The crash landing demo is a good place to start.
    This was the hardest part for me, I know OpenGL isn’t a programming language, it’s a drawing API but I was unfamiliar with it and it drove me mad. I grew up with Flash, I’ve been spoilt for drawing API’s.

    OpenGL is not OO, not as I understand OO anyways, it is very proceedural. While I was learning and unsure of the meanings of other people’s code I was poking around in at times it felt like Voodoo… I reccomend the big red book. It’s slow, painful going but if you wanna know what in God’s name is going on, that’s the way to do it.
    Once you learn a few of the optimisation tricks and get the hang of the coordinate system and stuff, it’s breathtaking.
    It’s just a phone for christsakes but the performance you can get out of it is amazing. Hardware acceleration FTW!

    So if I can offer anyone any advice, particulary the pure Flash guys who’ve always been a bit ‘meh’ about Flex and the like:
    – learn OpenGL
    – learn about pointers
    – figure out if you’re going to use the built in garbage collector or roll your own memory management, some example code uses one method, some another, watch for the memory leaks, you don’t get a lot of ram to play with.
    – just use straight ‘c’ if you like, especially if you’re not popping and pushing a whole lot at runtime some of that cocoa stuff isn’t worth the hassle and the mutable arrays hurt a whole lot more.

    Cheers Kieth, long time reader, first time poster. Hope to see you again in Brighton next year.

  9. felix says:

    pointers – urrgh. Why can’t Apple use a modern programming language?

  10. I bet you miss support of Flash in Iphone. It’s Apple’s turn to approve it. Once done – everybody will be happy

  11. liam walsh says:

    @Felix: Pointers aint so bad when you get to know ’em. And the compiler will give you a warning when you’re doing something you shouldn’t… (Nice work on your site btw)

    🙂

  12. kp says:

    This should help you understand pointers:

    http://www.bit-101.com/blog/?p=988

    🙂

  13. Mike J says:

    I’m working through this myself right now. The language was frustrating for me to learn as it’s very different from most modern languages. The whole messaging syntax for communicating, the documentation without examples on using, and several other aspects of it were daunting to say the least.

    I’ve finished my first application and am still waiting ( after almost a week ) for the “magic link” to show up on my dev center so I can request certificates to begin the provisioning for submission. I feel like I’m starting to get a decent handle on things, finally, but there are still many things that I find difficult at times.

    I’ve been staying away from Interface Builder since there feels to be a lot of voodoo involved in it and I really prefer to know what needs to happen ( and what IS happening ) under the hood. It can make things a bit more difficult to start with but I think it’s helped me understand things a bit better.

  14. Another decent book is “iPhone in Action”. Covers 1/2 on Web Dev and the other 1/2 on native dev with the SDK.

    P.S. Shouldn’t this post be titled: From ActionScript to Objective-C? 🙂 or from Flash to iPhone native App Development?

    I guess it sounds cooler the other way … probably works better for searches too. 🙂

  15. It’s a damn shame the barefootsoft guys haven’t released their b.tween product (yet):

    http://www.barefootsoft.com/index.php?option=com_content&view=article&id=73:new-boulevart-iphone-app&catid=41:news&Itemid=80

    Must be some legalities holding it back, or perhaps they know something everyone else doesn’t? … or perhaps they have moved on … not sure.

    -sj

  16. Hi Scott,
    yes it is shame we didn’t released it yet, there are some good and very valid reasons for this, but none of them are included in what you list (legalities etc). We are validating the ability to convert most of the AS2/3 we come across and beside not being a simple task, it requires time, actually a lot of it. Speaking to developers, we have learnt that it is far more interesting for them to be able to mashup Flash with the hosting platform native elements and capabilities; we are not a surrogate of a Flash player and never intend to be. At the moment we are working with a number of companies and helping them to convert their Flash content into native iPhone applications; see the latest Max Award in Milan, where Boulevart has won one of the prizes thanks to b.Tween
    We will start shortly to introduce b.Tween live in conferences around the globe, beside iPhone, we are actually targeting the whole market of smart devices.

  17. Iain says:

    Nice post. I wouldn’t say you’ve scared me off, but now I think I will definitely stick with learning Java properly before I bother getting into this obj-c / iphone mess. Unity3D also looks like a nice alternative for iphone games if you’ve got cash – it’s C# and JavaScript all the way!

  18. Haha, I can so relate to this… NSmyass. Wanted, an AS3 objc bridge 😉

  19. jacob says:

    i have only been doing objective c / iphone dev for a short time, but it’s not as unbearable as some people make it out to seem. I like coding in AS much more and debugging is much easier, but it’s still a very powerful language and can be learned fairly quickly if you are willing to accept that not every language is like ActionScript =)

    And i can second (or third, fourth) that the book he recommended is a great find. I wish there was info on connecting to a remote dbase, but i am sure when i need it i can find that info.

  20. Swfwizard says:

    So far the biggest issue I have with iPhone development is the drawing API(s) and lack of docs and simple snippets. All the [bracket notation] and syntax eventually sticks, but drawing simple gradients and custom stuff…now that’s another story.

  21. Swfwizard says:

    I am referring to drawing specific documentation, they are explaning the purposes, but are leaving out usage and implementation that would ideally accompany the purpose behind the CGContext methods.

  22. Trevor says:

    Very interesting to read your thoughts on this as I’m also diving into Obj-C + iPhone dev and coming from a very similar background experience. Cheers!

Leave a Reply