C.Wetherell
C.Wetherell

Reputation: 215

CGRectIntersectsRect: how change the detection to the edge of the picture inside UIImageView

Here is the code i'm using to detect if 2 UIImageViews hit each other.

 if (CGRectIntersectsRect(Appy.frame, Bottom.frame)) {
   [self GameOver];
}

At the moment its detecting the edge of the object.

How do i change it to detect the edge of the image inside the UIImageView.

The image inside has a transparent background and is "practically circle" so they are colliding if the corner of UIImageView hits the corner of another UIImageView but the image is not actually hitting so its making things look a little "messy"

EDIT*

Errors im getting

these are the errors im getting

enter image description here this is my .h


EDIT: below is what my code now looks like

.m Method

if (CGRectIntersectsRect([self: RectBox:Appy].frame, Bottom.frame)) {
        [self GameOver];
    }

.h

-(UIImageView *)RectBox:(UIImageView *)box;

i put this just above the very last @end on my .h just to be sure.

Im still "getting use of undeclared identifier 'RectBox'"

EDIT

Debug screem

EDIT

As requested here is the code in which i call the method

-(void) PipeMoving2{

    PipeTop2.center = CGPointMake(PipeTop2.center.x -2, PipeTop2.center.y);
    PipeBottom2.center = CGPointMake(PipeBottom2.center.x -2, PipeBottom2.center.y);





    if (PipeTop2.center.x < -53+33) {
        [self PlacePipe2];
    }

    if (PipeTop2.center.x == 62) {
        [self Score];
    }


    if (CGRectIntersectsRect(Appy.frame, PipeTop2.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect(Appy.frame, PipeBottom2.frame)) {
    [self GameOver];
    }

    if (CGRectIntersectsRect(Appy.frame, Top.frame)) {
        [self GameOver];
    }

    if (CGRectIntersectsRect([self RectBox:Appy].frame, Bottom.frame)) {
        [self GameOver];
    }

}

i think this is all u need to see right?

Upvotes: 2

Views: 1903

Answers (4)

user6512047
user6512047

Reputation:

var attributes = [UICollectionViewLayoutAttributes]()
    for att in cache {            
        if (att.frame.intersects(rect))
        {
        attributes.append(att)
        }
    }

In swift 3 You can use like this.

Upvotes: 0

Kumar Utsav
Kumar Utsav

Reputation: 2841

Try this code

CGPoint a = imageViewA.center;
CGPoint b = imageViewB.center;
CGFloat distance = sqrt(pow((b.x - a.x),2) + pow((b.y - a.y),2));

if(distance < (imageViewA.bounds.size.width/2.0 + imageViewB.bounds.size.width/2.0)){
    //images are touching.
}

Upvotes: 0

Unheilig
Unheilig

Reputation: 16292

Change it to the following:

if(CGRectIntersectsRect([self RectBox:Image].frame, Bottom.frame))

Another thing I noticed from your image is that you are placing the method within another method.

Ex: (what I see from your image, it seems)

- (void)yourMethod
{
    ......
    if (CGRectIntersectsRect([self RectBox:Image].frame, Bottom.frame)) 
    {
       [self GameOver];
    }
    .....
    //etc etc

    //and then you have this
    -(UIImageView *)RectBox:(UIImageView *)box
    {
        box.frame = CGRectMake(box.frame.origin.x +15,
                               box.frame.origin.y +15,
                               box.frame.size.width -30,
                               box.frame.size.height-30);
        return box;
    }

....
}

You should extract that and place it outside of your method, so it becomes:

- (void)yourMethod
{
    ......
    if (CGRectIntersectsRect([self RectBox:Image].frame, Bottom.frame)) 
    {
       [self GameOver];
    }
    .....
    //etc etc
    ....
}

//take it out and place it here, for example

- (UIImageView *)RectBox:(UIImageView *)box
{
     box.frame = CGRectMake(box.frame.origin.x +15,
                            box.frame.origin.y +15,
                            box.frame.size.width -30,
                            box.frame.size.height-30);
     return box;
}

Update to questions in comments not exactly related to the original question, but here is my finding from your code:

The reason you character is changing size may stem from the fact that you are changing its frame in your RectBox method.

Here is a rough redefinition of the method, so that we could "preserve" the frame of your character:

- (CGRect)RectBox:(UIImageView *)box
{
     CGRect tempRect = CGRectMake(box.frame.origin.x +15,
                                  box.frame.origin.y +15,
                                  box.frame.size.width -30,
                                  box.frame.size.height-30);
     return tempRect;
}

And make changes to the call.

Ex:

if (CGRectIntersectsRect([self RectBox:Image], Bottom.frame)) 
{
   [self GameOver];
}

Regarding the movements, check the frequency you set for your NSTimer. I wouldn't go more frequent than 0.1.

Hope this helps.

Upvotes: 1

Ahmad dar
Ahmad dar

Reputation: 81

Try this

.h

-(UIImageView *)RectBox:(UIImageView *)box;

.m

if (CGRectIntersectsRect([self RectBox:Image].frame, Bottom.frame)) 
{
   [self GameOver];
}

-(UIImageView *)RectBox:(UIImageView *)box
{
    box.frame = CGRectMake(box.frame.origin.x +15,
                          box.frame.origin.y +15,
                          box.frame.size.width -30,
                          box.frame.size.height-30);
    return box;
}

Upvotes: 1

Related Questions