0

Firstly here is my .h files code where i declared everything...

    @interface NextlLevel : UIViewController{

     IBOutlet UIImageView *Coin1;
      int score;
      IBOutlet UILabel *scorelable;
  }

 @property (nonatomic, assign) int score;
 @property (nonatomic, retain) IBOutlet UILabel *scorelable;

  @end

I have also synthesised my implementations in my .m file here is the code...

    @synthesize score;
    @synthesize scorelable;

Secondly I have some code which just checks if my drag-able image has collided with a still image. Here is the code...

-(void) ifCollided {

   if(CGRectIntersectsRect(Coin1.frame,MoneyBag.frame)){
       Coin1.hidden = YES;

    }
  }

This code is just a basic way of checking for a collision but once the image has collided I want to add to a int which is displayed in a label. The only way i thought i could do this was by ...

firstly adding this line of code to 'ifCollided'

  [self scorecheck];

So now 'ifCollided' looks something like this:

-(void) ifCollided {

   if(CGRectIntersectsRect(Coin1.frame,MoneyBag.frame)){
       Coin1.hidden = YES;
        [self scorecheck];
    }
  }

Thirdly I had to make 'scorecheck' I did this by...

  -(void)scorecheck{
              score++;
        scorelable.text = [NSString stringWithFormat:@"%i", score];


    }

  }

Here is a image to show that the code I'm currently using is not working correctly. And therefore is not just adding just one to the int that i set to 0. I'm displaying the int in the score label (at the top of my screen). Due to the code not correctly working as long as you touch, drag and hold whilst colliding in the boundaries of the image with the collission rule (in this case the moneybag)

Here is to the image i was talking about:

enter image description here

Any help would be very appreciated

Edit:Here is what i was meant to do in the void ifCollided

-(void) ifCollided {

    if (!Coin1.hidden)
    {
        Coin1.hidden = YES;
        [self scorecheck];
    }
  }

Thanks @Elliott Perry

4

1 回答 1

1

So, to clarify, you would like a mechanism that guarantees that the score will only increment once per each coin colliding with the moneybag?

There are any number of ways to do this but they all require you to keep track of which coins have already been processed in some way. You could give each of your coins a unique reference and store a collection of ids that represent coins that have already been processed, avoiding calling scorecheck if the collection contains the id of the coin in question.

Alternatively, and the method I'd go with, you can mark each coin to let yourself know not to process that coin a second time. Since your hiding the coins on collision, why not use the hidden property to determine whether to increment the score or not:

-(void)ifCollided
{
    if(CGRectIntersectsRect(Coin1.frame,MoneyBag.frame))
    {
        if (!Coin1.hidden)
        {
            Coin1.hidden = YES;
            [self scorecheck];
        }
    }
}

If for whatever reason that isn't feasible here is an example marking the coins using the tag property:

-(void)ifCollided
{
    if(CGRectIntersectsRect(Coin1.frame,MoneyBag.frame))
    {
        if (Coin1.tag != 1)
        {
            Coin1.hidden = YES;
            [self scorecheck];
            Coin1.tag = 1;
        }
    }
}
于 2013-03-30T20:48:35.727 回答