There are no active ads.

Introduction to iOS Development: Programming Pong (Part 3)

How do you develop apps for iOS? It’s a common question that many computer enthusiasts ask, and one we here at TechnoBuffalo hope to answer with this series of posts.

Learning how to develop software can be one of the most intimidating prospects for the average computer user, and with the growing saturation of applications in mobile marketplaces, it is becoming increasingly difficult to get your work noticed. That’s what this series is for, helping you learn iOS development from a conceptual perspective. No prior knowledge of computer programming will be necessary. Over the coming weeks, we’ll examine the iPhone’s ability to deliver immersive, intuitive content, a unique opportunity for both developers and consumers.

Today will be a continuation of last week’s lesson, recreating the classic game of Pong. When we parted last, we had finished detecting touch interaction and paddle movement. This week, I mulled over a way to make the score-keeping process more efficient, which is what we’ll be focusing on today.

1.  We’re going to begin by streamlining our application by creating custom fonts. To do this, we must delete the IBOutlets that we created in our initialization file so that the only code that appears is this:

——————————————–

#import <UIKit/UIKit.h>

@interface PongViewController : UIViewController {

IBOutlet UIImageView *ball;

IBOutlet UIImageView *playerPaddle;
IBOutlet UIImageView *computerPaddle;

IBOutlet UIImageView *playerScoreText;

IBOutlet UIImageView *computerScoreText;

}

@property (nonatomic, retain)IBOutlet UIImageView *ball;

@property (nonatomic, retain)IBOutlet UIImageView *playerPaddle;
@property (nonatomic, retain)IBOutlet UIImageView *computerPaddle;

@property (nonatomic, retain)IBOutlet UIImageView *playerScoreText;

@property (nonatomic, retain)IBOutlet UIImageView *computerScoreText;

@end

——————————————–

2.  Head on over to your implementation file now (PongViewController.m). Here, we’ll also be simplifying our code in the same way. Delete all of the @synthesize methods that don’t make any logical sense.

3.  Now, we can move on to our interface file. Drag out two UILabels and change their font sizes to white. Simply replace the given text with 0. Change the font size to 36. The font that we’ll end up use will turn out being about 110 x 70. Be sure to align the computer’s score to the right. Double-click on File’s Owner and drag from the computerScoreText to the label on the left and from playerScoreText to the label on the right.

4.  Move back to the implementation file. Underneath the viewDidLoad method add the following code, which will set up our customized font and ensure that our application will function properly.

——————————————–

– (void)viewDidLoad
{
[super viewDidLoad];

[playerScoreText setFont:[UIFont fontWithName:@”kongtext” size:36]];
[computerScoreText setFont:[UIFont fontWithName:@”kongtext” size:36]];

}

——————————————–

5.  Download and install this font – http://www.1001fonts.com/font_details.html?font_id=2610 – to your computer. Drag it into your project’s resources. Go to your application’s property list and add a new field. Select Fonts Added by the Application and then write in the field kongtext.ttf. Build and run your application, the font should be working correctly now.

That’s it for this part of the tutorial. Next week, we’ll be finishing off this application by writing a whole lot of game logic. Until then, feel free to browse previous installments in this series, which all listed below.

Questions?

If you have any questions about the topics discussed in this lesson, feel free to voice them in the comments below. We will do our best to ensure that you have a relatively painless experience developing for iOS. Stay tuned for the next installment of this series, which is released weekly.

To read previous installments from this series, check the links below.