iosobjective-ccollision-detectionimagenamed

how can i return same object in CGRectIntersectsRect if statement?


This line of code was working prior to iOS 8, but after updating to iOS 8 this line of code does not work anymore.

if (CGRectIntersectsRect(icon1.frame, pig.frame)
    && icon1.image == [UIImage imageNamed:@"BerryBlueberryIphone.png"]) {

what other options do i have to achieve the same thing as my old line of code. i need an if statement detecting collision of two objects with one of the objects having a particular image.

i have more info and code of the game i am trying to make in this link. the link leads to a previous question i made before knowing what was wrong with my game after updating. here is the link...

Collision not working on iphone just ipad Xcode 6.1.1


Solution

  • To fix, you'll need a stronger representation of the objects in your game. It looks like the collision you hope to detect is between two UIViews and you want to know whether one of those views has a particular image.

    A better way to think about this is to detect collisions between objects in your model that are represented by views. When the view controller detects that two views overlap, it can ask the corresponding model whether it's a pig or a blueberry or whatever. Prior to OS8, you were relying on the OS to answer the same image when passing the same string to imageNamed:. A better design even then would have been to have a model object.

    I strongly recommend a rethink of your project this way, but as quickie solution, you could reserve a special tag (say, 999) for imageViews that are blueberry images. Then the collision test can read:

    if (CGRectIntersectsRect(icon1.frame, pig.frame) && icon1.tag == 999) {}