2

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?

C.Wetherell
  • 215
  • 2
  • 14
  • For completeness, please add the `RectBox` method as well. – Unheilig Jun 01 '14 at 16:14
  • RectBox Method? the RectBox is in the bottom of this section of code. – C.Wetherell Jun 01 '14 at 16:16
  • But I don't see you defining `RectBox` in your `ietwoViewController.m`. There is no such method, hence the error. – Unheilig Jun 01 '14 at 16:19
  • Place 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; }` in your `.m` but outside of any method. – Unheilig Jun 01 '14 at 16:21
  • ok so the code seems to be working, the only problem is, my character (Appy) is now flashing from tiny size to his original size and back very fast. he should stay at 40x40 – C.Wetherell Jun 01 '14 at 16:30
  • If it helps, let us first close this question by marking my answer as correct and then open another question for that. – Unheilig Jun 01 '14 at 16:33
  • i cant be positive its working though as the character seems to be changing size at such a rapid rate its hard to see – C.Wetherell Jun 01 '14 at 16:38
  • i have done some testing with that app, its not working its just making my character change size im not too sure why either after looking at the code, its seems as though my character is changing to 3 different sizes and not even at his original size which was 40x40, i think he's changing from 5x5 to 10x10 and then 15x15 and back again – C.Wetherell Jun 01 '14 at 16:45
  • Please check answer update. Again, it rewards to have another question on this part. – Unheilig Jun 01 '14 at 17:34

4 Answers4

1

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;
}
Ahmad dar
  • 81
  • 1
  • 14
  • getting 'too few arguments to function call,expect 2, have 1' error, not too sure what this means? – C.Wetherell Jun 01 '14 at 13:20
  • @C.Wetherell have you tried this ? if (CGRectIntersectsRect(RectBox(Appy).frame, Bottom.frame)); and is Appy is an UIImageView ? – Ahmad dar Jun 01 '14 at 13:22
  • yes i changed it to that already and yes Appy is a UIImageView – C.Wetherell Jun 01 '14 at 13:24
  • have you declared -(UIImageView *)RectBox:(UIImageView *)box; in header file ? – Ahmad dar Jun 01 '14 at 13:49
  • i have declared the UIImageView name Appy as an IBOutlet in my .h i didnt realise i needed to? what do i declare it as? a method? – C.Wetherell Jun 01 '14 at 13:52
  • try this if(CGRectIntersectsRect([self RectBox:Appy].frame, bottom.frame)) – Ahmad dar Jun 01 '14 at 13:58
  • with ur last comment should i take out the `-(UIIMageView*)RectBox`.... line of code? – C.Wetherell Jun 01 '14 at 14:02
  • I am saying change your code line if (CGRectIntersectsRect(RectBox(Appy).frame, Bottom.frame)); to the following one if(CGRectIntersectsRect([self RectBox:Appy].frame, bottom.frame)); – Ahmad dar Jun 01 '14 at 14:05
  • Replace (CGRectIntersectsRect(RectBox(Appy).frame, Bottom.frame)); with this (CGRectIntersectsRect([self RectBox(Appy)].frame, Bottom.frame)); – Ahmad dar Jun 01 '14 at 14:08
  • it now reads `if (CGRectIntersectsRect([self RectBox(Appy)].frame, Bottom.frame)); { [self GameOver]; }` it says an error "expected ']'" but i cant see where i should be putting it, there isnt a ']' missing? – C.Wetherell Jun 01 '14 at 14:27
  • remove the semicolon ";" from the if statement – Ahmad dar Jun 01 '14 at 14:31
  • remove braces from Appy and put colon i.e. [self RectBox(Appy)] Replace it with [self RectBox:Appy] – Ahmad dar Jun 01 '14 at 14:43
  • i think i need to declare RectBox in my .h file for this to work but not sure what to declare it as? i changed it and it now says "Use of undeclared identifier 'RectBox'" – C.Wetherell Jun 01 '14 at 14:51
  • in your .h file add this line -(UIImageView *)RectBox:(UIImageView *)box; – Ahmad dar Jun 01 '14 at 14:53
  • this is a method so you have to declare it below the curly braces of your .h file. – Ahmad dar Jun 01 '14 at 14:59
  • i have updated my question, even tho it is now declared im still getting that the identifier is not declared.. – C.Wetherell Jun 01 '14 at 15:07
  • @C.Wetherell There should be no `:` after `self` in your second edit. – Unheilig Jun 01 '14 at 15:16
1

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.

Unheilig
  • 16,196
  • 193
  • 68
  • 98
  • i have completely taken out the `-(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; }` code now – C.Wetherell Jun 01 '14 at 15:39
  • @C.Wetherell And are you still experiencing any errors or is it already working for you? And as in my previous comment, there is no `:` after `self`: `[self RectBox:Image].frame`. – Unheilig Jun 01 '14 at 15:43
  • i have a warning that says the method RectBox is unused, and now when i start my app up and click play (which loads the method im editing) the screen freezes and i get the debug appear, i will post what it shows in my question as another edit – C.Wetherell Jun 01 '14 at 15:50
  • @C.Wetherell Yes, post that, if possible, type in the code in the post instead of taking an image of it. – Unheilig Jun 01 '14 at 15:52
  • @C.Wetherell Could you copy and paste the code in which you call and define the method? It would be helpful for further testing. – Unheilig Jun 01 '14 at 16:01
  • i think i posted all you need i can paste the full .m if that would be easier? i posted the entire .m to http://pastebin.com/bU7p6PGz incase its not what you needed – C.Wetherell Jun 01 '14 at 16:13
0

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.
}
Kumar Utsav
  • 2,761
  • 4
  • 24
  • 38
0
var attributes = [UICollectionViewLayoutAttributes]()
    for att in cache {            
        if (att.frame.intersects(rect))
        {
        attributes.append(att)
        }
    }

In swift 3 You can use like this.