iPad and iPhone game programming requires several special elements. These essential steps give your program the action and interactivity that today's gamers demand, regardless of the platform where they play.
>
>
Building a Game Loop in iPhone and iPad Game Development
A game loop gets your code to update the game world on your iPhone or iPad many times a second — fast enough to make it look like fluid motion to the player.
To do this, you need to update the position of all your views every time the screen is redrawn by iOS. To be notified of when this happens, you need to use a CADisplayLink class. CADisplayLink is an object that, when you create it, sends your class a message just before the screen is about to update. This gives you the opportunity to move your views around.
To set up a CADisplayLink.
Create a method that gets called when the screen updates; make the method look like this:
(void) update:(CADisplayLink*)displayLink {
}When your view is loaded, create and prepare a CADisplayLink object by adding this code to the viewDidLoad: method in your view controller’s .m file:
displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(update:)];
[displayLink addToRunLoop:[NSRunLoop mainRunLoop]
forMode:NSRunLoopCommonModes];
When your view loads, you receive the update: message many times a second, which is your opportunity to update the game world.
>
>
>
Setting Up a Nib for your iPhone or iPad
Nib files end in .xib and are used in Interface Builder to construct the screens of your game application for your iPhone or iPad.
To start building a nib file, double-click it in Xcode to open the file in Interface Builder. You can then drag views and controls, such as images and buttons, into the nib file.
To make your code aware of when the user taps buttons on your screen, follow these steps:
For each button on your screen, add your action methods to the .h file for your view controller.
Action methods look like this:
- (IBAction) doSomething;
Connect the button to the nib by following these steps:
Go back to the nib file in Interface Builder, and open the Document Window by pressing Command+0.
You see a file’s owner entry at the top of the list.
Hold down the Control key, and click and drag from the button onto the file’s owner.
A list of action methods you can attach the button to appears.
Click the method you want.
Add the method code that runs when the button is tapped to the .m file for your view controller.
This code looks something like this:
- (IBAction) doSomething {
// This code is run when the button is tapped
}
You might also want your code to be able to refer to the views in your nib. To do this, you need to have an outlet variable for each view you want to refer to.
To set up the outlet variable, follow these steps:
Add an outlet variable to store the reference to the control and put it in the .h file for your view controller, between the curly braces.
Outlet variables look like this:
IBOutlet UIButton* myButton;
Connect the control to the outlet variable by opening the Document Window, holding down the Control key, and dragging from the File’s Owner onto the control.
Now whenever you use the myButton variable, you work with the control you connected it to in the nib.
>
>
>
When to Use Subclassing in iPhone and iPad Game Development
Subclassing is one of the mechanisms you use to customize behaviors while you develop your iPhone or iPad game. Subclassing involves the following two stages:
Creating a new class, dubbed a subclass, that inherits properties from another (super) class
Adding properties as needed for your iPhone application
In general, you want to subclass
UIView: To create your (more complex) content views, which you may fill with controls, graphics, and the like
UIViewController: To manage the content views and connect it to the model
NSObject: To create Model views and delegates
>
>
>
Using Block Objects in iPhone and iPad Game Development
Blocks are an extension of the C language and are fully supported by Objective-C, the underlying programming language for all things Mac. In the new iOS 4, blocks are used more and more instead of (or to replace)
Delegates and delegate methods
Callback functions
Completion handlers for one-time operations
Other enumeration techniques
Asynchronous tasks that need performed
With iOS 4, the following methods and functions of the system frameworks take blocks as parameters:
Completion handlers
Notif handlers
Enumeration
View animation and transitions
Sorting
Blocks can be declared as variables, but you can also write a block literal inline where it's required as an argument.
To declare a block variable:
Use the ^ operator with the name of the variable.
For example, to declare a block variable that returns void and takes an NSNotification * as its single argument, do the following:
void (^keyBoardWillShow) (NSNotification *)
Just as with any other variable declaration (such as int i = 1), follow the equals sign with its definition.
You do that by using the ^ operator again to indicate the beginning of the block literal — the definition assigned to the block variable. The block literal includes argument names as well as the body (or code) of the block. For example, to inform the compiler that the name of the argument is notif, do the following
= ^(NSNotification * notif) { code
And then end the block literal with the usual
};
Use the block variable, as you would any other variable, as an argument in a message:
usingBlock:keyBoardWillShow
To write a block literal inline, use the ^ operator to indicate the beginning of the block literal — the definition assigned to the block variable. The block literal includes argument names as well as the body (code) of the block.
For example, to write a block literal inline where it's required as an argument for usingBlock, do the following:
usingBlock:^(NSNotification * notif) { code
And then end the block literal with the usual (no semicolon is needed because it is being used as an argument within a message)
}
>
>
dummies
Source:http://www.dummies.com/how-to/content/iphone-and-ipad-game-development-for-dummies-cheat.navId-323193.html
No comments:
Post a Comment