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:
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.
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.
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!!
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
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 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.