くらいダンジョンの中で階段を探すシンプルゲームをiPhoneアプリで描いてみます。
動作イメージ
XcodeからiOS7 iPhone Simulatorで動かすとこんな感じになります。
サンプルコード
#import “ViewController.h”
@interface ViewController ()
@property (strong, nonatomic) UIView *dungeon;
@property (strong, nonatomic) UIView *stairs;
@property (strong, nonatomic) UILabel *floorLabel;
@end
@implementation ViewController
– (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
[self createDungeon];
[self createStairs];
[self createLightMask];
[self createLabel];
}
– (void)createDungeon
{
self.dungeon = [[UIView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:self.dungeon];
self.dungeon.backgroundColor = [UIColor brownColor];
UIBezierPath *path = [UIBezierPath bezierPath];
for (int i=0; i<50; i++) {
float y = i * 12;
[path moveToPoint:CGPointMake(0, y)];
[path addLineToPoint:CGPointMake(320, y)];
}
CAShapeLayer *sl = [CAShapeLayer layer];
sl.fillColor = [UIColor clearColor].CGColor;
sl.strokeColor = [UIColor lightGrayColor].CGColor;
sl.lineWidth = 1;
sl.path = path.CGPath;
[self.dungeon.layer addSublayer:sl];
}
– (void)createStairs
{
float x = arc4random() % 200 + 60;
float y = arc4random() % 300 + 100;
self.stairs = [[UIView alloc] initWithFrame:CGRectMake(x, y, 50, 50)];
self.stairs.backgroundColor = [UIColor blackColor];
self.stairs.layer.borderColor = [UIColor grayColor].CGColor;
self.stairs.layer.borderWidth = 2;
[self.dungeon addSubview:self.stairs];
for (int i=0; i<3; i++) {
float x = 14 * i + 5;
float y = 8 * i + 10;
UIView *v = [[UIView alloc] initWithFrame:CGRectMake(x, y, 10, 45 – y)];
v.backgroundColor = [UIColor grayColor];
[self.stairs addSubview:v];
}
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(nextFloor)];
[self.stairs addGestureRecognizer:tap];
}
– (void)createLightMask
{
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0, 0, 50, 0, M_PI*2, YES);
CAShapeLayer *circle = [CAShapeLayer layer];
circle.path = myPath;
circle.position = CGPointMake(200, 400);
self.dungeon.layer.mask = circle;
}
– (void)createLabel
{
self.floorLabel = [[UILabel alloc] initWithFrame:CGRectMake(200, 400, 100, 50)];
self.floorLabel.font = [UIFont boldSystemFontOfSize:40];
self.floorLabel.text = @”B1F”;
self.floorLabel.textColor = [UIColor lightGrayColor];
[self.view addSubview:self.floorLabel];
}
– (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
CGPoint p = [[touches anyObject] locationInView:self.dungeon];
self.dungeon.layer.mask.position = p;
}
– (void)nextFloor
{
[UIView animateWithDuration:0.5 animations:^{
self.dungeon.transform = CGAffineTransformMakeScale(0, 0);
} completion:^(BOOL finished) {
[self.stairs removeFromSuperview];
[self createStairs];
int nextFloor = [[self.floorLabel.text stringByTrimmingCharactersInSet:[NSCharacterSet uppercaseLetterCharacterSet]] intValue] + 1;
self.floorLabel.text = [NSString stringWithFormat:@”B%dF”, nextFloor];
[UIView animateWithDuration:0.5 animations:^{
self.dungeon.transform = CGAffineTransformIdentity;
}];
}];
}
– (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end