How to add 1 to the score each time an object's y coordinate is equal to another object's y coordinate

StackOverflow https://stackoverflow.com/questions/23132403

  •  05-07-2023
  •  | 
  •  

Вопрос

i have a game where the user must move the ball in a vertical way between 2 objects , i want to have a scoring system that allows me to add 1 to the score each time the ball is equal to the 2 objects so i tried the code below but it is not working probably,it is not adding one each time the ball's y coordinates are equal to the object's y cooridnates.

if(imageview.center.y == imageview1.center.y){

int score = score + 1;
scorelabel.text = [NSString stringWithFormat:@"%i",score];
             } 
Это было полезно?

Решение

As I assume your imageview.centre.y will be different then imageView1.centre.y .Also you could use this method CGRectIntersection(<#CGRect r1#>, <#CGRect r2#>) and place your both frame in provided fields. So it will notify whenever there is an intersection of two frames. Code will be like -

if (CGRectIntersectsRect(self.pacman.frame, self.exit.frame))
{
   //Wirte your score handling code here...
}
else
{
   //Skip or do what you want...
}

Hope this might help you.

Другие советы

Your variable is recreated every time your if is performing. Define it somewhere before @implementation

int score = 0;

And replace int score = score + 1; with score++

As comment you should be aware of comparison of double. See This Question for details

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top