CGRectIntersectsRect问题,重叠帧但没有检测到它

时间:2012-03-14 16:22:09

标签: iphone objective-c ios xcode

我正在尝试为iPad创建一个应用程序,到目前为止一切都很好,现在我正试图让一个物体跳跃,所以我已经让它跳到空中然后再次跌落,多数民众赞成。但是当我希望它再次停留在地面时,我的问题就来了,我认为CGRectIntersectsRect会起作用,但它只是不起作用,我的意思是物体显然与地面图像重叠,但它并没有停止。

代码:

    -(void)gameStatePlayNormal{

    if(cubeState == CubeIsTouchingGround){
        cubeVelocity.y = 0;
    }

    gravity = CGPointMake(0, kGravity);

    cube.center = CGPointMake(cube.center.x, cube.center.y + cubeVelocity.y);
    cubeVelocity.y += gravity.y;

    if(touchState == Touch){

        if(cubeState != CubeIsJumping){
            cubeState = CubeIsJumping;
            cubeVelocity.y = cubeVelocity.y -10;
            [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(cubejump) userInfo:nil repeats:NO];

        }

    }

}
-(void)cubejump{

    if(cubeState == CubeIsJumping){
        if(CGRectIntersectsRect(cube.frame, ground.frame)){   
            NSLog(@"Cube is touching the ground.");
            cubeState = CubeIsTouchingGround;
            cubeVelocity.y = 0;

        }

    }
}

和.h文件:

    UIImageView *cube;
    UIImageView *spike;
    UIImageView *box;
    UIImageView *ground;

}

-(void)gameStatePlayNormal;

@property (nonatomic, retain) IBOutlet UIImageView *cube;
@property (nonatomic, retain) IBOutlet UIImageView *spike;
@property (nonatomic, retain) IBOutlet UIImageView *box;
@property (nonatomic, retain) IBOutlet UIImageView *ground;

当然,所有内容都链接在.xib文件中,我可以通过其他方式操作地面图像和立方体图像,以便将它们链接到xib文件中的变量。

请帮助,我尝试使用self.view.bounds.size.width,但它不适用于屏幕的下半部分。请帮助:)

0 个答案:

没有答案