Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 277 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
277
Dung lượng
5,58 MB
Nội dung
www.it-ebooks.info For your convenience Apress has placed some of the front matter material after the index. Please use the Bookmarks and Contents at a Glance links to access them. www.it-ebooks.info v Contents at a Glance About the Author ���������������������������������������������������������������������������������������������������������������� xv About the Technical Reviewer ������������������������������������������������������������������������������������������xvii Acknowledgments ������������������������������������������������������������������������������������������������������������� xix Introduction ����������������������������������������������������������������������������������������������������������������������� xxi Part 1: Get Ready � � � Get Set � � � ■ ����������������������������������������������������������������� 1 Chapter 1: Say Hello to My Little Friend: The CoronaSDK ■ �������������������������������������������������3 Chapter 2: The Pillar of Creation: Lua ■ ������������������������������������������������������������������������������19 Part 2: Go! ■ ��������������������������������������������������������������������������������������������������� 43 Chapter 3: Basic Application Structure ■ ���������������������������������������������������������������������������45 Chapter 4: Title, Menu, and Settings Scenes ■ �������������������������������������������������������������������71 Chapter 5: The Game, Part 1: Core Game Code ■ ��������������������������������������������������������������103 Chapter 6: The Game, Part 2: Main Loop ■ �����������������������������������������������������������������������149 Chapter 7: The Game, Part 3: Player Control Input ■ ��������������������������������������������������������167 www.it-ebooks.info vi Contents at a Glance Chapter 8: The Game, Part 4: Collision Events ■ ���������������������������������������������������������������177 Chapter 9: Wrapping Up ■ �������������������������������������������������������������������������������������������������191 Part 3: The Postgame Show ■ ���������������������������������������������������������������������� 203 Chapter 10: Odds and Ends ■ �������������������������������������������������������������������������������������������205 Chapter 11: Testing and Publishing ■ �������������������������������������������������������������������������������231 Index ���������������������������������������������������������������������������������������������������������������������������������257 www.it-ebooks.info xxi Introduction Writing mobile apps is hard. Writing mobile games is even more so. Why is that? Well, there are probably lots of reasons I could state, but one jumps to the forefront almost immediately: variety. The variety of platforms available today is staggering when you consider developing an application that runs everywhere. From iOS to Android, from BlackBerry to Windows Mobile, not to mention a number of lesser platforms, there are lots of places your app could run. Even if you simply concern yourself with the two market-share leaders, iOS and Android, it’s still a daunting task to develop for both. Sure, you could always develop two versions of the same application targeted for each platform, and plenty of times that’s exactly what is done. That approach, however, has the significant downside of requiring substantially different skill sets and tools, which means you generally need two sets of developers to maintain two completely (or nearly completely, anyway) code bases. You can, of course, go with a web-based approach and let HTML, JavaScript, and CSS be the common platform you develop to. While that works in many cases, for things like games it tends not to work as well. You’re giving up much of the capabilities, power, and performance of the native platforms in that model, something most developers would prefer not to do. It’s a trade-off to consider, though: maintaining a single, potentially less feature-rich and performant code base versus maintaining multiple “ideal” code bases. Thankfully, there’s another option. There are a number of cross-platform tools that let you develop a single code base that can run on multiple platforms while still maintaining most, if not all, of the native capabilities you’d have if you’d done true native development. Of them, the CoronaSDK is one of the best. In this book, we’ll explore the CoronaSDK together, see what it has to offer, and learn how to use it to develop high-performance applications that can run equally well on iOS and Android. We’ll focus our attention on game development, since that’s what Corona is focused on. However, we’ll also see how it’s not exclusively for that—you can in fact develop any type of application you want with Corona! www.it-ebooks.info xxii Introduction The Book: An Overview We’ll break up the experience over the course of 11 chapters in which we’ll build a game, Astro Rescue, that will demonstrate a significant chunk of Corona functionality. The contents of those 11 chapters will break down thusly: 1. What is Corona? Why use it? The functionality it provides, how to get it, licensing and a first small example running in the simulator on your desktop PC. 2. The basics of Lua, the language that underpins Corona. 3. The overall structure of a typical Corona game, including things like code modules, the Storyboard API, basic event processing, object-oriented game design, program flow, and configuration files. 4. Getting started with graphics. Getting things on the screen, moving them around, memory management, transitions, UI widgets, and more. 5. The core Astro Rescue game code. We’ll get deeper into graphics, get going with some audio, and of course the underlying logic behind the game. Things like the main loop, core events, and the beginning of input handling. We’ll get into sprites and animation in more depth and get a first look at physics. 6. Deeper into the core game code we go! We’ll walk through the main loop in detail and look closer at graphics, transitions and animation, drawing techniques, and so on. 7. Various forms of input events such as touch, accelerometer, and gyroscope control will be looked at here. 8. Collision events, a core concept of most games. Here we’ll also get into some “special effects” like masking and gradients. 9. We’ll complete walking through the game code here and finish up any odds and ends that remain to be seen. 10. Some advanced topics such as ads, SQLite, in-app purchases, and game network integration. 11. How to take the now-completed Astro Rescue project and build it for Android and iOS devices, how to get it onto those devices, and test and debugging techniques. We’ll also look at the app store models available to us and how to get your app published in them. By the end, you’ll have a solid foundation on which to build. You’ll have a good picture of what Corona provides and a better understanding of how to put it to good use. You’ll be able to quickly and easily create the next great Angry Birds–level hit, at which point I hope you remember your favorite author. www.it-ebooks.info xxiiiIntroduction Saving Your Fingers Are you the sort that likes to type in every bit of code you see in a book? If so, have at it and enjoy! For the rest of us, you can obtain the source code for this book by visiting www.apress.com/source-code and save yourself a lot of time and energy. Perfection Is Relative This book is certified 100% flawless. Not a single mistake will be found anywhere within it. On the off chance that proves to not be entirely accurate, errata will be posted on the Apress web site. But, as I said, its not a real concern anyway. ;) If You Want to Yap at Me Have a comment to fire at me? A compliment to pay? A complaint to lodge? I’m all ears—virtually of course! Feel free to fling e-mails my way at fzammetti@omnytex.com. I’m also on “the Twitter” at the not very creative username of @fzammetti. www.it-ebooks.info Part 1 Get Ready . . . Get Set . . . Walking on water and developing software from a specification are easy . . . if both are frozen! —Edward V. Berard On two occasions I have been asked, “Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?” I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question. —Charles Babbage I have always wished for my computer to be as easy to use as my telephone. My wish has come true because I can no longer figure out how to use my telephone. —Bjarne Stroustrup Einstein argued that there must be simplified explanations of nature, because God is not capricious or arbitrary. No such faith comforts the software engineer. —Fred Brooks . www.it-ebooks.info 3 Chapter 1 Say Hello to My Little Friend: The CoronaSDK When did you start programming? What first piqued your interest? If it became a lifelong passion, as it has with me, then you almost certainly have an interesting story to tell when answering that question. My own story is in many ways unique in that I was most definitely in the right place at the right time at a special point in human history when personal computers were just coming into existence and, more importantly, just starting to enter the public consciousness. Through a bit of luck, coupled with a drive to do something I found enthralling, I was propelled down a very specific road in life that I am happily still traveling. You see, as David said in Prometheus, “Big things have small beginnings.” (Your opinion of that movie will determine whether you find that line the best thing about it or just one of many cool things!) The CoronaSDK is in a sense this same concept in microcosm: when you look at what it is, and especially at how easy it makes a great many otherwise complex things, it’s really kind of surprising, perhaps even shocking, that you can do such incredible things with it with so little effort. This book, I hope, will provide you with the foundation you need to take your idea—however small the kernel of the idea may be—and, combined with the Corona SDK, grow it into something huge. Before I get to the CoronaSDK itself though, allow me to be a bit self-centered and talk a little bit about myself! A Long Time Ago In a School (Perhaps) Far, Far Away . . . When I was around nine years old, my school district introduced a new curriculum dealing with computers. It was only a trial program and they didn’t know if it would get any traction, so they asked six of the top students in the school if they’d like to participate. As you can guess, I was one www.it-ebooks.info 4 CHAPTER 1: Say Hello to My Little Friend: The CoronaSDK of those students (my grades subsequently sank down to much more, umm, let’s go with modest levels . . . but at the time I was near the top). I had heard about computers on Star Trek and other television shows, but I had never seen one in person to that point in my life and I didn’t really have an appreciation for what they were and what they could really do. In any case, being a science and electronics geek even at that age I of course said, “Absolutely I want in!” So, for about an hour once a week I got to leave my regular class and head down to a small room with a couple of computers and learn how to program (after learning what they really were and could do, of course). We six initial students did this for a few weeks, but seemingly with each class the number of students that showed up shrank (it was completely voluntary and we could quit at any time). Eventually, it was just me left. One day, I had gone to the computer lab after school and I discovered to my surprise that I wasn’t alone, as had always been the case before. On this day, another student (also named Frank, coincidentally) was there. It turns out this other Frank was quite a bit more advanced than I was at programming. He had written a program that at the time amazed me beyond words. What it did was draw a man, like so: \ O / \#/ # / \ / \ I know, that barely looks like a man! However, you have to understand that until that point all I had seen in person on a computer screen was plain old text. I mean, I thought it was extremely cool that I could have text that asked for two numbers and text that told me what they were when multiplied by each other—that on its own was cool! An actual graphical man, though? That was a whole other level of cool! The program that alter-Frank made also drew the man in another slightly different way: O /#\ / # \ | | | | It would draw that first version of the man, wait maybe a quarter second, then clear the screen and draw the other version. The program did this repeatedly and the effect, for anyone that has ever drawn some pictures in a notebook and then rapidly flipped the pages, was animation! Believe me: I realize just how silly all of this sounds when compared to our modern Xbox games. However, you have to try to understand what it was like for me: this was just astounding! He had essentially replicated what you do with a notebook and some drawing on a computer screen! He took a very simple, basic technique and wrote a program to do it, yielding something that, really, most people at that point hadn’t seen computers do. Now, it didn’t take long at all before I realized that this conceptually simple technique leads quite naturally to something much greater: video games! The basic notion of drawing something on the screen, clearing the screen, and then drawing it slightly differently is, to this day, the core concept www.it-ebooks.info [...]... conceptually from your programmer’s point of view, it’s very similar That’s precisely what the CoronaSDK is, or simply Corona for short from here on out Corona: History at a Glance Corona is a product of Corona Labs, Inc., formerly Ansca Mobile It was originally created by Walter Luh and Carlos Icaza, former employees of Adobe Corona saw its first release in December 2009 supporting a single platform: Apple’s... reach the very top of the free -game chart in the iTunes Store! www.it-ebooks.info CHAPTER 1: Say Hello to My Little Friend: The CoronaSDK 7 Figure 1-1. Bubble Ball screenshot Many other games have ridden Corona to high places on the charts and have brought in good money for their developers, and this is on all platforms Corona supports Some other great examples of Corona include Engineer by Etherient,... The CoronaSDK In the interest of full disclosure, Engineer is actually my own game However, another one that isn’t is The Secret of Grisly Manor, from developer Fire Maple Games, as seen in Figure 1-3 Figure 1-3. The Secret of Grisly Manor screenshot As you can see, Corona is a horse you can most definitely ride to a win in mobile application development, most especially if you’re developing games... CHAPTER 1: Say Hello to My Little Friend: The CoronaSDK MORE ON IDE SUPPORT There are a number of Corona- specific IDEs that are now available including Corona Complete (http://coronacomplete.com) and Lua Glider IDE (www.mydevelopersgames.com/Glider) While you don’t need an IDE I certainly would encourage you to check these out I have yet to find proper support for Corona in any major IDE, such as Eclipse... that underpins Corona You will learn the basics of using it so that you can move on in later chapters to building an honest-to-goodness game and learn about many facets of Corona in the process Buckle up; it’s going to be a fun ride! www.it-ebooks.info Chapter 2 The Pillar of Creation: Lua In Chapter 1, you looked at Corona at a high level and began to get a feel for what it offers Corona provides... course being that whatever your goals for the games you write, whether it’s to make a million bucks or just to hone your skills, it’s one of the very best programming exercises you can do for yourself Hey, Wait, Isn’t This Book about the Corona SDK? ! All of that being said, programming games can also be hard Especially when we’re talking about mobile game development, that can be very true In the mobile... introduced to the Corona SDK You learned that it is a tremendously powerful cross-platform development kit for mobile applications, especially games It provides a supersolid foundation to build upon and gives you all the tools you need to create great things You got your first taste, albeit a small one, of what you can accomplish with just a little bit of code In doing so, you got Corona installed,... short is that without doing very much optimization on your part, the games (and nongame apps) that you build with Corona should be quite fast on most devices www.it-ebooks.info CHAPTER 1: Say Hello to My Little Friend: The Corona SDK 9 Note As with virtually any discussion of performance in programming, it’s generally accepted that Corona apps can be as fast as those written with most other libraries... from the Corona web site at http://coronalabs.com In fact, now would be as good a time as any to go ahead and do that if you haven’t already Go ahead, I’ll wait patiently for you! www.it-ebooks.info 10 CHAPTER 1: Say Hello to My Little Friend: The Corona SDK Note Although you can download the SDK for free, develop your application, and test as for as long as you want, Corona is not actually free To be... apps in chapter 11 and why this matters), and access to the latest versions of Corona before the public releases (which are updated only two or three times a year on average) The Corona SDK includes the usual SDK components including the API libraries, documentation, and examples More important, though, it also includes the Corona Simulator The simulator is a cross-platform application that, as the name . if you’d done true native development. Of them, the Corona SDK is one of the best. In this book, we’ll explore the Corona SDK together, see what it has to offer, and learn how to use it to develop. very similar. That’s precisely what the Corona SDK is, or simply Corona for short from here on out. Corona: History at a Glance Corona is a product of Corona Labs, Inc., formerly Ansca Mobile part, the games (and nongame apps) that you build with Corona should be quite fast on most devices. www.it-ebooks.info 9CHAPTER 1: Say Hello to My Little Friend: The Corona SDK Corona also