"Simon says the color" is an app for iPhone developed by Silvio Daminato and Andrea Giavatto for the Wireless Networks project in Spring 2010. It is a game in which a player must take a picture with a certain color in the middle of the screen.
The main features of this app are described and some code example are shown.
1. iPhone project
Wireless Network
Silvio Daminato
February 28, 2011
2. Project: Simon says the color
⢠Kids game: âStrega comanda coloreâ ported to iPhone
⢠iPhone asks for a color
⢠player takes a picture with that color in the center
⢠Simple augmented reality
⢠File handling
⢠Bluetooth
⢠Upload of top scores
3. Single player
⢠Three game modes
⢠Practice
⢠Time attack
⢠Best out of 5
⢠Color randomly chosen between 8 colors
⢠Player has to take the picture within a timeout
4. Multiplayer
â˘A player is the Witch
⢠He/she choose the color
⢠He/she sets the difďŹculty (timeout)
⢠Up to six players
⢠Communication over bluetooth
⢠Client-Server architecture
5. Developing for iPhone
⢠http://developer.apple.com/
⢠Sign up as developer
⢠Download of development and debugging tools
⢠Manage proďŹles, certiďŹcates, devices
⢠http://developer.apple.com/programs/ios/university/
⢠It allows to install and test apps on a iOS device
7. Objective C
⢠Object oriented
⢠C-based
⢠Smalltalk style: based on messages exchange
C++ Obj-C
A* a = new A; A* a = [[A alloc] init];
a -> doSomething(argument); [a doSomething: argument];
delete a; [a release];
8. Delegation
⢠Widely used in Cocoa Touch
⢠An object is delegated by the application to handle some kind
of events
⢠Examples: UITableViewDelegate, UIAlertViewDelegate
â˘A delegate object has to implement speciďŹc methods with
speciďŹc signatures
9. Delegation - example
@interface MyViewController : UIViewController <UITableViewDelegate> {
< MyViewController declarations >
}
@implementation ServerController
...
- (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
< returns the number of rows in the section >
}
- (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
< sets up and returns the cell >
}
...
@end
10. Using the camera
⢠Class UIImagePickerController
⢠Source type UIImagePickerControllerSourceTypeCamera
⢠Delegate protocol UIImagePickerControllerDelegate
⢠Method imagePickerController:didFinishPickingMediaWithInfo: returns
the image
⢠Method imagePickerControllerDidCancel: is called when user
cancels
11. Augmented reality
⢠Create an UIView object or a UIView subclass object
⢠Assign that object to cameraOverlayView property of the
UIImagePickerController
12. Augmented reality
⢠Create an UIView object or a UIView subclass object
⢠Assign that object to cameraOverlayView property of the
UIImagePickerController
Thatâs all.
15. Text ďŹle handling
⢠.plist format
⢠XML with a speciďŹc format
⢠Handling is simple
⢠Set up data, generate path of ďŹle, write to ďŹle
⢠Generate path, read ďŹle
⢠Data as NSDictionary, NSArray, NSString, ...
17. Peer-to-peer over Bluetooth
⢠Allows to exchange information between two or more
devices
⢠Ad-hoc network between peers
⢠Communication is through sessions (Objects that handle
events related to it)
⢠Delegate protocol: GKSessionDelegate
⢠Data format is free
18. Peers
⢠Peers are identiďŹed by peerID
⢠Every peer has a state
⢠GKPeerStateAvailable
⢠GKPeerStateUnavailable
⢠GKPeerStateConnected
⢠GKPeerStateDisconnected
⢠GKPeerStateConnecting
⢠Method session:peer:didChangeState: called when a peer changes
state
19. Discovering peers
⢠Every session implements its speciďŹc service
â˘A session looks for other peer depending on its Session Mode
⢠Server
⢠Client
⢠Peer
⢠Toestablish a connection there must be at least a server that
advertise its service and a client looking for it
20. Implementing a Server
⢠Initialize the session: initWithSessionID:displayName:sessionMode:
⢠Session mode GKSessionModeServer or GKSessionModePeer
⢠Set property available = YES to advertise the service
⢠The service has its own Id
⢠Method session:didReceiveConnectionRequestFromPeer: notiďŹes a
connection request
⢠Server decides whether to accept the request or not
⢠When the session is created session:peer:didChangeState: method is
called
22. Implementing a Server - example
- (void) session:(GKSession *)session peer:(NSString *)peerID didChangeState:
(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateConnected:
NSLog(@"Peer %@ connected!", peerID);
connections_count ++;
break;
case GKPeerStateDisconnected:
NSLog(@"Peer %@ disconnected!", peerID);
connections_count --;
break;
}
}
23. Connecting to a service
⢠Initialize the session: initWithSessionID:displayName:sessionMode:
⢠Session mode GKSessionModeClient or GKSessionModePeer
⢠Set property available = YES to look for the service
⢠Only service with the same sessionID are found
⢠Method session:peer:didChangeState: called when a server has been
found
⢠Method connectToPeer:withTimeout: to request connection
⢠Method session:peer:didChangeState: called when the session has
been created
24. Connecting to a service -
example
- (GKSession *) initSession {
GKSession *session = [[GKSession alloc] initWithSessionID:@"Servizio"
displayName:@"Client"
sessionMode:GKSessionModeClient];
session.delegate = self;
[session setDataReceiveHandler: self withContext:nil];
session.available = YES;
return session;
}
- (void) session:(GKSession *)session peer:(NSString *)peerID didChangeState:
(GKPeerConnectionState)state {
switch (state) {
case GKPeerStateAvailable:
NSLog(@"Trovato servizio!");
[session connectToPeer:peerID withTimeout:5.0];
break;
case GKPeerStateConnected:
NSLog(@"Connessione avvenuta!");
session.available = NO;
break;
case GKPeerStateDisconnected:
NSLog(@"Disconnesso");
break;
}
25. Exchanging data
⢠Connected peers can exchange data
⢠Method sendDataToAllPeers:WithDataMode:error: sends to all peers
⢠Method sendData:toPeers:WithDataMode:error: sends to some peers
⢠Dataformat is not ďŹxed, but they have to be encapsulated in
an NSData object
26. Exchanging data - 2
⢠Two alternative dataModes:
⢠GKSendDataReliable
⢠Datais retransmitted if it doesnât reach destination
⢠Messages are received in the same order they were sent
⢠GKSendDataUnreliable
⢠Data is sent only once
⢠Method receiveData:fromPeer:inSession:context: to receive data
⢠Method setDataReceiveHandler:withContext: sets the object that
handles received data
28. Disconnecting peers
⢠End a session: disconnectFromAllPeers
⢠Disconnect a peer: disconnectPeerFromAllPeers:
⢠Ifa peer is non responsive for a period of time
(disconnectionTimeout) it is automatically disconnected
⢠Method session:peer:didChangeState: called when a peer
disconnects
29. Peer picker
⢠It is possible to create your own GUI
⢠Object provides the interface to discover
GKPeerPickerController
and connect to other peers
⢠Delegate protocol GKPeerPickerControllerDelegate
30. Help and documentation
⢠Xcode menu bar â Help â Developer documentation
⢠http://developer.apple.com/library/ios/navigation/
⢠http://www.google.com/
⢠http://stackoverďŹow.com/
⢠http://www.iphonedevsdk.com/forum/
31. References
⢠âSimon says the colorâ, S. Daminato, A. Giavatto, Progetto di
Reti Wireless 2009/2010
⢠http://developer.apple.com/library/ios/navigation/
⢠âGame Kit Programming Guideâ, Apple Inc.