SIMPLE 2D CAR GAME USING UIVIEWANIMATION IN IOS

iOS provides some basic cool animations in UIView like fade in, fade out, move, rotate, and transform. And the best part of iOS operating system is that we can do great stuff in couple of codes. In this article we are going to see how to implement a Simple 2D Car Game in iOS. iOS provides API to access device Motion using CMMotionManager class. Using this class you can detect the acceleration of device.


Before getting started you need to know the basic properties of UIView. These properties are going to play a major role in animations 1. center (center point of view) 2. frame (position of rectangle in the super view's coordinate, size, stretching, etc) 3. bounds (rectangle in its own coordinate system, size, stretching) 4. alpha (transparency) 5. transform (rotation, scaling, basically any changes to the 

UIView) We can make a UIView animation using below functions.
[UIView animateWithDuration:.5f animations:^{
}];
[UIView animateWithDuration:.5f animations:^{
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:.5 delay:.5 options:0 animations:^{
} completion:^(BOOL finished) {
}];
[UIView animateWithDuration:.5 delay:.5 usingSpringWithDamping:.5 initialSpringVelocity:20 options:0 animations:^{
} completion:^(BOOL finished) {
}];
Let’s begin our game....!!!! We will go with overview first. 1. Design a play ground 2. Accelerate the car 3. Create and animate the divider lines 4. Create and animate the Opposite Car 5. Detect accident Design a play ground 1. Create a new project with the type of Single View Application 2. And design a play ground environment like the below image with the objects
Road
Road blocks (left, right)
Car

3. Now make an outlet to a Car and Road View

@property (weak, nonatomic) IBOutlet UIImageView *car;
@property (weak, nonatomic) IBOutlet UIView *roadView;
@property (weak, nonatomic) IBOutlet UILabel *scoreLabel;

1 Accelerate the Car We are going to move the car towards all the four directions by using device acceleration. To get the device acceleration you need to create an object reference for CMMotionManager. CMMotionManager has a property accelerometerUpdateInterval to update the accelerate status. And you can add your callback block coding after acceleration get updates by using the method startAccelerometerUpdatesToQueue. This method has a handler with input parameters CMAccelerometerData and NSError  

@interface ViewController (){CMMotionManager *mManager;NSMutableArray *cars;int score;}- (void)viewDidLoad {[super viewDidLoad]; cars = [[NSMutableArray alloc] init];[self detectAcceleration];[NSTimer scheduledTimerWithTimeInterval:.5target:selfselector:@selector(createDivider)userInfo:nilrepeats:YES];[NSTimer scheduledTimerWithTimeInterval:2target:selfselector:@selector(createCars)userInfo:nilrepeats:YES];}//Get acceleration status- (void)detectAcceleration{ [mManager setAccelerometerUpdateInterval:0.01];[mManager startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData *accelerometerData, NSError *error) {//Move Car[self moveCar:accelerometerData];}];}
The CMAccelerometerData contains the acceleration on x, y, z points. When you code the below lines your CAR will react for the Device Motion. You can go forward, backward, left, and right.

Learn More at SIMPLE 2D CAR GAME USING UIVIEWANIMATION IN IOS

Hire developer, designers, and professional SEO consultant on an hourly basis from VelanApps for your custom web & mobile app development and marketing.

Comments