39,59 €
Corona SDK is the fastest and easiest way to create commercially successful cross platform mobile games. Just ask Robert Nay, a 14 year old who created Bubble Ball - downloaded three million times, famously knocking Angry Birds off the top spot. You don't need to be a programming veteran to create games using Corona. Corona SDK is the number one tool for creating fun, simple blockbuster games. Assuming no experience at all with programming or game development you will learn the basic foundations of Lua and Corona right through to creating several monetized games deployable to Android and Apple stores. You will begin with a crash course in Lua, the programming language underpinning the Corona SDK tool. After downloading and installing Corona and writing some simple code you will dive straight into game development. You will start by creating a simple breakout game with controls optimized for mobile. You will build on this by creating two more games incorporating different features such as falling physics. The book ends with a tutorial on social network integration, implementing in app purchase and most important of all monetizing and shipping your game to the Android and App stores.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 441
Veröffentlichungsjahr: 2012
Copyright © Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2012
Production Reference: 1170412
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham B3 2PB, UK.
ISBN 978-1-84969-188-8
www.packtpub.com
Cover Image by Raphael Pudlowski (<[email protected]>)
Author
Michelle M. Fernandez
Reviewers
Mitch Allen
Nicholas C. Golden
Alan Grace
Clement Ho
Steve Hoffman
Predrag Pedja Končar
Walter Luh
Edgar Miranda
Jordan Schuetz
Jayant Varma
Acquisition Editor
Robin de Jongh
Lead Technical Editor
Chris Rodrigues
Technical Editors
Manasi Poonthottam
Manali Mehta
Ankita Shashi
Manmeet Singh Vasir
Copy Editor
Laxmi Subramanian
Project Coordinator
Alka Nayak
Proofreader
Lisa Brady
Indexer
Rekha Nair
Production Coordinator
Prachali Bhiwandkar
Cover Work
Prachali Bhiwandkar
Michelle M. Fernandez is a mobile game developer for iOS/Android devices and co-founder of MobiDojo (http://www.mobidojo.com). She is also a mentor for aspiring artists and programmers trying to break into the game industry. After experimenting with several game engines over the years, she was introduced to Corona SDK in late 2010 and has been an avid user of the program ever since. She enjoys creating tutorials and new game frameworks for others to learn from. When Michelle is not developing games, she is spending time with friends and family, playing video games, basketball, singing, or baking delicious pastries.
I would like to thank first and foremost my family and friends for their love and encouragement throughout the production of this book. To my dear friend and partner, Ed Ho, thanks for introducing me to Corona SDK. You have genuinely been my support throughout this project and always challenged and pushed me to succeed. To April Quileza and Yanglyn Ou, you guys have always been there for me to help review my work and become my personal cheerleaders when things got hectic. I would like to acknowledge the founders of Ansca Mobile, Carlos Icaza and Walter Luh for giving Corona developers an amazing platform to work with. Big thanks to David Barnes, Robin de Jongh, Chris Rodrigues, Alka Nayak, Manasi Poonthottam, Steven Wilding, the reviewers, and the rest of the Packt staff for giving me this opportunity to share my love and knowledge of mobile game development in this book.
Mitch Allen has worked on software projects for Lotus, IBM, Dragon, Nuance, Yahoo, and Intuit. He specializes in everything from Enterprise Web Apps to Mobile. He currently lives and works in Silicon Valley. For more information, please visit his website at: http://mitchallen.com.
Nicholas Golden can be found coding furiously away working on dozens of games and applications simultaneously. He is also heavily involved with helping others out and will go out of his way to assist. He is an accomplished professional with a jack of all trades, master of a few approach in key areas such as music composition and games, specifically game development. His thinking is extremely eccentric but focused, abstract but concentrated which results in approaching problem solving in non-standard ways while avoiding the status quo.
Nicholas Golden runs an indie development studio, information regarding his adventures can be found at: www.NicholasGolden.com and www.AngryCarrot.com.
I would like to thank my wife Michelle and my baby boy Ryan (3 months old at the time of writing), for putting up with my ramblings and late-night reviewing while being overly excited about the material. I am always pitching insane ideas at least dozens of times a day which amounts to me having so much work to do, it would literally take me 30 lifetimes to finish!
I would also like to of course thank Michelle Fernandez who let me in on this Corona SDK book in the first place. What started out as a curiosity on the Anscamobile.com forums, turned into an opportunity to communicate with a talented writer and review this fantastic book. I am sure you, the reader will enjoy this book as much as I did reviewing it!1!!11!11! (The 1s are on purpose and signify importance to a statement, ZoMg don't you know this?)
I would also like to give a shout out to CARLOS THE MAN over at Anscamobile.com for the Corona SDK! Without that, there would be no book! Carlos is FTW!
Alan Grace is a co-founder of Pixel Wolf Studios, an Indie game development studio based in Dublin, Ireland. Having worked for a number of years in Web and graphic design running his own company Alpha Solutions, Alan has a vast area of expertise across multimedia and game design. He recently completed his MSc in Digital Games and set up Pixel Wolf Studios moving his focus into game development for the iOS and Google Play Store.
Pixel Wolf Studios was established in 2011 and currently has three titles in development for the iOS and Google Play Store. All three are due for release in 2012 and will be developed using Corona SDK. For more information visit www.pixelwolfstudios.com.
Clement Ho is currently pursuing an undergraduate degree in Computer Science from Abilene Christian University. He has a strong passion for mobile and web development which has led him to pursue multiple freelance and independent projects. He has developed for the iOS and BlackBerry platform using Corona SDK, xCode and Adobe Air technologies.
Steve Hoffman (also known as Captain Hoff) is a high tech serial entrepreneur. He's currently involved in Playkast, a stealth startup that will be launching a new type of entertainment platform later this year.
Hoffman is a founding member of the Academy of Television's Interactive Media Group and was the Chairman and Founder of the San Francisco Chapter of the Producers Guild. He is also as serving on the Board of Governors for the New Media Council.Hoffman and co-founded LavaMind, which publishes Founders Space, as well as numerous websites, games and mobile applications.
In 2007, Hoffman founded RocketOn, a social media and virtual world startup. Prior to this, Hoffman was the COO of Tap11 (formerly Zannel), a venture-funded startup that provides businesses with advanced analytics and CRM for Twitter.
Before that, Hoffman was the North American Studio Head for Infospace, where he ran the US mobile games publishing and development group. While in Los Angeles, Hoff was the Chairman and CEO of Spiderdance, Hollywood's leading interactive television studio, whose customers included NBC, Viacom, Time Warner, TBS, GSN and A&E. He also consulted for R/GA Interactive, designing projects for Kodak, Disney, Intel, AdAge, and the Children's Television Workshop.
In Japan, Hoffman worked as a game designer for Sega, generating new concepts and designs for games and amusement rides. Prior to Sega, he was a Hollywood development executive at Fries Entertainment, where he managed TV development. He graduated from the University of California with a BS in Electrical Computer Engineering and went on to earn a Masters in Film and Television from USC. He is also the co-author of the first-edition of Game Design Workshop published by CMP.
Predrag is a renaissance man who is interested in a number of different subjects. His primary areas of interest are programming and digital and traditional art. He also likes to spend his free time painting and exploring robotics and interactive design. In the last 12 years, Predrag has worked as a Technical and Art Director on many interactive projects, published over 40 online games, and has participated in a production of several iPhone projects. He has a strong background in ActionScript, MEL script, and Lua. Predrag is a member of MENSA International and has a number of professional certificates in different areas.
Walter is the creator of Corona SDK.
In a former life, he led the Adobe Flash Lite engineering team as lead architect. He has a track record of shipping successful products from mobile runtimes like Flash Lite to industry-standard desktop products such as Adobe Illustrator and Apple's Final Cut Pro.
Walter holds an undergraduate degree in Physics from Swarthmore College, and a master's degree in computer science from Stanford University.
Edgar Miranda has been developing games since 2006, everything from simple action games, multiplayer turn base games, social games to mobile games. He's had several years of experience as a consultant working with technologies like Flash, Flex, and Corona SDK. He is the founder and owner of Pie Hammer Games (www.piehammer.com), which is focused on building mobile games for iOS and Android. You can check out his blog at: www.edgarmiranda.net.
I'd like to thank my parents for always supporting my dream of making games for a living and all the people out there who support me by playing my games.
Jordan Schuetz is currently a 17 year old high school student who will be attending college in the fall of 2012. Jordan has been developing with Corona from the age of 16 and has had success with the development software ever since.
He is the founder of Ninja Pig Studios which creates games and applications for mobile cellular devices. Ninja Pig Studios has released some very successful applications on iPhone, Android, Nook, and Kindle. Their most popular application is called Gun App which reached the fourth spot in the Entertainment category on Nook. Annoying Pig Game was another hit which reached spot 34 in the Top New Free Games on the Google Play Store. The game is currently available for iPhone, Android, Nook, and Kindle.
In addition to owning his own company, Jordan has also been a great contributor to the Ansca Mobile community. He has become a Corona Ambassador and has been featured as a Case Study on the Ansca Mobile website.
Jordan has also developed a mobile application for an online shopping website called WeirdStuff which is a computer electronics store in the Silicon Valley. Jordan will be studying Computer Engineering in college and will continue working on application development throughout his college career.
Jayant Varma has been the IT Manager for BMW and Nissan dealership in Asia and the Middle East, an Academic lecturer in Australia before being a Technopreneur. With his diverse knowledge in the industry, he has founded OZ Apps a consultancy, specializing in mobile development, training and consulting services globally. He has a master's degree in Business and IT and is well versed with and has developed a wide range of languages including Assemble, dBase, Clipper, Pascal, C, C++, Obj-C, java, Visual Basic, and Lua to name a few. He has run workshops and has been a speaker for the AUC (Apple University Consortium), ACS (Australian Computer Society), and other organizations. He runs a blog, howto.oz-apps.com, that helps developers with tutorials on using lua-based frameworks for mobile development and review me.oz-apps.com that reviews software. He has to his credit over a dozen apps on the app store both personal and contractual that have been in the charts.
You might want to visit www.PacktPub.com for support files and downloads related to your book.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
http://PacktLib.PacktPub.com
Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books.
If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.
This book is designed to introduce you to the basic standards of using the Corona SDK across iOS and Android platforms. You will enhance your learning experience by building three unique games in easy-to-follow steps. Apart from developing games, you will also dive into learning about social network integration, In-App Purchasing, monetizing, and shipping your applications to the App Store and/or Google Play Store.
Chapter 1, Getting Started With Corona SDK, begins by teaching users how to install Corona SDK on both Mac OSX and Windows operating systems. You will learn how to create your first program in just two lines of code. Lastly, we'll go through the process of building and loading an application to an iOS or Android device.
Chapter 2, Lua Crash Course and the Corona Framework, dives into the Lua programming language that is used to develop in Corona SDK. We'll go over the basics of variables, functions, and data structures in Lua. This chapter will also introduce how to implement a variety of display objects within the Corona framework.
Chapter 3, Building our First Game: Breakout, discusses the first half of building your first game, Breakout. We'll learn how to structure game files in a Corona project and create game objects that will display on screen.
Chapter 4, Game Controls, continues on with the last half of building your first game, Breakout. We'll cover game object movement as well as collision detection between objects in the scene. You will also learn how to create a score system that will implement win and lose conditions of the game.
Chapter 5, Animating our Game, explains how to animate a game using movie clips and sprite sheets. This chapter will go in depth with managing motion and transitions while creating a new game framework.
Chapter 6, Playing Sounds and Music, provides information on how to apply sound effects and music to your applications. It is vital to include some type of audio to enhance the sensory experience of our game's development. You will learn how to incorporate audio through loading, executing, and looping techniques with the Corona Audio System.
Chapter 7, Physics: Falling Objects, covers how to implement the Box2D engine in Corona SDK using display objects. You will be able to customize body construction and work with physical behaviors of falling objects. In this chapter, we'll apply the uses of dynamic/static bodies and explain the purpose of post-collisions.
Chapter 8, Operation Storyboard, discusses how to manage all your game scenes with Storyboard API. We'll also go into detail on menu design, such as creating a pause menu and main menu. In addition, you'll learn how to save high scores within your game.
Chapter 9, Handling Multiple Devices and Networking your Apps, provides information about integrating your applications with social networks such as Twitter or Facebook. Other social capabilities that will be discussed are incorporating achievements and leaderboards with OpenFeint. This will enable your app to reach a bigger audience globally.
Chapter 10, Optimizing, Testing, and Shipping your Games, explains the application submission process for both iOS and Android devices. This chapter will guide you on how to set up a Distribution Provisioning Profile for the App Store and manage your app information in iTunes Connect. Android developers will learn how to sign their applications for publication so it can be submitted to the Google Play Store.
Chapter 11, Implementing In-App Purchases, covers monetization of your game by creating consumable, non-consumable, or subscription purchases. You will apply In-App Purchases in the App Store using Corona's store module. We'll take a look at testing purchases on a device to see if transactions have been applied using the Sandbox environment.
You will need the following items before you can start developing games with Corona SDK for Mac:
You will need the following items before you can start developing games with Corona SDK for Windows:
You must be enrolled as an Google Play developer if you want to submit and publish apps for Android devices.
The game tutorials require resource files that are available with this book and can be downloaded from the Packt website.
Lastly, you will need the latest stable build of Corona SDK: Version 2011.704. This is applicable for test drivers and subscribers.
This book is for anyone who wants to have a go at creating commercially successful games for Android and iOS. You don't need game development or programming experience.
In this book, you will find several headings appearing frequently.
To give clear instructions of how to complete a procedure or task, we use:
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.
To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title via the subject of your message.
If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail <[email protected]>.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/support, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.
Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]> with a link to the suspected pirated material.
We appreciate your help in protecting our authors, and our ability to bring you valuable content.
You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.
Before we jump right into coding some simple games, we need to install and run the necessary programs that will make our applications come to life. Corona SDKis primarily a 2D development engine. If you've had experience developing for iOS or Android, you will find the experience working with Corona refreshing and simple to use. In no time you'll be creating finished products to distribute on the iPhone App Store and Google Play Store.
In this chapter, we will:
You have the option of choosing the Mac OS X or Microsoft Windows operating system to develop on. Keep in mind the following system requirements required to run the program.
If you are installing Corona for Mac OS X, be sure that your system is as follows:
If you are running Microsoft Windows, be sure that your system is as follows:
Let's begin by setting up Corona SDK on our desktop.
Once it is loaded, you should see the mounted disk image folder as shown in the following screenshot:
Next, drag the CoronaSDK folder into theApplications folder. This will copy the contents of the Corona folder into /Applications. You will be prompted to enter an administrator password if you are not the main administrator of the account. You will be able to see the CoronaSDK folder in /Applications once it has been successfully installed.For easy access to the folder's contents, create an alias by dragging the CoronaSDK folder to the Dock of your Mac desktop.
First time Corona SDK users will have to perform a quick and easy one-time authorization process before it canbe accessible.
You must be connected to the Internet to complete the authorization process.
If you register with Corona as a trial user, there is no fee to develop on iOS and/or Android devices. Developers that would like to publish their applications on the App Store or Google Play need to purchase the Corona SDK subscription on the website in order to do so at: http://www.anscamobile.com/corona/.
Setting up Corona SDK on your Mac operating system is as simple as installing any other dedicated Mac program. After you have authorized the SDK on your machine and logged in with your e-mail and password it will be ready for use. From here on out, every time you launch Corona, it will automatically log in to your account. You will notice that you are greeted with a Corona SDK screen when this happens.
Let's set up Corona SDK on our desktop.
The process to activate the SDK should be the same as that of the Mac procedure once you launch Corona for the very first time.
If you run into issues with images not displaying properly, check to see if you're using an up-to-date OpenGL graphics driver that is 1.3 or higher.
Be aware that Corona SDK on Windows can only build for Android devices, not for iOS devices (iPhone, iPad, or iPod Touch). A Mac can only build for iOS devices in Corona but can also build applications for Android.
Installing the SDK on Windows has a different setup to that of the Mac OS X. While executing the installer file, Windows will automatically present a designated location to install the application such as the Programs folder so you won't have to manually choose a destination. Upon a successful installation you will see the Corona SDK icon on your desktop for easy access or highlighted in the Programs list in the Start menu, assuming you're accessing it for the first time. When you authorize Corona on your machine and sign in with your login information, it is ready for you to use and will lo in automatically every time it is launched.
On Mac OS X, launch Corona SDK by either selecting CoronaTerminal or CoronaSimulator from the ApplicationsDirectory. Both selections will access the SDK. The CoronaSimulator will only open the simulator. The CoronaTerminal will open both the simulator and terminal window. The terminal is helpful for debugging your programs and displays simulator errors/warnings and print() messages.
On Microsoft Windows, select the Corona SDK folder and click on CoronaSimulator from the list of Programs in your Start menu or double-click the Corona icon on the desktop. (The simulator and terminal are always opened together if you are using Windows).
Let's go over the useful contents contained in the Corona SDK folder (located in Applications/Corona SDK on Mac and Start/All Programs/Corona SDK on Windows:
The Corona SDK window opens by default when you launch the simulator. You can open a Corona project in the simulator, create a device build for testing or distribution, and view soe example games and apps to get you familiar with the SDK.
Let's take a look at the HelloPhysics sample project in the simulator:
Accessing the SDK through the Corona Terminal or Corona Simulator depends on your preference of action. Many Mac users prefer to use the Corona Terminal so they can track messages outputted in the terminal, especially for debugging purposes. When you launch the SDK through the Corona Simulator, the simulator will display, but not the terminal window. When Windows users launch the CoronaSimulator, it will display both the simulator and terminal windows. This is nice to use when you want to play around with any of the example applications that are provided by Corona.
The main.lua file is a special filename that tells Corona where to start in a project folder. This file can also load other code files or other program resources such as sounds or graphics.
When you launch the HelloPhysics application in Corona, you will observe a box object fall from top of the screen in the simulator and collide with a ground object. The transition from launching the main.lua file to viewing the results in the simulator are almost immediate.
As you start getting familiar with the CoronaSimulator, whether you're in Windows or Mac OSX, a default device is always used when you launch an application. Windows uses the Droid as the default device, while the Mac OS X uses the regular iPhone. Try launching the sample code on a different device shell to view the difference in screen resolution in all the devices the simulator has available.
When porting builds to more than one platform, you'll have to consider the variety of screen resolutions in both iOS and Android devices. A build is a compiled version of all your source code converted into one file. Having your game build configured for multiple platforms broadens the audience reach for your application.
Corona does not have a designated program editor to code in so you will have to find one that suits your needs.
For Mac OS, TextWrangler is a good one and free too! You can download it at http://www.barebones.com/products/textwrangler/download.html. Other text editors such as BBEdit at http://www.barebones.com/thedeck and TextMate at http://macromates.com/ are great, but you will need to purchase those in order to use them. TextMate is also compatible with the Corona TextMate Bundle: http://www.ludicroussoftware.com/corona-textmate-bundle/index.html.
For Microsoft Windows Notepad++ is recommended and can be downloaded at http://notepad-plus-plus.org/ or Crimson Editor at http://www.crimsoneditor.com/. Both editors are free.
Any text editor, such as TextEdit for Mac or Notepad for Windows, that is already included in each operating system works as well, but it'll be easier to work with one that is designed for programming. For Corona, using an editor that supports Lua syntax highlighting will work the best when coding. Syntax highlighting adds formatting attributes to keywords and punctuation in a way that makes it easier for the reader to separate code from text.
Apple's developer kit Xcode or the Android SDK is not necessary to download if you only want to use the Corona Simulator. In order to build and test your code on an iOS device (iPhone, iPod Touch and iPad), you will need to sign up as an Apple Developer and create and download provisioning profiles. If you want to develop on Android, you don't need to download the Android SDK unless you want to use the ADB tool to help with installing builds and viewing debug messages.
The trial version of Corona Simulator allows building Adhoc (for iOS) and Debug builds (Android) for testing on your own devices. The builds will contain a Trial User message box when the app runs on the device. You will need to purchase a Corona Subscription if you want to build for Apple's App Store or Android's Google Play Store. Corona Subscribers also get the benefit of subscriber-only features such as access to Daily Builds, Subscribers Only areas on the Corona forums and LaunchPad.
In order to develop any iOS application you will need to enroll in the Apple Developer Program, which costs $99 a year, and create an account on the Apple website http://developer.apple.com/programs/ios/.
We just went through the steps of how to install Xcode to Mac OS X. By enrolling in the Apple Developer Program, you will have access to up-to-date development tools on the website. Remember that to continue being an Apple Developer you have to pay a fee of $99 annually to keep your subscription.
The Xcode file is quite large so it will take a bit of time to download depending on how fast your Internet connection is. When your installation is complete, Xcode will be ready to go.
Before we dive into more complex examples, let's alter some minor things in your program.
