Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-25-2010, 10:30 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 97
hodgey87 is on a distinguished road
Default Football collision with box :(

Hi Guys,

Just a quick one, im trying to increase the score on a game once a ball hits a box. At the minute the score is increasing while the ball is actually moving and not when it hits the box. Im not quite sure where to put this code:

Code:
score += 10;
	scoreLabel.text = [NSString stringWithFormat:@"%d", score];
This is the rest of the code:

Code:
- (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;
	score += 10;
	scoreLabel.text = [NSString stringWithFormat:@"%d", score];
	
	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";
		} else {
			messageLabel.text = @"Ball Hit The Floor :(";
		}
		messageLabel.hidden = NO;
	}
	
}
Id appreciate any help

Cheers
hodgey87 is offline   Reply With Quote
Old 10-25-2010, 04:36 PM   #2 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 97
hodgey87 is on a distinguished road
Default

Been trying this code all night not sure what I'm doing wrong. Would I be able to put the increase score code into the bottom code?
hodgey87 is offline   Reply With Quote
Old 10-25-2010, 04:45 PM   #3 (permalink)
Banned
 
Join Date: May 2010
Location: New Jersey
Posts: 595
Chessin is on a distinguished road
Send a message via AIM to Chessin
Default

Quote:
Originally Posted by hodgey87 View Post
Been trying this code all night not sure what I'm doing wrong. Would I be able to put the increase score code into the bottom code?
Try a method like this...
Code:
- (void)checkcollision {
	if (CGRectIntersectsRect(image.frame, Square.frame)) {
		
		//Below here put in the code to start the score and updating the label
		
		score = score +10;
		scorelabel.text = [NSString stringWithFormat:@"%i" , score];
		
		
	}
Replace image and Square with whatever your buttons or images are

Also what I am doing is here is updating a uilabel in the 2nd line
Chessin is offline   Reply With Quote
Old 10-26-2010, 02:53 AM   #4 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 97
hodgey87 is on a distinguished road
Default

Thats what i tried in the end

I cant seem to be able to get the score to add up though, theres probably something wrong with the maths behind it.

Quote:
- (void)animateBallNSTimer *)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";
} else {
messageLabel.text = @"Ball Hit The Floor ";
}
messageLabel.hidden = NO;
}

}

- (void)checkcollision {
if (CGRectIntersectsRect(ball.frame, paddle.frame)) {

//Below here put in the code to start the score and updating the label

score += 10;
scoreLabel.text = [NSString stringWithFormat:@"%d", score];
/*
if (ballMovement.x > 0 && paddle.frame.origin.x - ball.center.x <= 4)
ballMovement.x = -ballMovement.x;

else if (ballMovement.x < 0 && ball.center.x - (paddle.frame.origin.x +
paddle.frame.size.width) <= 4)
ballMovement.x = -ballMovement.x;
if (ballMovement.y > 0 && paddle.frame.origin.y - ball.center.y <= 4)
ballMovement.y = -ballMovement.y;
else if (ballMovement.y < 0 && ball.center.y - (paddle.frame.origin.y +
paddle.frame.size.height) <= 4)
ballMovement.y = -ballMovement.y;
*/

if (ball.center.y < paddle.center.y) {
ballMovement.y = -ballMovement.y;
float diff=ball.center.x - paddle.center.x;
ballMovement.x=0; // default if it hits the middle
if (diff>0) {
ballMovement.x=1;
}
if (diff<0) {
ballMovement.x=-1;
}
}

}
}
Ive tried 2 different types.

Last edited by hodgey87; 10-26-2010 at 03:06 AM.
hodgey87 is offline   Reply With Quote
Old 10-26-2010, 03:52 AM   #5 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 97
hodgey87 is on a distinguished road
Default

This where i am now,

I can get the score to increment by 10 each time, but when the ball drops down it doesnt hit the box it seems to hit like an invisible line as if it was the bottom of the screen for example:

I added that in here
Code:
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;
	[self checkcollision];
Then this is the collision part:

Code:
- (void)checkcollision {
	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
		
		//Below here put in the code to start the score and updating the label
		NSLog(@"Touched");
		
		score += 10;
		scoreLabel.text = [NSString stringWithFormat:@"%d", score];

				
		if (ball.center.y < paddle.center.y) {
			ballMovement.y = -ballMovement.y;
			float diff=ball.center.x - paddle.center.x;
			ballMovement.x=100;  // default if it hits the middle
			if (diff>100) {
				ballMovement.x=10;
			}
			if (diff<100) {
				ballMovement.x=-10;
			}
		}
		
	}
}
hodgey87 is offline   Reply With Quote
Old 10-26-2010, 06:21 AM   #6 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 97
hodgey87 is on a distinguished road
Default

Right im getting there i think,

I can get the ball to hit the box fine, lives decrease fine now. Its just the scoring part of it. When the ball hits the box the score should only go up by 1. But it goes up about 50 at a time at the minute which is definately wrong.

Heres what i have so far

Code:
- (void)startPlaying {
	if (!lives) {
		lives = 3;
		score = 0;
	}
	scoreLabel.text = [NSString stringWithFormat:@"%05d", 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];

}
- (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 initialiseTimer];
}
- (void)initialiseTimer {
	if (theTimer == nil) {
		float theInterval = 1.0f/30.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";
		} else {
			messageLabel.text = @"Ball Hit The Floor :(";
		}
		messageLabel.hidden = NO;
	}
	[self checkcollision]; ---------I THINK THIS IS THE PROBLEM
	
}

- (void)checkcollision {
	if (CGRectIntersectsRect(ball.frame, paddle.frame)) {
		score += 1;
		scoreLabel.text = [NSString stringWithFormat:@"%d", score];
		
		/*
		if (ball.center.y < paddle.center.y) {
			ballMovement.y = -ballMovement.y;
			
			
			float diff=ball.center.x - paddle.center.x;
			ballMovement.x=100;  // default if it hits the middle
			if (diff>100) {
				ballMovement.x=10;
			}
			if (diff<100) {
				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
Id really appreciate any help
hodgey87 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 329
12 members and 317 guests
7twenty7, chiataytuday, condor304, Desert Diva, Domele, dre, dreamdash3, mottdog, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0