田植機にのったおじさんをタッチして、
ぽんぽんと稲を植えていこう。
色がついているところに上手に植えてね!
ポイント
今回は、田植機、稲の二つの画像ファイルを使っています。
iPadのPhotoshopで適当に描いたものです。
NSTimerで田植機の動きを制御して、
上まで来たらゲームオーバーにしています。
環境
今回つくったiPhoneアプリサンプルは、
XcodeのiOS6 iPhone Simulatorで動かしています。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
@interface ViewController () {
NSTimer *timer;
UIImageView *car;
UIImage *greenImg;
UILabel *counter;
NSMutableArray *markers;
int goodCounter;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor brownColor];
[self createMarker];
[self loadGreen];
[self createCar];
[self createUI];
}
– (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
if (!timer) {
[self startTimer];
}
}
– (void)loadGreen
{
greenImg = [UIImage imageNamed:@”green”];
}
– (void)createCar
{
UIImage *carImg = [UIImage imageNamed:@”car”];
car = [[UIImageView alloc] initWithImage:carImg];
car.bounds = CGRectMake(0,0,80, 80);
car.center = CGPointMake(300, 400);
car.userInteractionEnabled = YES;
[self.view addSubview:car];
UIImageView *green = [[UIImageView alloc] initWithImage:greenImg];
green.bounds = CGRectMake(0, 0, 40, 40);
green.center = CGPointMake(80, 50);
green.userInteractionEnabled = YES;
[car addSubview:green];
CABasicAnimation *swing = [CABasicAnimation animationWithKeyPath:@”transform.translation.y”];
swing.repeatCount = 1000;
swing.duration = 0.2;
swing.toValue = [NSNumber numberWithInt:10];
[green.layer addAnimation:swing forKey:nil];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(plant:)];
[car addGestureRecognizer:tap];
}
– (void)plant:(UITapGestureRecognizer*)gr
{
UIImageView *plant = [[UIImageView alloc] initWithImage:greenImg];
plant.bounds = CGRectMake(0, 0, 20, 40);
plant.contentMode = UIViewContentModeScaleAspectFill;
plant.center = CGPointMake(gr.view.center.x+40, gr.view.center.y + 20);
[self.view addSubview:plant];
int next = [[counter.text substringFromIndex:1] intValue] – 1;
counter.text = [NSString stringWithFormat:@”x%d”,next];
//check point
for (UIView *m in markers) {
if (CGRectContainsPoint(plant.frame, m.center)) {
UILabel *good = [[UILabel alloc] init];
good.text = @”good!”;
good.font = [UIFont fontWithName:@”Chalkduster” size:30];
good.textColor = m.backgroundColor;
good.backgroundColor = [UIColor clearColor];
[good sizeToFit];
good.center = plant.center;
[self.view addSubview:good];
[UIView animateWithDuration:0.5 animations:^{
good.transform = CGAffineTransformMakeTranslation(0, –50);
} completion:^(BOOL finished) {
[good removeFromSuperview];
}];
goodCounter++;
}
}
}
– (void)createUI
{
UIImageView *plant = [[UIImageView alloc] initWithImage:greenImg];
plant.frame = CGRectMake(20, 20, 30, 30);
[self.view addSubview:plant];
counter = [[UILabel alloc] initWithFrame:CGRectMake(50, 20, 100, 30)];
counter.font = [UIFont fontWithName:@”Chalkduster” size:20];
counter.text = @”×50″;
counter.backgroundColor = [UIColor clearColor];
[self.view addSubview:counter];
}
– (void)createMarker
{
UIColor *color1 = [UIColor colorWithRed:255.0/255.0 green:12.0/255.0 blue:122.0/255.0 alpha:1];
UIColor *color2 = [UIColor colorWithRed:255.0/255.0 green:94.0/255.0 blue:162.0/255.0 alpha:1];
UIColor *color3 = [UIColor colorWithRed:255.0/255.0 green:243.0/255.0 blue:189.0/255.0 alpha:1];
UIColor *color4 = [UIColor colorWithRed:159.0/255.0 green:222.0/255.0 blue:204.0/255.0 alpha:1];
UIColor *color5 = [UIColor colorWithRed:2.0/255.0 green:211.0/255.0 blue:162.0/255.0 alpha:1];
NSArray *colors = [NSArray arrayWithObjects:color1, color2, color3, color4, color5, nil];
markers = [[NSMutableArray alloc] init];
for (int i=0; i<16; i++) {
float x = (i % 4) * 70 + 50;
float y = 430 – (i / 4) * 100;
UIView *mark = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];
mark.backgroundColor = [colors objectAtIndex:arc4random() % 5];
mark.center = CGPointMake(x, y);
[self.view addSubview:mark];
[markers addObject:mark];
}
}
– (void)startTimer
{
timer = [NSTimer scheduledTimerWithTimeInterval:1.0/60.0 target:self selector:@selector(tick:) userInfo:nil repeats:YES];
}
– (void)tick:(NSTimer*)sender
{
float x = car.center.x – 0.5;
float y = car.center.y;
if (x < 0) {
x = 380;
y = car.center.y – 100;
}
car.center = CGPointMake(x, y);
if (y < 100) {
[sender invalidate];
UIView *bar = [[UIView alloc] initWithFrame:CGRectMake(0, 200, 320, 100)];
bar.backgroundColor = [UIColor blackColor];
bar.transform = CGAffineTransformMakeScale(1, 0);
[self.view addSubview:bar];
[UIView animateWithDuration:0.5 animations:^{
bar.transform = CGAffineTransformIdentity;
}completion:^(BOOL finished) {
UILabel *score = [[UILabel alloc] init];
score.text = [NSString stringWithFormat:@”game over\ngood x %d”, goodCounter];
score.font = [UIFont fontWithName:@”Chalkduster” size:20];
score.textColor = [UIColor whiteColor];
score.backgroundColor = [UIColor clearColor];
score.numberOfLines = 0;
[score sizeToFit];
score.center = CGPointMake(500, self.view.center.y);
[self.view addSubview:score];
[UIView animateWithDuration:0.5 animations:^{
score.center = self.view.center;
}];
}];
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end