“幽灵矩形”影响程序?

时间:2013-03-28 16:55:14

标签: iphone ios objective-c frameworks

昨天我的节目的顶角有一个长方形卡住了,出于某种原因,无论我做了什么,我都无法将它从那里拿出来。今天我重新运行程序,它消失了,但我相信它的内存仍然卡住,因为它影响了我的代码。

我正在尝试检测矩形A(由加速度计运动)何时接触矩形B(随机放置;不动)。当A接触B时没有任何反应,但是当我将A移动到昨天幽灵矩形的顶角时,它会持续运行我的if语句,直到我将其移开。

这是代码,只是需要它

@synthesize Button;
@synthesize Rand;
//Awaking the accelerometer
-(void) awakeaccelerometer
{
    [[UIAccelerometer sharedAccelerometer]setUpdateInterval:1.0/50.0];//<---Sensitivity
    [[UIAccelerometer sharedAccelerometer]setDelegate:self];

    NSLog(@"Accelerometer is awake");

    float randX = arc4random() % 320;
    float randY = arc4random() % 548;

    CGPoint randNewPlace = CGPointMake(randX, randY);
    Rand.center = randNewPlace;

}


//Controlling the accelerometer
-(void) accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration
{
#define MovingObjectRadius 4

    //Acceleration for player
    valueX = acceleration.x * 10; //speeds
    valueY = acceleration.y * -10; //speeds



    //Create new integers
    int newX = (int)(Button.center.x + valueX);
    int newY = (int)(Button.center.y + valueY);


    //Position Validate...This stops the movements from going past the screen; 320 pixels
    //For X
    if (newX > (320 - MovingObjectRadius))
    {
        newX = (320 - MovingObjectRadius);
    }

    if (newX < (0 + MovingObjectRadius))
    {
        newX = (0 + MovingObjectRadius);
    }

    //For Y
    if (newY > (548 - MovingObjectRadius))
    {
        newY = (548 - MovingObjectRadius);
    }

    if (newY < (0 + MovingObjectRadius))
    {
        newY = (0 + MovingObjectRadius);
    }



    //Make the new point
    CGPoint buttonNewCenter = CGPointMake(newX,newY);
    Button.center = buttonNewCenter;


    CGRect blockRect = CGRectMake(newX, newY, 20, 20);
    CGRect targetRect = CGRectMake(randX, randY, 20, 20);

    if (CGRectIntersectsRect(blockRect, targetRect))
        {
            float tnewX = arc4random() % 320;
            float tnewY = arc4random() % 548;

            CGPoint randNewPl = CGPointMake(tnewX, tnewY);
            Rand.center = randNewPl;
        }
}

0 个答案:

没有答案