我对 Objective-c 和 iPhone 编程非常陌生(尽管我对 C# 有更多的背景知识),而且我正在边做边学。
我目前正在尝试制作一个迷你平台游戏,而不是单独检查每个平台以查看我的玩家是否与它相交,我想制作一个数组和一个 for 语句来解决这个问题。(如果我错了,请纠正我,但NSMutableArray
看起来很像List
C# 中的功能)
我输入了我认为可行的内容,但没有,任何想法为什么?在我的@interface
我有:
@interface ViewController : UIViewController
{
NSMutableArray *platforms;
UIImageView *platform1;
UIImageView *platform2;
UIImageView *platform3;
UIImageView *platform4;
UIImageView *platform5;
UIImageView *player;
}
@property (nonatomic) NSInteger GameState;
@property IBOutlet UIImageView *player;
@property IBOutlet UIImageView *platform1;
@property IBOutlet UIImageView *platform2;
@property IBOutlet UIImageView *platform3;
@property IBOutlet UIImageView *platform4;
@property IBOutlet UIImageView *platform5;
在我的@implementation 我有:
- (void)viewDidLoad
{
[super viewDidLoad];
[NSTimer scheduledTimerWithTimeInterval:1.0/60 target:self selector:@selector(gameLoop) userInfo:nil repeats:YES];
gravity = CGPointMake(0,0.195);
[platforms addObject:platform1];
[platforms addObject:platform2];
[platforms addObject:platform3];
[platforms addObject:platform4];
[platforms addObject:platform5];
}
- (void)gameLoop
{
playerVelocity = CGPointMake(playerVelocity.x,playerVelocity.y + gravity.y);
player.center = CGPointMake(player.center.x + playerVelocity.x,player.center.y + playerVelocity.y);
for(UIImageView *platform in platforms)
{
if(CGRectIntersectsRect(platform.frame,player.frame))
{
BOOL check = YES; //break point here to check if it reaches this point
}
}
}
另外,当我简单地输入:
if(CGRectIntersectsRect(platform1.frame,player.frame))
{
BOOL doubleCHECK = YES;
}
有用。