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 01-02-2011, 02:46 PM   #1 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default How to animate a ball image with bounce and create multiple images with NSTimer

Hi Smasher, with respect to my earlier messages to you ,

I have a project I have been working on for sometime now and my problem is that I can't seem to get the ball in the project to bounce off the bottom of the iphone screen and be removed when they go off the screen...this is my complete code:


Code:
@interface FallingBalllViewController : UIViewController {
UIImage *ballImage;
CGPoint ballMovement;



}
@property (nonatomic, retain) UIImage* ballImage;



- (void)onTimer;


Code:
@implementation fallingBallViewController

@synthesize ballImage;


- (void)viewDidLoad {
[super viewDidLoad];



self.view.backgroundColor = [UIColor colorWithRed:0.5 green:0.5 blue:1.0 alpha:1.0];


ballImage = [UIImage imageNamed:@"ball.png"];




[NSTimer scheduledTimerWithTimeInterval1.00) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}





- (void)onTimer
{


UIImageView* ballView = [[UIImageView alloc] initWithImage:ballImage];



CGPointMake(ballMovement.x,ballMovement.y);


int x=arc4random() % 310;

int y = round(random() % 320);
double scale = 1 / arc4random() % 100 + 1.0;
double speed = 1 / arc4random() % 100 + 1.0;



ballView.frame= CGRectMake(x, -100, 40 * scale, 40 * scale);

CGRect newFrame = ballView.frame;

newFrame.origin.x += ballMovement.x;
newFrame.origin.y += ballMovement.y;


if (newFrame.origin.x > 310 || newFrame.origin.x < 16)
ballMovement.x = -ballMovement.x;
if (newFrame.origin.y < 32)
ballMovement.y = -ballMovement.y;	


[self.view addSubview:ballView];

[UIView beginAnimations:nil context:ballView];


[UIView setAnimationDuration: 5 * speed];



ballView.frame = CGRectMake(y,450, 40 * scale,40 * scale);



if (newFrame.origin.x > 310 || newFrame.origin.x < 16)
ballVelocity.x = - ballVelocity.x;
if (newFrame.origin.y < 32)
ballVelocity.y = -ballVelocity.y;	


[UIView setAnimationDidStopSelector:@selector(onAnimationC omplete:finished:context];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];



}



- (void)onAnimationCompleteNSString *)animationID finishedNSNumber *)finished contextvoid *)context {

UIImageView *ballView = context;
[ballView removeFromSuperview];
// memory 
// usage in check
NSLog([NSString stringWithFormat:@"[ballView retainCount] = %d", [ballView retainCount]]);
[ballView release];


}

- (BOOL)shouldAutorotateToInterfaceOrientationUIIn terfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
// Release anything that's not essential, such as cached data
}


- (void)dealloc {
[ballImage release];

[super dealloc];
}

@end
On running this code, balls are produced randomly and the balls just keep falling through the screen quite all right . What I really want is to get the balls to bounce at the bottom of the screen or at a point I set manually also to bounce on themselves self if they collide and eventually get removed if they bounce off the screen.

Please can you look at this for me because I really don't know where I have gone wrong or what I am not doing right.

Thnaks in advance.
Cheers!!
eyinEkpe is offline   Reply With Quote
Old 01-02-2011, 03:32 PM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

I don't think that beginAnimations / commitAnimations are a good choice for game-type apps; once you set up an animation for a UIview it stops reporting the current position of your views.

You probably should use a single timer that runs 10-20 frames per second. It will trigger a method that moves all of the balls, and changes their direction if they hit the bottom.

You also need a way to give each ball a "direction" property - this could be a single positive/negative number to indicate up or down, or it could be tweo numbers (like a CGPoint) to indicate any 2d direction.

For each ball to have a separate direction variable You'll need to create a "Ball" class; it might be a subclass of UIImageView (not great, but fine for your first game) or it might have a pointer to the UIImageView.

Colliding against each other is a little harder; the loop that moves the balls will have to check each ball against the others and change its direction. I'd work on getting the bouncing working first.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-02-2011, 05:43 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default

Hi smasher, thanks for agreeing to work on the bounce...

Currently, I am using just one ball image and the NSTimer actually fires 20 times per second so I have balls been produced from one ball image. My main problem is to get them, the balls to bounce when they get to the bottom of the iPhone screen as all that happens now is that the balls just fall off the screen.

I have tried using CGPoint to reverse the movement of the ball (ballMovement.x and ballMovement.y) as you can see in the code but to no avail.

In sum I really need the ball to recognise other objects or points and be able to bounce. I look forward to receiving your help on this...
Thanks once again. Cheers!!
eyinEkpe is offline   Reply With Quote
Old 01-02-2011, 06:11 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Currently, I am using just one ball image and the NSTimer actually fires 20 times per second so I have balls been produced from one ball image.
From your code it looks like onTimer fires once per second; not sure about that though, and it creates a new ball and uses beginAnimations / commitAnimations to move it from the top to the bottom of the screen.

You want separate methods, one that creates new balls and adds them to an array (every second?) and another that moves all the balls 10-20 times per second and changes their direction if they hit the bottom.

Quote:
Originally Posted by eyinEkpe View Post
I have tried using CGPoint to reverse the movement of the ball (ballMovement.x and ballMovement.y) as you can see in the code but to no avail.
I see this code:

Code:
if (newFrame.origin.x > 310 || newFrame.origin.x < 16)
ballMovement.x = -ballMovement.x;
if (newFrame.origin.y < 32)
ballMovement.y = -ballMovement.y;
I see your intent -- if the ball x is greater than 310 we'll reverse direction -- but it seems like you have only one ballMovement variable. Each ball needs its own ballMovement variable, right? Some might be going up and some might be going down. I'd suggest you need to create a "Ball" class; it might be a subclass of UIImageView (not great, but fine for your first game) or it might have a pointer to the UIImageView.

You might want to look at the random rocks code for a start:
http://www.iphonedevsdk.com/forum/ip...dom-rocks.html
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-03-2011, 02:41 AM   #5 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default

Yeah it fires once per second .The reason is that if set to fire 10 or 20 times per second then I will have a rain of balls falling down the screen...

Ok, so now are you saying that I need to have two NSTimers, where one creates the balls and the other takes care of the balls movement?
eyinEkpe is offline   Reply With Quote
Old 01-04-2011, 12:40 AM   #6 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

You definitely want two *methods*, one to create balls and one to move them. You can trigger them with two timers, or with one timer that calls an "update" methods. The "update" method can call moveBalls every frame and createBalls less often by using some kind of counter or time accumulator.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 01-04-2011, 03:38 PM   #7 (permalink)
Registered Member
 
Join Date: Dec 2010
Posts: 14
eyinEkpe is on a distinguished road
Default

Ok smasher, thanks a lot for your help. I will try out what you just suggested.
Cheers!
eyinEkpe is offline   Reply With Quote
Reply

Bookmarks

Tags
animate, ball, bounce, image, nstimer

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: 415
12 members and 403 guests
7twenty7, chemistry, ChrisYates, djohnson, gmarro, hussain1982, Kirkout, Retouchable, skrew88, SLIC, walex, xzoonxoom
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:09 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0