0

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

我试图检测矩形 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;
        }
}
4

0 回答 0