user2277872
user2277872

Reputation: 2973

UIImageView comparing images in game

I am working on a rock paper scissor shoe game, and I have no idea on how to do this. I have two UIImageView(s), and within each one is an image. I want to be able to compare the two images. I have looked up online, and cannot find anything. I want to be able to say

 if([imageView1.image == rock] && [imageView2.image == scissor]) {
    textLabel.text = @"You won!";
 }

I know that this syntax is wrong of course, but I am trying to show the english part of what I am looking for. Any help is appreciated.

I do not have any kind of source code to show you as I do not know what I am doing with this. I am not looking for pixel to pixel comparison, or anything complex, I am only looking for a way to determine if the image is the same or not.

Upvotes: 0

Views: 121

Answers (1)

Bergasms
Bergasms

Reputation: 2203

OK, Here is how I would solve this problem using enums. Firstly, declare your enum. You can call it whatever you like, i cam calling it RPS_STATE

enum RPS_STATE {
    RPS_ROCK,
    RPS_SCISSOR,
    RPS_PAPER,
    RPS_UNDEFINED
};

it's always useful to include an undefined state for init purposes. Now, these things defined in the enum are actually just integers from 0-3.

So you can use the following when you are setting your images.

 -(void) setToRock:(UIImageView *) view {
      view.image = rockImage;
      view.tag = RPS_ROCK;
 }

 -(void) setToScissor:(UIImageView *) view  {
      view.image = scissorImage;
      view.tag = RPS_SCISSOR;
 }

 -(void) setToPaper:(UIImageView *) view  {
      view.image = paperImage;
      view.tag = RPS_PAPER;
 }

then you can set and compare them nice and easy.

 [self setToPaper:imageView1];
 [self setToPaper:imageView2];

 if(imageView1.tag == imageView2.tag){

 }  

and so on. You can also use enums as a type. eg

 enum RPS_STATE variableHoldingOnlyRPSState; 

Hope that helps :)

Upvotes: 3

Related Questions