Gravity on iPhone

Years ago I wrote a tutorial on how to make a bouncing ball in ActionScript, along with gravity and the ability to drag and throw the ball. That was 2001 and I still get emails almost weekly from people who are reading it and learning something from it (though how they get that ancient code to work is beyond me). When I wrote my book, Foundation ActionScript Animation: Making Things Move! I expanded the concepts there and that became the basis for a good chunk of the first half of the book.

Well, I recently decided to learn Objective C in an attempt to recreate my Gravity Pods game for the iPhone. I worked through several chapters of a couple different books and learned quite a bit, and decided to branch out on my own.

I think when most people start learning a language, they have a particular thing that they program to help them learn the language. It helps to have an app or example that you are familiar with as a goal to implement in a new environment. It’s a personal “Hello World” type thing. For John Grden, it’s a Star Wars trench run (LOVE YOU JOHN!). For me, it’s a bouncing, throwable ball with gravity. It has a bunch of different concepts – animation, screen refreshes / redraws, velocity, acceleration, collision detection and reaction, friction, user interaction. Once you know how to do all that in a language, you can really start do do any kind of game or animation with it.

So that’s what I did this weekend. And here is the result:

As you can see, I not only got all the elements of the original gravity tutorial going, I even added in accelerometer detection! I’m pretty psyched about how far I got with it in such a short time. I still have a lot more to learn to create a full game, but I might expand this into some small free apps – just fun toys to play around with – and submit them for release.

This entry was posted in Flash, iPhone. Bookmark the permalink.

21 Responses to Gravity on iPhone

  1. Pete says:

    that’s pretty cool Keith – I’m just getting to grips with iPhone stuff too. Which books did you read?

  2. Ash says:

    You could probably sell that on the app store for $0.99. People will buy anything 🙂

  3. kp says:

    Ash, I considered it. 🙂 Wouldn’t feel right though.

  4. kp says:

    Pete, I love the Apress iPhone book. My take on it here: http://www.bit-101.com/blog/?p=1761

  5. I really hope the flash community’s sense of sharing code rubs off on iPhone development. Thanks for taking the first steps Keith.

  6. P.Svilans says:

    Hey Kieth! Do you know of a way to develop for the iPhone on a PC? Any pointers to any programs would be nice!

    Great work!

    P.

  7. kp says:

    No idea if it’s possible to code for iPhone on a PC. I doubt it since all the dev tools are the same ones for developing OS X applications, and not much incentive for Apple to make PC based development tools. 🙂

    But if it makes you feel any better, I feel the same sense of loss at not being able to develop Silverlight apps on my Mac. 😉

  8. howard says:

    Keith – any chance you can post the source? Seeing how you built this could be a great jumping off point for other flash developers (myself included).

  9. kp says:

    howard. source isn’t quite ready to show. did a lot of hacking around to get it going. I plan on posting some kind of tutorial write up when it’s cleaned up.

  10. snatchy rhino says:

    KP, awesome. Only just stepped into iPhone development and pull this one out the bag – been studying from your experience and publications for a couple years. Your work has been invaluable to my work as a flash developer in the UK.

    I am keen to follow your development, I think you should speak with BigBoss who can host your apps on his repository for people to install via Cydia.

    Can’t wait to get the new book.

    Have a good xmas.

  11. Dave Jeffery says:

    Having just read this, I wouldn’t use an iPhone to stop my chair wobbling:

    http://wikileaks.org/wiki/Why_Apple_Won%27t_Allow_Adobe_Flash_on_iPhone

  12. kp says:

    Dave, I’ve been saying that all along. Given what the iPhone does already, putting a Flash player in it wouldn’t be an unachievable feat technology wise. It’s all about the business model. Apple makes great chunks of cash, both from developers signing up and paying $99 to get their apps in the store, and the millions of people who pay 99 cents to buy an app that makes a fart sound. Allowing Flash on the iPhone would drop a giant nuke in the middle of that business model. So be it. If you can’t beat em, join em. Then beat em. 🙂

  13. howard says:

    Keith – I’ve recently (yesterday) started porting old flash experiments to the iphone – so far so good! I’m curious to hear if you used the coregraphics framework for your gravity test or built it with open gl. So far I’ve found coregraphics to be pretty powerful.

  14. howard says:

    errr, scratch that. It’s no comparison on the device. Open GL is so much faster.

  15. kp says:

    yeah, i’m using core graphics right now. For this piece, no big deal. But GL is next on the list of stuff to wrap me brain around.

  16. Kit says:

    This is fantastic tutorial. followed from Robert’s blog… Awesome. Great works, Keith.

    Kit

  17. nate says:

    Great port. I saw other interested seeing the source for the December 23, 2008 2:20 pm
    Gravity on iPhone demo. Have you by chance posted a tutorial on this. I could see it providing the community a great head start on understanding iphone dev. I am new to it my self and would love to see if i could get the source to learn from.

Leave a Reply