0

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];
             } 
rmaddy
  • 314,917
  • 42
  • 532
  • 579

3 Answers3

1

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.

nikhil84
  • 3,235
  • 4
  • 22
  • 43
  • I used this method to end the game , but I want the score code to run without them hitting each other , I only want the score code to run when it is equal to its y coordinates without hitting it – Abdullah Ossama Omari Apr 17 '14 at 18:07
  • okay I see so I assume problem is that you are setting int score in if condition which will initialise score everytime its true. So you need to assign score in viewdidload method or init method like int score = 0; Then in your if condition write score = score + 1 or score++; That's it ! Hope I helped you out. – nikhil84 Apr 18 '14 at 04:19
  • I have already tried that and it didn't work though , I think the problem is with comparing floats and I can't see how to fix that – Abdullah Ossama Omari Apr 20 '14 at 12:02
  • I tried below code:- `NSLog(@"View- %f",_viewShadow.center.y); NSLog(@"View- %f",_viewShadow1.center.y); if(_viewShadow.center.y == _viewShadow1.center.y) { NSLog(@"Equal"); } else { NSLog(@"Not Equal"); }` This worked for me... also could you provide more code so I have see on its working. – nikhil84 Apr 21 '14 at 05:40
  • Also screen shot of game where it gets in contact of another view. – nikhil84 Apr 21 '14 at 05:42
  • the game uses touchesmoved method to move the bird wherever the finger is moved and there are two obstacles that are moving downward and between them is a gap where the bird should enter, i want the score to increase by one when it enters the gap ,i tried to use an if statement as shown above but its not accurate ,since sometimes it doesn't increase by one ,and rarely does,and also i have i would be grateful if you can tell me what does the %f means. thanks for your help – Abdullah Ossama Omari Apr 22 '14 at 14:16
  • "f" is for Float...just like "d" for int. – nikhil84 Apr 23 '14 at 03:58
  • You should create a CAlayer(clear background color) that fill that gap between the two obstacle and then check with **CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)** if bird is there or not. Bonzaaaa !!! You caught the bird !!!..hope this help's u. Also if you come across some alternative do let m know. – nikhil84 Apr 23 '14 at 04:24
  • Thanks A lot , ur idea did work and now the game runs as I wanted it to run , but the problem which I now faced is that if I swipe the bird too fast sometimes the CGRectIntersectRect doesn't get called , thanks for the help – Abdullah Ossama Omari Apr 24 '14 at 04:31
  • @AbdullahOssamaOmari in case when you moving it fast ...you must be considering only start and end point of bird. So intersect method dont get notified. it's like you drag in go. Check for points using nslog ! – nikhil84 Apr 24 '14 at 04:56
  • ya i already did this, and for some reason it wont do that any more even i swiped very fast between the obstacles. – Abdullah Ossama Omari Apr 24 '14 at 13:20
  • So when you move it very fast that time if you check for points then you will see that your bird x and y doesn't match with gap frame x and y. As when moving fast the points captured are start and end of your movement so I tried to look into that but didn't got much idea. Will ping you if found any solution or alternative regarding that Or if you find out any solution do let me know. – nikhil84 Apr 26 '14 at 13:17
  • it works perfectly now ,even if i swipes fast,i dont know why does it work but it is working perfectly now,i guess that regenerating the CAlayer when it goes down to fall down again from the top made it work. – Abdullah Ossama Omari Apr 28 '14 at 15:40
  • @AbdullahOssamaOmari okay cool !... also don't forget to vote up...would be appreciated. – nikhil84 May 01 '14 at 13:14
0

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++

nicael
  • 18,550
  • 13
  • 57
  • 90
0

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

Community
  • 1
  • 1
Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15