简体   繁体   English

CGRectsIntersects 没有两个图像碰撞

[英]CGRectsIntersects without two images colliding

I have an issue at the moment.我现在有一个问题。 I am making a game.我正在制作游戏。 When two imageViews collide the game will end.当两个 imageViews 碰撞时,游戏将结束。 I am using CGRECTIntersects rect to detect if the two images collide.我正在使用 CGRECTIntersects rect 来检测两个图像是否发生碰撞。

The issue is that when i restart the game the collision will happen when in fact the two images have not actually touched one another?问题是当我重新启动游戏时,实际上两个图像实际上并没有相互接触时会发生碰撞?

Any suggestions would be much appreciated.任何建议将不胜感激。 Thank you谢谢

-(void)collision { -(无效)碰撞{

if (CGRectIntersectsRect(object.frame, object2.frame)) {
    object.hidden=YES;
    object2.hidden=YES;
    retry.hidden=NO;
    [timer invalidate];
}

} }

/- (void)viewDidLoad { /- (void)viewDidLoad {

retry.hidden=YES;


timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(object2) userInfo:nil repeats:YES];



objectSpeed1 = CGPointMake(3.0, 2.0);

[super viewDidLoad];

} }

/- (IBAction)retry:(id)sender { /- (IBAction)retry:(id)sender {

 [self performSegueWithIdentifier:@"restart" sender:sender];

} }

-(void)object2 { -(无效)object2 {

object2.center = CGPointMake(object2.center.x + object2.x, object2.center.y + objectspeed1.y);

if (object2.center.x > 310 || object2.center.x < 10) {
    objectspeed1.x = - objectspeed1.x;
}
if (object2.center.y > 558 || object2.center.y < 10) {
    objectspeed1.y = - objectspeed1.y;
}

Just consider this case on the code.只需在代码中考虑这种情况。 You can check if image views are on their start position or they came to this position from another.你可以检查图像视图是在它们的起始位置还是从另一个位置来到这个位置。 It's enough to keep BOOL property on viewcontroller's class like this:像这样在 viewcontroller 的类上保留BOOL属性就足够了:

// ViewController.m

@interface ViewController ()

@property (readonly) BOOL isStarted;

@end

@implementation

- (void)viewDidLoad {
    [super viewDidLoad];

    _isStarted = NO;

    // Make some preparations for app's needs

    _isStarted = YES;
}

@end

_isStarted is a flag which shows whether viewcontroller is ready to handle the data. _isStarted是一个标志,显示_isStarted是否准备好处理数据。

if(_isStarted) {
    // Analyze image views' frame
}
else {
    // Do nothing
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM