Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Programmer

macrumors member
Original poster
Jun 16, 2009
79
0
I am creating a game for the iphone and i have frame by frame images set up of my object getting closer and closer to you (I used frame by frame so i could speed up and slow down the object without distortion). Is there a way to say that when the UIImageView equals a certian to do something. I tried saying this.

if (Main.image = [UIImage imageNamed:mad:"picture1.png"]) {

//My Code Was Here...

}
I put that in the view did load and when the app started up the image was set to that image instead of the NSArray of images it was supposed to go through.
 
On an if statement, to check for equalities you must use "==" rather than "=".

The way you have it, you are telling it to make the image of Main to the UIImage you want to compare, rather than actually comparing.

I don't know if if statements work with images, but this code should work:

Code:
if (Main.image == [UIImage imageNamed:@"picture1.png"]) {

//Code

}
 
On an if statement, to check for equalities you must use "==" rather than "=".

The way you have it, you are telling it to make the image of Main to the UIImage you want to compare, rather than actually comparing.

I don't know if if statements work with images, but this code should work:

Code:
if (Main.image == [UIImage imageNamed:@"picture1.png"]) {

//Code

}

That won't work. You are checking that the memory address of the images is the same. And it isn't.

Basically unless you track which object came from which file yourself you cannot do what you are trying to do: UIImage does not retain this information and you cannot ask it to check if one image is the same as another (and this would be a potentially very expensive operation if you could).
 
On an if statement, to check for equalities you must use "==" rather than "=".

The way you have it, you are telling it to make the image of Main to the UIImage you want to compare, rather than actually comparing.

I don't know if if statements work with images, but this code should work:

Code:
if (Main.image == [UIImage imageNamed:@"picture1.png"]) {

//Code

}

It goes like this:


== means is equal to
!= means is not equal to
>= means is greater than or equal to
<= means is less than or equal to
< means less than
> means greater than

You can also handle multiple booleans at once:


Code:
if (var2+var1==var3 && var3+2==var4){

//These statements will only work if both conditions are true

}


if (var2+var1==var3 || var3+2=var4){

//These statements will work if just one condition is true
//It does not matter which one is true
//You can add as many || or && symbols as you want
}

If I'm missing anything, please tell me
I'm not sure if it works with images but I know it works with Image views
 
Since you are dealing with pointers to objects, you want something like:

if ([firstImage isEqualToImage: secondImage])

which doesn't exist.

What about:

NSData *firstImageData = UIImagePNGRepresentation(firstImage);

NSData *secondImageData = UIImagePNGRepresentation(secondImage);

if ([firstImageData isEqualToData: secondImageData])

This is certainly *not* the optimal way to go about doing things. You should have some kind of mapping (maybe even just a C array).
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.