Contents



Welcome to Codea Tutorials, brought to you courtesy of Reefwing Software. If you are interested in developing applications for the iPad, on the iPad, then you have come to the right place. The distinction between a Tutorial and Interlude is arbitrary, generally the Tutorials are a fulsome description of a concept or pattern, while the Interludes are an aside or provide supplementary information. Feel free to dip in and out of the areas that interest you. The first paragraph of each Tutorial / Interlude is shown below, to read more just click or tap on the relevant title...
  

Tutorials


Tutorial 1 - Getting Started

To start you need to purchase the Codea App and load it on your iPad. I'm using an original iPad, so any of the later editions should just be quicker and shinier. You can get the App from iTunes here or just search for Codea in the App store...

Tutorial 2 - Creating a Rounded Rectangle

Ok so our aim in these tutorials is to create code and classes that can be reused. We are working up to a standard menu class which can be used in your programs. But we need to build some foundations first, and the good people at Two Lives Left have provided most of the sample code we need...

Tutorial 3 - A Simple Button Class

A control that you will use frequently is a button. Now that we have the foundations sorted we can punch out a simple button quite easily. Mostly because a sample button class is provided with Codea...

Tutorial 4 - A Splash Screen

Personally we like adding a splash screen to our Apps. However before starting todays tutorial we should point out Apple's position on this...

Tutorial 5 - Finite State Machines

Just about every game includes some sort of Finite State Machine (FSM). It is the Developers go to structure for keeping track of what state the game is in and can be used to simulate intelligent behaviour...

Tutorial 6 - A MineSweeper Game Part 1

To further illustrate the many uses of Finite State Machines (FSM) we will deconstruct a simple game called MineSweeper. This game will illustrate a bunch of new concepts including recursive coding and closures as well as using a lot of the code that we developed in early tutorials...

Tutorial 7 - A MineSweeper Game Part 2

In Part 1 of this tutorial we covered elements of game design. We will now get much closer to the metal and look at the key parts of the code for MineSweeper. Between tutorials we updated the functionality to include High Scores. There aren't many games which don't have this and they are one method of adding longevity to your App (a subject for a later tutorial)...

Tutorial 8 - A Directional Pad (dPad) Class

In Interlude 9 we looked at moving an object on the screen using 4 buttons. This is such a common requirement for games that the directional pad evolved and is now a ubiquitous part of any console controller. While this is not always the best control mechanism for touch screen device it has the advantage of being intuitive and simple...

Tutorial 9 - Dependencies and Libraries

Version 1.4.3 of Codea introduced the concept of Dependencies. This feature is used to address the problem that over time there are certain functions or classes that you use again and again but you don't want to have to re-type these in each project... 

Tutorial 10 - A Simple Dial Class

This tutorial is based on some code that we ported from our iPhone App Personality Profile. In this App you use a dial to answer a number of questions and a profile is provided based on your selection. We used a dial because we wanted a touch friendly solution and this seemed like an intuitive approach...

Tutorial 11 - Physics 101

We are constantly amazed by the capability of Codea. An example of this is the integration of the Box2D physics engine. The talented folks at Two Lives Left have made incorporating physics into your App absurdly easy...

Tutorial 12 - Submitting to the Apple App Store Part 1

For many people the whole point of writing applications is to see them published in the App store and available for download from iTunes. Before you quit your job and go out and buy matching Porsche 911's you would be wise to get a few months of sales data under your belt...

Tutorial 13 - Submitting to the Apple App Store Part 2

Further to part 1 of this tutorial, there are a couple of things, which if you are not aware of them, can cause problems with running your Codea App in the runtime...

Tutorial 14 - The SpaceWar! Game and a Extended Ship Class

In Interlude 11 we introduced a ship class. We will use this to explore some new concepts in game design...

Tutorial 15 - A* Path Finding

In the previous Tutorial we implemented a very simple path finding algorithm which added or subtracted from our current (x,y) co-ordinates until we ended up at our target co-ordinates. As pointed out at the time, this approach has its limitations (e.g. it can't handle obstacles or differing terrain). There is a better way...

Tutorial 16 - Convert a String to Table and Table to String (Parsing Tables)

In our last tutorial we developed a simple level editor. This is not much use unless you can save and then load the level data produced. Codea provides a function to saveProjectData and one to saveGlobalData(). Since we would like to save our level data from dGenerator and then read it into our game program, saveGlobalData() is the function we need, as saveProjectData() saves data which is only accessible by the program which saved it...

Tutorial 17 - A Simple File Manager for Codea

Tutorial 16 explained the various ways that data can be stored using Codea. In order to facilitate the loading and storing of data for our level generator we created a simple file management class...

Tutorial 18 - Loading and Saving Complex Tables

Tutorial 18 brings together elements from a number of the previous Tutorials. It is an example of where asking the right question makes finding the answer much simpler. Our aim is to be able to load and save level data from our Tower Defence level generator. 

Tutorial 19 - Integrating Game Centre (Part 1)

Game Center is Apple’s social gaming network. Integrating Game Center functionality into your Codea App has to be done in Xcode. There are three main areas that we need to concern ourself with to implement Game Center...

Tutorial 20 - Integrating Game Center (Part 2)

Before getting too much further we need to decide what scores and achievements that you want to have for your game. For MineSweeper we will keep things fairly simple. We will set up a leader board for each game difficulty (easy, medium and hard) and keep track of the following achievements...

Tutorial 21 - Integrating Game Center (Part 3)

Juan Bel√≥n (@juaxix over on the Codea Forums) has already done a lot of the hard work required to get Game Center up and running on your Codea App. We want to be able to submit scores to multiple leader boards (Easy, Medium and Hard) and incorporate achievements so we need to add to the code that Juan has generously contributed. To this end we will fork his Game Center class on GitHub...

Tutorial 22 - Building a Universal App in Codea

A universal application is defined as one targeted at both the iPhone/iPod touch and iPad. If you want to build a universal iOS application then you have to use the runtime and Xcode. There is currently no way to target iPhone development from within Codea...

Tutorial 23 - Implementing iAds with Codea 

Since iOS 4.0 the iAds framework has been available for developers. Using this you can add banner or full screen advertisements to your application. If you are planning to include ads on a screen then you need to ensure that you have left space within your interface to display them. Full screen ads (referred to as interstitial ads by Apple) are only available on the iPad and are only suitable in certain situations, so we wont cover those in this tutorial...

Tutorial 24 - Basic 3D Graphics

Version 1.5 of Codea is a huge update. In addition to camera access, image blend modes and a tween library for simple animation, it includes full access to shaders and a shader editor. This feature gives you full access to GLSL (OpenGL Shading Language) vertex and fragment shaders (which can be used to apply the ripple shader effect shown in Figure 1). To understand how to implement and use shaders we need to take a few steps back and provide some graphical foundations...

Tutorial 25 - A Lua Primer for Codea (Part 1)

This tutorial will try and provide a quick primer on the Lua language which Codea uses. To ensure that Codea does not incur the wrath of Apple, there are some features of the Lua language which have been disabled in Codea. We will discuss these briefly but the list is diminishing with every new release of Codea and there isn't anything which will likely cause you any problems. Lua is a light weight scripting language written in C. By light weight we don't mean that it isn't capable of writing complicated code but that the syntax is stream lined and straight forward. This makes it a great first language to learn... 

Tutorial 26 - RLE Image Compression for Spritely

Todays tutorial is brought to you courtesy of Dermot Balson from the lovely city of Perth (the most remote city on Earth according to Bill Bryson). Dermot noted that Spritely (the image editor which comes with Codea) produces some fairly large and unwieldy image files. Given Dermot's background in compression he immediately worked out a way to dramatically reduce these files in size. Essentially, Dermot creates an array of unique colours, and builds a string for each of them which consist of two-digit cell addresses... 

Tutorial 27 - Codea v1.5.2: Export to Xcode

The team at Two Lives Left continue to innovate. The latest version of Codea includes an export to Xcode function which is so simple to use that you can now submit to the App store without knowing any Objective C at all. Previously, incorporating your Lua code into the Codea runtime was something of an arcane art (see our earlier tutorial on submitting an app to the Apple App Store). Now, it is a matter of a few taps. This latest addition really rounds out Codea as the only development environment that you need...

Tutorial 28 - Codea v1.5.2: Objective C Add On, Audio

As our first example we will demonstrate how to play an mp3 file from within Codea. iOS provides a number of mechanisms for implementing audio playback. The easiest technique from the perspective of the application developer is to use the AVAudioPlayer class which is part of the AV Foundation Framework. Apple recommends that you use this class for audio playback unless you are playing audio captured from a network stream or require very low I/O latency...

Tutorial 29 - Codea v1.5.2: Objective C Add On, iAds

We have previously done tutorials to add Game Center and iAd functionality using the old runtime but with the great new export to Xcode functionality, this is no longer the best way to skin that particular cat. You may want to re-read our previous tutorial on implementing iAds in Codea as we won't cover the same background in this tutorial. We will build on the previous tutorial which uses the audio add on. This has the added benefit of showing how you can stack a number of add ons to the same project...

Tutorial 30 - Codea v1.5.2: Objective C Add On, Game Center

As with the previous tutorial on iAds, we have provided a lot of background on Game Center in these tutorials:
  • Tutorial 19 - Integrating Game Centre (Part 1); 
  • Tutorial 20 - Integrating Game Centre (Part 2); 
  • Tutorial 21 - Integrating Game Center (Part 3).

Have a look at the above to refresh your knowledge on what is required from an iTunes Connect perspective as we will only cover that area briefly in this tutorial...

Interludes

 
Interlude 1 - Display / Sleep and Display Frame Rate

A question on the Codea Forums got me thinking about how you might slow things down if your code is animating too quickly. There is no sleep or delay function in Lua so initially I  thought something like the following would work...

Interlude 2 - Codea Keyboard Buttons and Editing

So for my benefit as much as anyone else I decided to list all of the custom keyboard buttons here and describe what they do. Note that some of these keys are context sensitive and some will pop up an action dialog box...

Interlude 3 - A Fader Class

There will be times when your App needs to transition from one screen to another and effects are useful in letting the user know what is happening. The following is one approach to what transition to use and when...

Interlude 4 - A Rounded Border Class

For our splash screen Tutorial we wanted to include a nice rounded border. To that end the following extends the roundRect() function that we have used in previous tutorials (to create a button). There are much more elegant ways to achieve this (e.g. using a mesh) but this has the advantage of simplicity...

Interlude 5 - A Twinkling Star Field Class

For our Spacewar game we need a background star field. The original coders found that a made up static background was lacking in verisimilitude so they created a new background based on real star charts that scrolled slowly: at any one time, 45% of the night sky was visible, every star down to the fifth magnitude. We won't be doing that...

Interlude 6 - Lua Tables

The table in Lua is a fascinating object, not least because it is the only data structure in Lua. It is very flexible and very powerful and if you are used to array, dictionary and matrix data structures in other languages, it can be tricky to get your head around initially...

Interlude 7 - Recursion and Closures in Lua

Before part 2 of the MineSweeper tutorial, we need to go over two concepts which you may not have come across before, namely recursion and closures. Writing recursive code is a fairly common technique but Lua is the first language that we have come across that uses closures...

Interlude 8 - A Simpler Star Field

If your game is more frame rate critical or you just want a different look then you could use the following function instead of this class. Using this star field effect gives us an extra 4-5 frames per second...

Interlude 9 - Control Object Movement with Buttons

One of the most useful design patterns in game design is being able to move an object on the screen using buttons (up, down, left and right)...

Interlude 10 - Formatting Text in Columns 

One of the areas of functionality that we wanted to add to the game was keeping track of high scores. The code to save and retrieve this was relatively straight forward (and we will cover it in the tutorial) but printing the results in neat columns turned out to be harder than expected...

Interlude 11 - Classes in Lua and Codea

In Object Oriented Programming (OOP) a class is a coding construct used to define and create an object. Our original training was in the days of Procedural Programming, so we have some sympathy for those grappling with classes and other OOP paradigms for the first time. One way to think of a class is that it is the blueprint for an object...

Interlude 12 - MineSweeper Sales

It has been ten days since MineSweeper was released into the wilds of the App Store so I thought I would provide an update on how downloads are going. In summary, downloads are better than I expected, given it is a clone and there are a bunch of established clones already in the store...

Interlude 13 - Pixels vs Points in iOS

From iOS 4, dimensions are measured in “points” instead of pixels. All coordinate values and distances are specified using points, which are floating-point values. The measurable size of a point varies from device to device and is largely irrelevant. The main thing to understand about points is that they provide a fixed frame of reference for drawing...