我在单一视图应用程序中制作应用程序。这只是一个简单的迷宫游戏。我想阻止角色穿过墙壁,但我无法做到这一点(缺乏经验)。请有人帮忙,我真的很感激。我的代码如下。
COLOR.H
#import <UIKit/UIKit.h>
int CharacterMovementUp;
int CharacterMovementLeft;
BOOL CollideWithWalls;
@interface color : UIViewController{
NSTimer *MovementTimer;
}
@property (strong, nonatomic) IBOutlet UIImageView *Character;
@property (strong, nonatomic) IBOutletCollection(UIImageView) NSArray *Walls;
-(IBAction)Start:(id)sender;
-(void)Movement;
-(void)Collision;
@end
COLOR.M
#import "color.h"
@interface color ()
@end
@implementation color
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *touch = [touches anyObject];
CGPoint point = [touch locationInView:self.view];
if ((point.y > 460) && (CollideWithWalls == NO)) {
CharacterMovementUp = 7;
CharacterMovementLeft = 0;
}
else if((point.y < 140) && (CollideWithWalls == NO)) {
CharacterMovementUp = -7;
CharacterMovementLeft = 0;
}
else if((point.x < 160) && (point.y < 463) && (point.y > 140) && (CollideWithWalls == NO)){
CharacterMovementUp = 0;
CharacterMovementLeft = -7;
}
else if((point.x > 160) && (point.y < 460) && (point.y > 140) && (CollideWithWalls == NO)){
CharacterMovementUp = 0;
CharacterMovementLeft = 7;
}
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CharacterMovementUp = 0;
CharacterMovementLeft = 0;
}
-(void)Movement{
_Character.center = CGPointMake(_Character.center.x + CharacterMovementLeft, _Character.center.y + CharacterMovementUp);
}
-(IBAction)Start:(id)sender{
MovementTimer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(Movement) userInfo:nil repeats:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
CollideWithWalls = NO;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:0)