Thread: Game score increasing rapidly....

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    1

    Game score increasing rapidly....

    Not sure if anyone can help me, or if ive even posted in the correct place


    Im building just a simple game with a ball and a paddle at the bottom, everytime the ball hits the paddle you get 1 point. But the minute the score is increasing by 7-8 points everytime you hit the paddle. Im not sure where im going wrong could someone help.

    Code:
    #import "GameViewController.h"
    #import "KeepyUppiesViewController.h"
    #import "GameOverViewController.h"
    
    @implementation GameViewController
    
    @synthesize scoreLabel;
    @synthesize score;
    @synthesize ball;
    @synthesize paddle;
    @synthesize livesLabel;
    @synthesize messageLabel;
    
    - (void)startPlaying {
    	if (!lives) {
    		lives = 3;
    		score = 0;
    		
    	}
    	scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    	livesLabel.text = [NSString stringWithFormat:@"%d", lives];
    	ball.center = CGPointMake(159, 239);
    	ballMovement = CGPointMake(4,4);
    	// choose whether the ball moves left to right or right to left
    	if (arc4random() % 100 < 50)
    		ballMovement.x = -ballMovement.x;
    	messageLabel.hidden = YES;
    	isPlaying = YES;
    	/*
    	[self initialiseTimer];
    	*/
    	[self performSelector:@selector(initialiseTimer) withObject:nil afterDelay:2.0];
    	
    }
    - (void)pauseGame {
    	[theTimer invalidate];
    	theTimer = nil;
    	
    }
    
    
    // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
    - (void)viewDidLoad {
        [super viewDidLoad];
    	
    	[self startPlaying];
    	
    	ballMovement = CGPointMake(4,4);
    	
    	[self performSelector:@selector(initialiseTimer) withObject:nil afterDelay:2.0];
    
    
    
    	 }
    - (void)initialiseTimer {
    	
    	
    	if (theTimer == nil) {
    		
    		float theInterval = 1.0f/50.0f;
    		
    		theTimer = [NSTimer scheduledTimerWithTimeInterval:theInterval target:self
    												  selector:@selector(animateBall:) userInfo:nil repeats:YES];
    		
    	}
    }
    
    - (void)animateBall:(NSTimer *)theTimer {
    	
    	ball.center = CGPointMake(ball.center.x+ballMovement.x, ball.center.y+ballMovement.y);
    	
    	BOOL paddleCollision = ball.center.y >= paddle.center.y - 16 &&
    	
    	ball.center.y <= paddle.center.y + 16 &&
    	ball.center.x > paddle.center.x - 32 &&
    	ball.center.x < paddle.center.x + 32;
    	
    
    	if(paddleCollision)
    		ballMovement.y = -ballMovement.y;
    	
    	
    	if (ball.center.x > 310 || ball.center.x < 16)
    		ballMovement.x = -ballMovement.x;
    	
    	if (ball.center.y < 32)
    		ballMovement.y = -ballMovement.y;
    	if (ball.center.y > 444) {
    		[self pauseGame];
    		isPlaying = NO;
    		lives--;
    		
    		livesLabel.text = [NSString stringWithFormat:@"%d", lives];
    		if (!lives) {
    			messageLabel.text = @"Game Over";
    			[self performSelector:@selector(initialiseTimer) withObject:nil afterDelay:2.0];
    			
    			GameOverViewController *second = [[GameOverViewController alloc] initWithNibName:nil bundle:nil];
    			second.score = score;
    			second.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
    			[self presentModalViewController:second animated:YES];
    			
    			
    		} else {
    			messageLabel.text = @"Ball Hit The Floor :(";
    		}
    		messageLabel.hidden = NO;
    	}
    
    
    	
    	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
    		
    		
    		score ++;
    		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
    		
    		[self checkcollision];
    			stopdetection = FALSE;
    		
    		
    	}
    	
    	
    }
    
     - (void)checkcollision {
     if (!stopdetection && CGRectIntersectsRect(ball.frame, paddle.frame)) {
    
     
     stopdetection = TRUE;
     
     float diff=ball.center.x - paddle.center.x;
     ballMovement.x=150;  // default if it hits the middle
     if (diff>150) {
     ballMovement.x=10;
     }
     if (diff<150) {
     ballMovement.x=-10;
     }
     
     
     }
     
     }
     
     
     
     
     
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    	if (isPlaying) {
    		
    		UITouch *touch = [[event allTouches] anyObject];
    		touchOffset = paddle.center.x -
    		[touch locationInView:touch.view].x;
    		
    	} else {
    		[self startPlaying];
    		
    	}
    }
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    	if (isPlaying) {
    		
    		UITouch *touch = [[event allTouches] anyObject];
    		float distanceMoved =([touch locationInView:touch.view].x + touchOffset) -paddle.center.x;
    		float newX = paddle.center.x + distanceMoved;
    		if (newX > 30 && newX < 290)
    			
    			paddle.center = CGPointMake( newX, paddle.center.y );
    		
    	}
    }
    
    
    - (void)didReceiveMemoryWarning {
    	// Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    	
    	// Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
    	// Release any retained subviews of the main view.
    	// e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
    	[scoreLabel release];
    	[ball release];
    	[paddle release];
    	[livesLabel release];
    	[messageLabel release];
        [super dealloc];
    }
    
    @end
    I havent been programming long so be easy on me

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    What is this?

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    9
    Quote Originally Posted by Bubba View Post
    What is this?
    objective c

    had to look it up.. had never see actual usage :-|
    Last edited by illizit; 11-03-2010 at 07:36 PM.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    This is a C++ forum therefore it serves that Game programming in the context of this board means Game Programming in C/C++.

    This is not an Objective C board and your question will be best answered elsewhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extracting lowest and highest score from array
    By sjalesho in forum C Programming
    Replies: 6
    Last Post: 03-01-2011, 06:24 PM
  2. my upcoming UNO card game :)
    By Hussain Hani in forum Game Programming
    Replies: 5
    Last Post: 01-24-2008, 01:19 AM
  3. Please comment on my c++ game
    By MegaManZZ in forum Game Programming
    Replies: 10
    Last Post: 01-22-2008, 11:03 AM
  4. Game Engine Link Prob
    By swgh in forum Game Programming
    Replies: 2
    Last Post: 01-26-2006, 12:14 AM
  5. Game Programmer's AIM Circle: Join Today
    By KingZoolerius66 in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 12-20-2003, 12:12 PM