簡體   English   中英

CGRectsIntersects 沒有兩個圖像碰撞

[英]CGRectsIntersects without two images colliding

我現在有一個問題。 我正在制作游戲。 當兩個 imageViews 碰撞時,游戲將結束。 我正在使用 CGRECTIntersects rect 來檢測兩個圖像是否發生碰撞。

問題是當我重新啟動游戲時,實際上兩個圖像實際上並沒有相互接觸時會發生碰撞?

任何建議將不勝感激。 謝謝

-(無效)碰撞{

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

}

/- (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 {

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

}

-(無效)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;
}

只需在代碼中考慮這種情況。 你可以檢查圖像視圖是在它們的起始位置還是從另一個位置來到這個位置。 像這樣在 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是一個標志,顯示_isStarted是否准備好處理數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM