Gaming App3 Using Accelerometer Control

Introduction

This is another very simple article in the gaming app field; here I will use one more new concept of iPhone, the "accelerometer". Using it, we control the iPhone screen without events. Here I will use the touch event of animation to describe the functionality of the app. In this article I will use one custom button "Push" button which is used to bounce a ball and one image view which is used in the code. Here I will make a very simple or interesting game "Bouncing Ball". Here we add one more functionality of the iPhone game accelerometer. Here when we click on the push button, the ball will be bounce for a specified event but the ball will be controlled depending on the coordinates used in the acceleration delegate. Here we add two Objective-C classes of uiview type for adding the functionality of the accelerometer.

The Accelerometer functionality cannot be tested by the simulator. So to test this app we must run this app on a real iOS device. Here I will add animation functionality of the touch event to describe the functionality of the app. 

For more detail use the following.

Step 1

Open Xcode by double-clicking on it.

Step 2

Create a New XCode Project by clicking on it.

Step 3

Now select Single View Application and click on Next.

Step 4

Now provide your Product Name and Company Identifier then click on Next.

Step 5

Select the location where you want to save your project and click on Create.

Step 6

The follpwing is the code:

AppDelegate.h

#import <UIKit/UIKit.h>

@class BallViewController;

@interface BallAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;

@property (strong, nonatomic) BallViewController *viewController;

@end

AppDelegate.m

#import "BallAppDelegate.h"

#import "BallViewController.h"

@implementation BallAppDelegate

- (void)dealloc

{

[_window release];

[_viewController release];

[super dealloc];

}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

sleep(5);

self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];

// Override point for customization after application launch.

self.viewController = [[[BallViewController alloc] initWithNibName:@"BallViewController" bundle:nil] autorelease];

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];

return YES;

}

- (void)applicationWillResignActive:(UIApplication *)application{

// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.

// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.

}

- (void)applicationDidEnterBackground:(UIApplication *)application

{

exit(0);// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.

// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.

}

- (void)applicationWillEnterForeground:(UIApplication *)application

{

// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.

}

- (void)applicationDidBecomeActive:(UIApplication *)application

{

// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.

}

- (void)applicationWillTerminate:(UIApplication *)application

{

// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.

}

@end


ViewController.h

#define degreesToRadians(x) (M_PI * x / 180.0)

#define kUpdateInterval (1.0f/60.0f)

#import <UIKit/UIKit.h>

@interface BallViewController : UIViewController <UIAccelerometerDelegate>

{ IBOutlet UIButton *pushButton;

UIImageView *beachBall;

CGPoint touch1;

CGPoint touch2;

}

@property(nonatomic,strong)UIImageView *beachBall;

@property(nonatomic,strong)IBOutlet UIButton *pushButton;

-(IBAction)pushToStart:(id)sender;

- (void)showjumpingView;

@end

ViewController.m

#import "BallViewController.h"

#import "BallView.h"

@interface BallViewController ()

@end

@implementation BallViewController

@synthesize beachBall;
@synthesize pushButton;

- (void)showjumpingView

{

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationDuration:0.3];

[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

[UIView setAnimationBeginsFromCurrentState:YES];

// execute fall after the animation ended

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(fall:finished:context:)];

beachBall.transform = CGAffineTransformMakeTranslation(0, -100);

[UIView commitAnimations];

[UIView beginAnimations:nil context:nil];

// [UIView setAnimationDuration:0.5];

// [UIView setAnimationBeginsFromCurrentState:YES];

// [UIView setAnimationRepeatCount:1];

// [UIView setAnimationRepeatAutoreverses:YES];

// CGAffineTransform transform2 = CGAffineTransformMakeTranslation(0.0, -100.0);

// [beachBall setTransform:transform2];

// [UIView commitAnimations];

}

- (void)normalStance {

[self.beachBall setImage:[UIImage imageNamed:@"Ball.png"]];

}

- (void)cleanStance {

[self.beachBall setImage:nil];

self.beachBall.animationImages = nil;

}

-(void)fall:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

[self cleanStance];

[self normalStance];

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationCurve: UIViewAnimationCurveEaseOut];

[UIView setAnimationBeginsFromCurrentState:YES];

[UIView setAnimationDuration:0.2];

self.beachBall.transform = CGAffineTransformMakeTranslation(0, 0);

[UIView commitAnimations];

}

-(IBAction)pushToStart:(id)sender{

[self showjumpingView];

}

- (void)viewDidLoad

{

[super viewDidLoad];

[self.view setMultipleTouchEnabled:YES];

self.view.backgroundColor = [UIColor greenColor];

self.beachBall = [[UIImageView alloc] initWithFrame:

CGRectMake(90, 419, 40.0, 40.0)];

// explicitly opaque for performance

self.beachBall.opaque = YES;

[self.view addSubview:self.beachBall];

[self.beachBall setImage:[UIImage imageNamed:@"Ball.png"]];

[self.view addSubview:beachBall];

UIAccelerometer *accelerometer = [UIAccelerometer sharedAccelerometer];

accelerometer.delegate = self;

accelerometer.updateInterval = kUpdateInterval;

[super viewDidLoad];}

- (void) touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event {

[super touchesBegan:touches withEvent:event];

NSArray *allTouches = [touches allObjects];

UITouch *touch = [touches anyObject];

int count = [allTouches count];

if (count == 1) {

if ([touch tapCount] < 2) {

if (CGRectContainsPoint([beachBall frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {

[UIView beginAnimations:@"TouchDownAnimation" context:NULL];

[UIView setAnimationBeginsFromCurrentState:YES];

[UIView setAnimationDelegate:self];

[UIView setAnimationDidStopSelector:@selector(finishedTouchDownAnimation:finished:context:)];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[UIView setAnimationDuration:0.25];

CGAffineTransform transform = CGAffineTransformMakeScale(1.1, 1.1);

beachBall.transform = transform;

beachBall.alpha = 0.85;

[UIView commitAnimations];

}

}

}

if (count > 1) {

touch1 = [[allTouches objectAtIndex:0] locationInView:self.view];

touch2 = [[allTouches objectAtIndex:1] locationInView:self.view];

}

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesMoved:touches withEvent:event];

CGPoint currentTouch1;

CGPoint currentTouch2;

NSArray *allTouches = [touches allObjects];

int count = [allTouches count];

if (count == 1) {

if (CGRectContainsPoint([beachBall frame], [[allTouches objectAtIndex:0] locationInView:self.view])) {

beachBall.center = [[allTouches objectAtIndex:0] locationInView:self.view];

return;

}

}

if (count > 1) {

if ((CGRectContainsPoint([beachBall frame], [[allTouches objectAtIndex:0] locationInView:self.view])) ||

(CGRectContainsPoint([beachBall frame], [[allTouches objectAtIndex:1] locationInView:self.view]))) {

currentTouch1 = [[allTouches objectAtIndex:0] locationInView:self.view];

currentTouch2 = [[allTouches objectAtIndex:1] locationInView:self.view];

CGFloat previousAngle = atan2(touch2.y - touch1.y, touch2.x - touch1.x) * 180 / M_PI;

CGFloat currentAngle = atan2(currentTouch2.y - currentTouch1.y,currentTouch2.x - currentTouch1.x) * 180 / M_PI;

CGFloat angleToRotate = currentAngle - previousAngle;

CGAffineTransform transform = CGAffineTransformRotate(beachBall.transform, degreesToRadians(angleToRotate));

beachBall.transform = transform;

touch1 = currentTouch1;

touch2 = currentTouch2;

}

}

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

[super touchesEnded:touches withEvent:event];

UITouch *touch = [touches anyObject];

if ([touch tapCount] == 2) {

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationBeginsFromCurrentState:YES];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[UIView setAnimationDuration:0.20];

beachBall.center = self.view.center;

beachBall.transform = CGAffineTransformIdentity;

[UIView commitAnimations];

return;

}

}

#pragma mark Animation Selectors

- (void)finishedTouchDownAnimation:(NSString*)animationID finished:(BOOL)finished context:(void *)context {

[UIView beginAnimations:nil context:NULL];

[UIView setAnimationBeginsFromCurrentState:YES];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

[UIView setAnimationDuration:0.25];

CGAffineTransform transform = CGAffineTransformMakeScale(1.0, 1.0);

beachBall.transform = transform;

beachBall.alpha = 1.0;

[UIView commitAnimations];

}

- (void)dealloc {

[beachBall release];

[super dealloc];

}

- (void)didReceiveMemoryWarning

{

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

-(void)accelerometer:(UIAccelerometer *)accelerometer

didAccelerate:(UIAcceleration *)acceleration

{

[(BallView *)self.view setAcceleration:acceleration];

[(BallView *)self.view draw];

}

@end


View.h

#define kVelocityMultiplier

#import <UIKit/UIKit.h>

@interface BallView : UIView {

UIImage *image;

CGPoint currentPoint;

CGPoint previousPoint;

UIAcceleration *acceleration;

CGFloat ballXVelocity;

CGFloat ballYVelocity;

}

@property(nonatomic,retain) UIImage *image;

@property CGPoint currentPoint;

@property CGPoint previousPoint;

@property(nonatomic,retain) UIAcceleration *acceleration;

@property CGFloat ballXVelocity;

@property CGFloat ballYVelocity;

-(void)draw;

@end



View.m

#import "BallView.h"

@implementation BallView

@synthesize image;

@synthesize currentPoint;

@synthesize previousPoint;

@synthesize acceleration;

@synthesize ballXVelocity;

@synthesize ballYVelocity;

/*- (id)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

// Initialization code

}

return self;

}*/

- (id)initWithCoder:(NSCoder *)coder {

if (self = [super initWithCoder:coder]) {

// Initialization code

self.image = [UIImage imageNamed:@"Ball.png"];

self.currentPoint = CGPointMake((self.bounds.size.width/2.0f) +

(image.size.width/2.0f), (self.bounds.size.height/ 2.0f) + (image.size.height/2.0f));

ballXVelocity = 0.0f;

ballYVelocity = 0.0f;

}

return self;

}

- (void)drawRect:(CGRect)rect {

// Drawing code

[image drawAtPoint:currentPoint];

}

-(CGPoint)currentPoint {

return currentPoint;

}

-(void)setCurrentPoint:(CGPoint)newPoint {

previousPoint = currentPoint;

currentPoint = newPoint;

if(currentPoint.x < 0){

currentPoint.x = 0;

ballXVelocity = 0;

}

if(currentPoint.y < 0){

currentPoint.y = 0;

ballYVelocity = 0;

}

if(currentPoint.x > self.bounds.size.width - image.size.width){

currentPoint.x = self.bounds.size.width - image.size.width;

ballXVelocity = 0;

}

if(currentPoint.y > self.bounds.size.width - image.size.width){

currentPoint.y = self.bounds.size.width - image.size.width;

ballYVelocity = 0;

}

CGRect currentImageRect = CGRectMake(currentPoint.x , currentPoint.y,

currentPoint.x + image.size.width,

currentPoint.y + image.size.height);

CGRect previousImageRect = CGRectMake(previousPoint.x , previousPoint.y,

previousPoint.x + image.size.width,

previousPoint.y + image.size.height);

[self setNeedsDisplayInRect:CGRectUnion(currentImageRect,previousImageRect)];

}

-(void)draw{

static NSDate *lastDrawTime;

if(lastDrawTime != nil){

NSTimeInterval secondsSinceLastDraw = -([lastDrawTime timeIntervalSinceNow]);

ballYVelocity = ballYVelocity + -(acceleration.y * secondsSinceLastDraw);

ballXVelocity = ballXVelocity + acceleration.x * secondsSinceLastDraw;

CGFloat xAcceleration = secondsSinceLastDraw * ballXVelocity * 500;

CGFloat yAcceleration = secondsSinceLastDraw * ballYVelocity * 500;

self.currentPoint = CGPointMake(self.currentPoint.x + xAcceleration,

self.currentPoint.y +yAcceleration);

}

[lastDrawTime release];

lastDrawTime = [[NSDate alloc] init];

}

- (void)dealloc {

[image release];

[acceleration release];

[super dealloc];

}

@end


Step 7

Now select the iPhone platform to see output in the Simulator.

Output

Splash in iPhone:

splash-in-iphone.png

Output1 in iPhone:

Output1-in-iPhone.png

Here when we click on the push button, the ball will jump.

Output2 in iPhone:

Output2-in-iphone.png

Output3 in iPhone:

When we play this game in a real device the ball will move depending on the device orientation.

Output3-in-iphone.png

Output4 in iPhone:

Output4-in-iphone.png

Output5 in iPhone:

Output5-in-iphone.png

Output6 in iPhone:

Output6-in-iphone.png

When we click on the push button, the button is also animated.

Output7 in iPhone:

Output7-in-iphone.png


Similar Articles