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 Game Development

Reply
 
LinkBack Thread Tools Display Modes
Old 03-19-2011, 12:01 AM   #1 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 8
Sbasham is on a distinguished road
Default Ball floats off of screen (Tutorial)

I finally got the ball to move and the paddle to move when i click the screen but now when i make it so the ball collides with the paddle. there is no screen border. so i will just float off of the screen and never come back.

Following: Beginning iPhone Games Development.


I have narrowed it down to these lines of code:
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;
	
	if(paddleCollision) {
		ballMovement.y = -ballMovement.y;
		
		if (ball.center.y >= paddle.center.y - 16 && ballMovement.y < 0) {
			ball.center = CGPointMake(ball.center.x, paddle.center.y - 16);
		} else if (ball.center.y <= paddle.center.y + 16 && ballMovement.y > 0) {
			ball.center = CGPointMake(ball.center.x, paddle.center.y + 16); 
		} else if (ball.center.x >= paddle.center.x - 32 && ballMovement.x < 0) {
			ball.center = CGPointMake(ball.center.x - 32, ball.center.y);
		} else if (ball.center.x <= paddle.center.x + 32 && ballMovement.x > 0) {
			ball.center = CGPointMake(paddle.center.x + 32, ball.center.y);
		}

Last edited by Sbasham; 03-19-2011 at 12:03 AM.
Sbasham is offline   Reply With Quote
Old 03-22-2011, 10:04 PM   #2 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 8
Sbasham is on a distinguished road
Default

Sbasham is offline   Reply With Quote
Old 03-24-2011, 01:15 PM   #3 (permalink)
Win
iOS dev
 
Join Date: Jan 2011
Location: Philadelphia
Posts: 11
Win is on a distinguished road
Thumbs up

This looks like the example from the aPress bbok: introduction to iPhone gaming.

Basically, you need to calculate the position of the ball and if it's x value is less than say 5 and greater than 300 (just example #'s) reverse it's x direction. Same thing for the Y value. It's very similiar to making the ball "bounce" off the paddle.


This code goes inside the gameLogic or animateBall method called by the timer.
Code:
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) {
			[self saveGameState];
			messageLabel.text = @"Game Over";
		} else {
			messageLabel.text = @"Ball Out of Bounds";
		}
		messageLabel.hidden = NO;
	}
The source code is available for free @ APRESS.COM: Beginning iPhone Games Development
Win is offline   Reply With Quote
Old 03-24-2011, 06:53 PM   #4 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 8
Sbasham is on a distinguished road
Default

So i just coded the boundaries in the wrong part of the code?

I downloaded the source code but i cant run it. says SDK is missing.

I used that code in place of what i had. and it still travels out of bounds.

Last edited by Sbasham; 03-24-2011 at 06:57 PM.
Sbasham 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: 424
5 members and 419 guests
chemistry, hussain1982, Retouchable, skrew88, SLIC
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,679
Threads: 94,128
Posts: 402,921
Top Poster: BrianSlick (7,990)
Welcome to our newest member, xzoonxoom
Powered by vBadvanced CMPS v3.1.0

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