四角が、歩いてジャンプするだけの簡易ゲーム作ってみる
(XcodeのiOS6 iPhone Simulatorで試しています。)
横スクロールでジャンプ、歩く、コイン取るといった要素を実装してみる。
歩くときは、縦方向をスゴく小さくした楕円の軌跡で足を動かす。
ジャンプは、固定の距離上昇後、落下、ボタンを続けて押せば二段
というような感じ。
サンプルコード
#import “ViewController.h”
#import <QuartzCore/QuartzCore.h>
typedef enum {
KakuStatusStop,
KakuStatusWalk,
KakuStatusJump,
KakuStatusUp,
KakuStatusDown,
KakuBody,
KakuRFoot,
KakuLFoot,
} KakukakuTag;
@interface ViewController () {
UIView *kakukaku;
UIView *floor;
NSMutableArray *balls;
NSTimer *timer;
}
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
[self createBack];
[self createKakukaku];
[self createConsole];
[self start];
}
– (void)createBack
{
floor = [[UIView alloc] initWithFrame:CGRectMake(0, 250, 320, 100)];
floor.backgroundColor = [UIColor brownColor];
[self.view addSubview:floor];
for (int i=0; i<8 ; i++) {
CALayer *l = [[CALayer alloc] init];
l.frame = CGRectMake(i*50, 0, 2, 100);
l.backgroundColor = [UIColor darkGrayColor].CGColor;
l.transform = CATransform3DMakeRotation(M_PI * 0.1, 0, 0, 1);
[floor.layer addSublayer:l];
}
}
– (void)createKakukaku
{
UIColor *baseColor = [UIColor greenColor];
kakukaku = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 50, 50)];
kakukaku.tag = KakuStatusDown;
[self.view addSubview:kakukaku];
UIView *body = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 45, 35)];
body.backgroundColor = baseColor;
body.tag = KakuBody;
[kakukaku addSubview:body];
UIView *foot1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];
foot1.center = CGPointMake(10, 45);
foot1.backgroundColor = baseColor;
foot1.layer.cornerRadius = 5.0;
foot1.tag = KakuLFoot;
[kakukaku addSubview:foot1];
UIView *foot2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 10)];
foot2.center = CGPointMake(40, 45);
foot2.backgroundColor = baseColor;
foot2.layer.cornerRadius = 5.0;
foot2.tag = KakuRFoot;
[kakukaku addSubview:foot2];
UIView *eye = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 10)];
eye.center = CGPointMake(40, 10);
eye.backgroundColor = [UIColor blackColor];
[body addSubview:eye];
}
– (void)createConsole
{
UILabel *jumpBtn = [[UILabel alloc] initWithFrame:CGRectMake(250, 400, 80, 50)];
jumpBtn.text = @”jump”;
jumpBtn.textAlignment = 1;
jumpBtn.layer.cornerRadius = 10.0;
jumpBtn.backgroundColor = [UIColor redColor];
[self.view addSubview:jumpBtn];
jumpBtn.userInteractionEnabled = YES;
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushj)];
[jumpBtn addGestureRecognizer:tap];
}
– (void)pushj
{
kakukaku.tag = KakuStatusJump;
}
– (void)start
{
timer = [NSTimer scheduledTimerWithTimeInterval:2.0/60.0 target:self selector:@selector(updateDisp:) userInfo:nil repeats:YES];
}
– (void)updateDisp:(NSTimer*)sender
{
if (kakukaku.tag == KakuStatusWalk) {
[self walk];
}
if (kakukaku.tag == KakuStatusJump) {
kakukaku.tag = KakuStatusUp;
[self jump];
}
if (kakukaku.tag == KakuStatusDown) {
[self fall];
}
// floor
for (CALayer *l in floor.layer.sublayers) {
float x = l.position.x – 1;
x = (x < –25) ? 370 : x;
[CATransaction begin];
[CATransaction setValue: (id) kCFBooleanTrue forKey: kCATransactionDisableActions];
l.position = CGPointMake(x, l.position.y);
[CATransaction commit];
}
// ボール管理
[self ballManage];
// ボールを取れたか
[self checkBallCatch];
}
– (void)ballManage
{
if (!balls) {
balls = [[NSMutableArray alloc] init];
}
int i = arc4random() % 300;
static int lock = 0;
if (i < 3 && lock == 0) {
lock = 1;
float y = 200 – 50 * i;
UIView *b = [[UIView alloc] initWithFrame:CGRectMake(350, y, 16, 20)];
b.layer.cornerRadius = 8.0;
b.backgroundColor = [UIColor yellowColor];
b.layer.borderColor = [UIColor blackColor].CGColor;
b.layer.borderWidth = 2.0;
[self.view addSubview:b];
[balls addObject:b];
} else if (lock != 0) {
lock++;
if (lock > 50) {
lock = 0;
}
}
for (int i=0; i<[balls count]; i++) {
UIView *v = [balls objectAtIndex:i];
v.center = CGPointMake(v.center.x – 1, v.center.y);
if (v.center.x < 0) {
[v removeFromSuperview];
[balls removeObject:v];
}
}
}
– (void)walk
{
static float angle;
float r = 10.0;
float a = 1.0;
float b = 0.1;
angle += M_PI * 0.1;
// newx = a * r cos(angle)
// newy = b * r sin(angle)
int i = 0;
for (UIView *v in kakukaku.subviews) {
if (v.tag == KakuLFoot || v.tag == KakuRFoot) {
float x = 25 + r * a * cos(angle + M_PI * i);
float y = 40 + r * b * sin(angle + M_PI * i);
v.center = CGPointMake(x, y);
i++;
}
}
}
– (void)jump
{
[UIView animateWithDuration:0.2 animations:^{
UIView *body = [kakukaku viewWithTag:KakuBody];
body.center = CGPointMake(25, 25);
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.5 animations:^{
UIView *body = [kakukaku viewWithTag:KakuBody];
body.center = CGPointMake(25, 15);
kakukaku.center = CGPointMake(kakukaku.center.x, kakukaku.center.y – 100);
} completion:^(BOOL finished) {
kakukaku.tag = KakuStatusDown;
}];
}];
}
– (void)fall
{
kakukaku.center = CGPointMake(kakukaku.center.x, kakukaku.center.y + 5);
if (kakukaku.center.y > 250) {
kakukaku.center = CGPointMake(kakukaku.center.x, 250);
kakukaku.tag = KakuStatusWalk;
}
}
– (void)checkBallCatch
{
CGRect kakuRect = ((CALayer*)kakukaku.layer.presentationLayer).frame;
for (UIView *v in balls) {
if (CGRectIntersectsRect(kakuRect, v.frame)) {
[UIView animateWithDuration:0.5 animations:^{
v.center = CGPointMake(v.center.x, v.center.y – 150);
v.alpha = 0.2;
} completion:^(BOOL finished) {
[v removeFromSuperview];
[balls removeObject:v];
}];
}
}
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end