如何防止与arc4random_uniform

时间:2015-08-17 14:33:06

标签: arc4random

我正在制作一个简单的游戏,我有四个物体同时在屏幕上移动(两个物体向左移动,两个向右移动)。 X轴是固定点,无需更改。

使用arc4random_uniform,有时两个对象是重叠的,即对象在它们的随机位置生成,但是它们中的两个非常接近并且当它们在屏幕上移动时彼此重叠。

我希望能够设置间隙,使对象的间隙最小为25px(y轴),这样当它们在屏幕上移动时它们不会相互重叠。我用什么样的代码来做这件事?

这是我的代码:

- (void)GenerateSpikes {

    CGRect frame = SpikeLeft.superview.bounds;
    CGFloat leftMargin = 5; //use whatever values you want for magins
    CGFloat rightMargin = 5;
    CGFloat topMargin = 80;
    CGFloat bottomMargin = 80;
    CGFloat randomX, randomY;

    randomX = arc4random_uniform(0) + 330 + rightMargin; // (330) Where they appear on screen on the Y Axis
    CGFloat SpikeLeftyMax = frame.size.height-topMargin-bottomMargin-
    SpikeLeft.bounds.size.height/2;
    randomY = arc4random_uniform(SpikeLeftyMax) + topMargin;
    SpikeLeft.center = CGPointMake(randomX, randomY);

    randomX = arc4random_uniform(0) + 10 + leftMargin;
    CGFloat SpikeRightyMax = frame.size.height-topMargin-bottomMargin-
    SpikeRight.bounds.size.height/2;
    randomY = arc4random_uniform(SpikeRightyMax) + topMargin;
    SpikeRight.center = CGPointMake(randomX, randomY);

    randomX = arc4random_uniform(0) + 10 + leftMargin;
    CGFloat SpikeRightOneyMax = frame.size.height-topMargin-bottomMargin-
    SpikeRightOne.bounds.size.height/2;
    randomY = arc4random_uniform(SpikeRightOneyMax) + topMargin;
    SpikeRightOne.center = CGPointMake(randomX, randomY);

    randomX = arc4random_uniform(0) + 200 + leftMargin;
    CGFloat SpikeLeftOneyMax = frame.size.height-topMargin-bottomMargin-
    SpikeLeftOne.bounds.size.height/2;
    randomY = arc4random_uniform(SpikeLeftOneyMax) + topMargin;
    SpikeLeftOne.center = CGPointMake(randomX, randomY);
}

- (void)ReverseSpikesMoving {

    SpikeLeft.center = CGPointMake(SpikeLeft.center.x -1, SpikeLeft.center.y);
    SpikeRight.center = CGPointMake(SpikeRight.center.x +1, SpikeRight.center.y);
    SpikeLeftOne.center = CGPointMake(SpikeLeftOne.center.x -1, SpikeLeftOne.center.y);
    SpikeRightOne.center = CGPointMake(SpikeRightOne.center.x +1, SpikeRightOne.center.y);


    if (SpikeRight.center.x > 330) { //Change to SpikeLeft and 16 <
        [ReverseSpikesMoving invalidate];
        [self GenerateSpikes];
        [self SpikeMovingTimer];
        [self ScoreNormal];

    }

    if (TimerCountdown < 0) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, TopTunnel.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, BottomTunnel.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, SpikeLeft.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, SpikeRight.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, SpikeLeftOne.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Face.frame, SpikeRightOne.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(SpikeLeft.frame, X2Score.frame)) {
        [self HideX2];
    }

    if (CGRectIntersectsRect(SpikeLeftOne.frame, X2Score.frame)) {
        [self HideX2];
    }

    if (CGRectIntersectsRect(SpikeRight.frame, X2Score.frame)) {
        [self HideX2];
    }

    if (CGRectIntersectsRect(SpikeRightOne.frame, X2Score.frame)) {
        [self HideX2];
    }

    if (CGRectIntersectsRect(Face.frame, X2Score.frame)) {
        [self ScoreX2];
        [self HideX2];
    }
}

0 个答案:

没有答案