推箱子第一关核心代码
2013-10-17 13:19
204 查看
#import
"GameViewController.h"
#import "cAppDelegate.h"
@interface GameViewController
()
@end
@implementation
GameViewController
@synthesize m_box1;
@synthesize m_box2;
@synthesize m_box3;
@synthesize m_box4;
@synthesize m_target;
@synthesize destinationX;
@synthesize destinationY;
@synthesize ableMoveX;
@synthesize ableMoveY;
@synthesize boxFlag;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom
initialization
//初始化游戏
[self
initGame];
}
return self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading
the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
//初始化箱子的目的地数组,人物和箱子可以移动的坐标数组
-(void)initGame
{
//初始化箱子和人物的位置
m_target.center = CGPointMake(176.5, 275.5);
m_box1.center = CGPointMake(141.5, 240.5);
m_box2.center = CGPointMake(176.5, 240.5);
m_box3.center = CGPointMake(141.5, 275.5);
m_box4.center = CGPointMake(211.5, 275.5);
destinationX =
[[NSMutableArray alloc]init];
destinationY =
[[NSMutableArray alloc]init];
ableMoveX =
[[NSMutableArray alloc]init];
ableMoveY =
[[NSMutableArray alloc]init];
//初始化箱子的目的地x坐标
[destinationX
addObject:[NSNumber numberWithFloat:141.5]];
[destinationX
addObject:[NSNumber numberWithFloat:71.5]];
[destinationX
addObject:[NSNumber numberWithFloat:176.5]];
[destinationX
addObject:[NSNumber numberWithFloat:246.5]];
//初始化箱子的目的地y坐标
[destinationY
addObject:[NSNumber numberWithFloat:345.5]];
[destinationY
addObject:[NSNumber numberWithFloat:240.5]];
[destinationY
addObject:[NSNumber numberWithFloat:170.5]];
[destinationY
addObject:[NSNumber numberWithFloat:275.5]];
//人物和箱子可以移动的x坐标
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:106.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:211.5]];
//人物和箱子可以移动的y坐标
[ableMoveY
addObject:[NSNumber numberWithFloat:310.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:205.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
}
- (IBAction)btnBack:(id)sender {
[cAppDelegate
switchViewController:1];
}
//接收触屏事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取当前应用程序的代理对象
cAppDelegate * nowDelegate = [UIApplication sharedApplication].delegate;
CGPoint touchPoint = [[touches anyObject]locationInView:nowDelegate.m_gameVC.view];
[self moveTarget:touchPoint];
}
//人物和箱子移动
-(void)moveTarget:(CGPoint)touchPoint
{
int w = abs(touchPoint.x- m_target.center.x);
int h = abs(touchPoint.y -m_target.center.y);
if
(w>h) //
left or right
{
if
(touchPoint.x<<span
style="color: #4f8187">m_target.center.x)
{
dir = left;
}
else
{
dir = right;
}
}
else
//up or
down
{
if
(touchPoint.y<<span
style="color: #4f8187">m_target.center.y)
{
dir = up;
}
else
{
dir = down;
}
}
switch (dir) {
case
up:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
}
}
break;
case
down:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
}
}
break;
case
left:
//判断下一个点是否可以走
if ([self judge:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x-70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x-70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
}
}
break;
case
right:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if([self judge:CGPointMake( m_target.center.x+70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x+70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
}
}
break;
}
}
//判断人物或箱子将要移动的坐标是否正确
-(BOOL)judge:(CGPoint)nextPoint
{
for(int
i=0;i<<span style="color:
#4f8187">ableMoveX.count;i++)
{
int w
=abs(nextPoint.x - [[ableMoveX objectAtIndex:i]floatValue]);
int h =
abs( nextPoint.y - [[ableMoveY objectAtIndex:i]floatValue]);
//误差小于10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
return YES;
}
}
for (int i=0; i<<span style="color:
#4f8187">destinationX.count;
i++)
{
int w
=abs( nextPoint.x - [[destinationX objectAtIndex:i]floatValue]);
int h =
abs(nextPoint.y - [[destinationY objectAtIndex:i]floatValue]);
//误差小于10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
return YES;
}
}
return NO;
}
//判断人物的下一个坐标是否有箱子
-(BOOL)judgeBoxExist:(CGPoint)nextPoint
{
int w = abs( nextPoint.x - m_box1.center.x);
int h = abs(nextPoint.y - m_box1.center.y);
//误差小于10
if (w<<span style="color:
#272ad8">10&&h<<span style="color:
#272ad8">10)
{
boxFlag =
1;
return
YES;
}
w = abs(nextPoint.x - m_box2.center.x);
h = abs(nextPoint.y - m_box2.center.y);
//误差小于10
if (w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
2;
return
YES;
}
w = abs(nextPoint.x - m_box3.center.x);
h = abs(nextPoint.y - m_box3.center.y);
//误差小于10
if ( w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
3;
return
YES;
}
w =abs(nextPoint.x - m_box4.center.x);
h =abs(nextPoint.y - m_box4.center.y);
//误差小于10
if ( w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
4;
return
YES;
}
return NO;
}
//移动箱子
-(void)moveTheBox
{
switch (dir)
{
case
up:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y-35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y-35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y-35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y-35);
break;
}
break;
case
down:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y+35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y+35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y+35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y+35);
break;
}
break;
case
left:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x-35,
m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x-35,
m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x-35,
m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x-35,
m_box4.center.y);
break;
}
break;
case
right:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x+35,
m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x+35,
m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x+35,
m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x+35,
m_box4.center.y);
break;
}
break;
}
[self
NextEevel];
}
//成功闯关
-(void)NextEevel
{
int num = 0;
for (int i=0; i<<span style="color:
#4f8187">destinationX.count;
i++)
{
int w =
abs(m_box1.center.x-[[destinationX objectAtIndex:i]floatValue]);
int h =
abs(m_box1.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box2.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box2.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box3.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box3.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box4.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box4.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
}
if (num ==4)
{
UIAlertView
*alert = [[UIAlertView
alloc]initWithTitle:@"祝贺您"
message:@"成功闯关!"
delegate:self cancelButtonTitle:@"再玩一次"
otherButtonTitles:@"下一关",
nil];
[alert show];
}
}
//处理警告结果
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case
0:
//再玩一次
[self initGame];
break;
case
1:
//进入第二关
[cAppDelegate
switchViewController:7];
break;
}
}
@end
"GameViewController.h"
#import "cAppDelegate.h"
@interface GameViewController
()
@end
@implementation
GameViewController
@synthesize m_box1;
@synthesize m_box2;
@synthesize m_box3;
@synthesize m_box4;
@synthesize m_target;
@synthesize destinationX;
@synthesize destinationY;
@synthesize ableMoveX;
@synthesize ableMoveY;
@synthesize boxFlag;
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle
*)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom
initialization
//初始化游戏
[self
initGame];
}
return self;
}
- (void)viewDidLoad
{
[super
viewDidLoad];
// Do any additional setup after loading
the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super
didReceiveMemoryWarning];
// Dispose of any resources that can be
recreated.
}
//初始化箱子的目的地数组,人物和箱子可以移动的坐标数组
-(void)initGame
{
//初始化箱子和人物的位置
m_target.center = CGPointMake(176.5, 275.5);
m_box1.center = CGPointMake(141.5, 240.5);
m_box2.center = CGPointMake(176.5, 240.5);
m_box3.center = CGPointMake(141.5, 275.5);
m_box4.center = CGPointMake(211.5, 275.5);
destinationX =
[[NSMutableArray alloc]init];
destinationY =
[[NSMutableArray alloc]init];
ableMoveX =
[[NSMutableArray alloc]init];
ableMoveY =
[[NSMutableArray alloc]init];
//初始化箱子的目的地x坐标
[destinationX
addObject:[NSNumber numberWithFloat:141.5]];
[destinationX
addObject:[NSNumber numberWithFloat:71.5]];
[destinationX
addObject:[NSNumber numberWithFloat:176.5]];
[destinationX
addObject:[NSNumber numberWithFloat:246.5]];
//初始化箱子的目的地y坐标
[destinationY
addObject:[NSNumber numberWithFloat:345.5]];
[destinationY
addObject:[NSNumber numberWithFloat:240.5]];
[destinationY
addObject:[NSNumber numberWithFloat:170.5]];
[destinationY
addObject:[NSNumber numberWithFloat:275.5]];
//人物和箱子可以移动的x坐标
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:141.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:106.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:176.5]];
[ableMoveX
addObject:[NSNumber numberWithFloat:211.5]];
//人物和箱子可以移动的y坐标
[ableMoveY
addObject:[NSNumber numberWithFloat:310.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:205.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:240.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
[ableMoveY
addObject:[NSNumber numberWithFloat:275.5]];
}
- (IBAction)btnBack:(id)sender {
[cAppDelegate
switchViewController:1];
}
//接收触屏事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
//获取当前应用程序的代理对象
cAppDelegate * nowDelegate = [UIApplication sharedApplication].delegate;
CGPoint touchPoint = [[touches anyObject]locationInView:nowDelegate.m_gameVC.view];
[self moveTarget:touchPoint];
}
//人物和箱子移动
-(void)moveTarget:(CGPoint)touchPoint
{
int w = abs(touchPoint.x- m_target.center.x);
int h = abs(touchPoint.y -m_target.center.y);
if
(w>h) //
left or right
{
if
(touchPoint.x<<span
style="color: #4f8187">m_target.center.x)
{
dir = left;
}
else
{
dir = right;
}
}
else
//up or
down
{
if
(touchPoint.y<<span
style="color: #4f8187">m_target.center.y)
{
dir = up;
}
else
{
dir = down;
}
}
switch (dir) {
case
up:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y-70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y-70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y-35);
}
}
break;
case
down:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//判断一个点是否有箱子
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+35)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x,m_target.center.y+70)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x,m_target.center.y+70)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x,m_target.center.y+35);
}
}
break;
case
left:
//判断下一个点是否可以走
if ([self judge:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x-35,m_target.center.y)]==YES)
{
//如果箱子存在,判断箱子是否能走到下一个点
if([self judge:CGPointMake( m_target.center.x-70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x-70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x-35,m_target.center.y);
}
}
break;
case
right:
//判断下一个点是否可以走
if([self judge:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if ([self judgeBoxExist:CGPointMake( m_target.center.x+35,m_target.center.y)]==YES)
{
if([self judge:CGPointMake( m_target.center.x+70,m_target.center.y)]==YES)
{
//判断当前箱子的下一个点是否有其他箱子,如果没有箱子则可以移动,反之则不能
if ([self judgeBoxExist:CGPointMake( m_target.center.x+70,m_target.center.y)]==NO)
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
//移动箱子
[self moveTheBox];
}
}
}
else
{
//移动人物
m_target.center = CGPointMake( m_target.center.x+35,m_target.center.y);
}
}
break;
}
}
//判断人物或箱子将要移动的坐标是否正确
-(BOOL)judge:(CGPoint)nextPoint
{
for(int
i=0;i<<span style="color:
#4f8187">ableMoveX.count;i++)
{
int w
=abs(nextPoint.x - [[ableMoveX objectAtIndex:i]floatValue]);
int h =
abs( nextPoint.y - [[ableMoveY objectAtIndex:i]floatValue]);
//误差小于10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
return YES;
}
}
for (int i=0; i<<span style="color:
#4f8187">destinationX.count;
i++)
{
int w
=abs( nextPoint.x - [[destinationX objectAtIndex:i]floatValue]);
int h =
abs(nextPoint.y - [[destinationY objectAtIndex:i]floatValue]);
//误差小于10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
return YES;
}
}
return NO;
}
//判断人物的下一个坐标是否有箱子
-(BOOL)judgeBoxExist:(CGPoint)nextPoint
{
int w = abs( nextPoint.x - m_box1.center.x);
int h = abs(nextPoint.y - m_box1.center.y);
//误差小于10
if (w<<span style="color:
#272ad8">10&&h<<span style="color:
#272ad8">10)
{
boxFlag =
1;
return
YES;
}
w = abs(nextPoint.x - m_box2.center.x);
h = abs(nextPoint.y - m_box2.center.y);
//误差小于10
if (w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
2;
return
YES;
}
w = abs(nextPoint.x - m_box3.center.x);
h = abs(nextPoint.y - m_box3.center.y);
//误差小于10
if ( w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
3;
return
YES;
}
w =abs(nextPoint.x - m_box4.center.x);
h =abs(nextPoint.y - m_box4.center.y);
//误差小于10
if ( w<<span style="color:
#272ad8">10&&h<<span style="color: #272ad8">10
)
{
boxFlag =
4;
return
YES;
}
return NO;
}
//移动箱子
-(void)moveTheBox
{
switch (dir)
{
case
up:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y-35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y-35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y-35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y-35);
break;
}
break;
case
down:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x, m_box1.center.y+35);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x, m_box2.center.y+35);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x, m_box3.center.y+35);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x, m_box4.center.y+35);
break;
}
break;
case
left:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x-35,
m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x-35,
m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x-35,
m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x-35,
m_box4.center.y);
break;
}
break;
case
right:
switch (boxFlag)
{
case 1: //移动m_box1
m_box1.center = CGPointMake(m_box1.center.x+35,
m_box1.center.y);
break;
case 2: //移动m_box2
m_box2.center = CGPointMake(m_box2.center.x+35,
m_box2.center.y);
break;
case 3: //移动m_box3
m_box3.center = CGPointMake(m_box3.center.x+35,
m_box3.center.y);
break;
case 4: //移动m_box4
m_box4.center = CGPointMake(m_box4.center.x+35,
m_box4.center.y);
break;
}
break;
}
[self
NextEevel];
}
//成功闯关
-(void)NextEevel
{
int num = 0;
for (int i=0; i<<span style="color:
#4f8187">destinationX.count;
i++)
{
int w =
abs(m_box1.center.x-[[destinationX objectAtIndex:i]floatValue]);
int h =
abs(m_box1.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box2.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box2.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box3.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box3.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
w = abs(m_box4.center.x-[[destinationX objectAtIndex:i]floatValue]);
h = abs(m_box4.center.y-[[destinationY objectAtIndex:i]floatValue]);
//误差为10
if
(w<<span style="color: #272ad8">10&&h<<span
style="color: #272ad8">10)
{
num++;
}
}
if (num ==4)
{
UIAlertView
*alert = [[UIAlertView
alloc]initWithTitle:@"祝贺您"
message:@"成功闯关!"
delegate:self cancelButtonTitle:@"再玩一次"
otherButtonTitles:@"下一关",
nil];
[alert show];
}
}
//处理警告结果
- (void)alertView:(UIAlertView *)alertView
clickedButtonAtIndex:(NSInteger)buttonIndex
{
switch (buttonIndex)
{
case
0:
//再玩一次
[self initGame];
break;
case
1:
//进入第二关
[cAppDelegate
switchViewController:7];
break;
}
}
@end
相关文章推荐
- uboot 2013.01 代码简析(2)第一阶段初始化
- Ajax-Ajax核心代码
- jquery ajax方式直接提交整个表单核心代码
- atoi核心代码
- 如何编写更棒的代码:11个核心要点
- 自动生成MVC中的model实体类的核心代码
- 购物车核心代码
- android小游戏——2048 核心功能的实现与简单的代码重构(2)
- 主要数据库连接核心代码【Java】
- 基于visual c++之windows核心编程代码分析(29)ICMP实现远程控制
- 基于visual c++之windows核心编程代码分析(37)实践信息安全隐患监测-嗅探
- ExtJs源码分析与学习—ExtJs核心代码(三)
- 基于visual c++之windows核心编程代码分析(42)windows下进程的身份切换
- 自定义UITableView:核心代码就3句
- 基于visual c++之windows核心编程代码分析(49)基于匿名管道实现远程控制
- 基于visual c++之windows核心编程代码分析(57)监控系统的每一个进程的创建
- 基于visual c++之windows核心编程代码分析(65)实现程序自我复制
- 几行代码搞定YUI3的核心对象
- (八)springmvc+mybatis+dubbo+zookeeper分布式架构 整合 - maven构建ant-framework核心代码annotation
- 代码结构组织的核心思想