simple function to move an object not doing as wanted
HI guys, looking for some help, getting in a state while trying to teach myself objective c at the same time
Basically i am coding away making a bowling game right - simple as can be, i have set up my touchesBegan and touchesEnded in such a way that the "game now know s when the user strikes their finger foward as in to throw the ball down the lane, when a vertical strike is detected, i call a function by [self throwBall] which decleration in the code is above and looks like :
ok if i dont use the while loop, the ball will go to the correct position ( -10 on the y axis) however , as soon as i put on this while loop ( intended to animate the ball up the y axis) it doesnt even register the vertical swipe ( i set a UILabel to vertical when detetched )
any help guys - clearly this may not be the best way to animate the ball, and a timer would perhaps be better, but still i am only learning
thanks in advance - your legends!
Basically i am coding away making a bowling game right - simple as can be, i have set up my touchesBegan and touchesEnded in such a way that the "game now know s when the user strikes their finger foward as in to throw the ball down the lane, when a vertical strike is detected, i call a function by [self throwBall] which decleration in the code is above and looks like :
Code:
-(void) throwBall
{
CGPoint location = CGPointMake(ball.center.x, ball.center.y);
while (ball.center.y !=100)
{
location =CGPointMake(ball.center.x, ball.center.y +-10);
ball.center =location;
}
}ok if i dont use the while loop, the ball will go to the correct position ( -10 on the y axis) however , as soon as i put on this while loop ( intended to animate the ball up the y axis) it doesnt even register the vertical swipe ( i set a UILabel to vertical when detetched )
any help guys - clearly this may not be the best way to animate the ball, and a timer would perhaps be better, but still i am only learning

thanks in advance - your legends!
Okay, I haven't used pure Objective-C/Cocoa in quite some time, nor have I slept in quite some time, but if I understand your intentions correctly - that you want to animate the object 'ball' so that it appears to move up the screen - what you've done will never work...
1. You're using an absolute comparison to compare a float type number ("while (ball.center.y !=100)"); errors in floating point approximation mean that you cannot trust that this comparison will ever return true; this isn't the cause of the bug you've encountered, but it is still a problem.
2. You should go away and read up on main loops
What you've done will never animate; it will simply enter the 'throwBall' method and move the 'ball' object to its final position by laborious means, before leaving the 'throwBall' method (after which the 'ball' object is presumably rendered?); I've thrown together some heavily abbreviated code below showing - in general - what you should be doing:
Unless of course sleep deprivation means I'm completely mistaken; somebody else care to comment and clarify?
1. You're using an absolute comparison to compare a float type number ("while (ball.center.y !=100)"); errors in floating point approximation mean that you cannot trust that this comparison will ever return true; this isn't the cause of the bug you've encountered, but it is still a problem.
2. You should go away and read up on main loops
What you've done will never animate; it will simply enter the 'throwBall' method and move the 'ball' object to its final position by laborious means, before leaving the 'throwBall' method (after which the 'ball' object is presumably rendered?); I've thrown together some heavily abbreviated code below showing - in general - what you should be doing:Code:
const CGFloat kBallMovePerTick = ( 100.0 / 1000.0 ); // So the 'ball' object will take 1 second to move 100 pixels...
...
// 'ball' object class attributes
BOOL ballIsMoving;
CGPoint ballPosition;
...
// 'ball' object class methods
-( void )startBallMoving
{
ballIsMoving = YES;
}
-( void )updateBall:( int )ticksSinceLastUpdate;
{
if( ballIsMoving )
{
ballPosition.y -= (( CGFloat )ticksSinceLastUpdate * kBallMovePerTick );
if( ballPosition.y <= 100.0 )
{
ballIsMoving = NO;
ballPosition.y = 100.0;
}
}
}
...
// Inside your swipe detection method...
[ ball startBallMoving ];
...
// Inside your main loop...
[ ball updateBall:ticksSinceLastUpdate ];
[ ball render ];Unless of course sleep deprivation means I'm completely mistaken; somebody else care to comment and clarify?
Mark Bishop
mark - thanks for the update . i shall have a look and get back with my results - thanks
Hi mark - sorry to be such a noob - but ive implemented what you suggested above- i can see the logic behind it. but im still having major newbie issues with adapting the the functions 
what you suggested is where it needs be, with the ball in its own class etc
ive renamed the object to bowlball as apposed to ball above
the code warnings i now get are :
bowlball may not respond to +startBallMoving
another
bowlball may not respond to +updateBall
but will complie and immediately terminate due to uncaught exception
ok, what im sending to update is as you written above
maybe its my initalisation of the variable " ticksSinceLastUpdate "
can you provide any help as to this atall. im in crisis mode with also no lack of sleep as this whole project is due in 3 weeks! argh!

what you suggested is where it needs be, with the ball in its own class etc
ive renamed the object to bowlball as apposed to ball above
the code warnings i now get are :
bowlball may not respond to +startBallMoving
another
bowlball may not respond to +updateBall
but will complie and immediately terminate due to uncaught exception
Quote: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** +[bowlball updateBall:]: unrecognized selector sent to class 0x5408'
ok, what im sending to update is as you written above
maybe its my initalisation of the variable " ticksSinceLastUpdate "
can you provide any help as to this atall. im in crisis mode with also no lack of sleep as this whole project is due in 3 weeks! argh!
If you send me the most recent version of your project prior to those changes, I'll try to take a look.
Mark Bishop
Hi sealfin, have dropped in your inbox - any help and pointers as to where my understanding isnt right is much appreciated.. thanks alot!!!
Okay, I've taken a very quick look at your code, and it looks like your crash is caused by your never having alloc'd/init'd bowlball anywhere
Anyway, I'll fix that and take a longer look...
Anyway, I'll fix that and take a longer look...
Mark Bishop
I've sent you an email...
Mark Bishop
I am very appreciative of the help - is great to know there are people that can help where needed - as per your comments in the email - absolutely a good read up is required. i have 29 days to go in which to incorperate better graphics, object collision, and a scoring system. thinks its going to be a wild ride!
thanks again dude!
thanks again dude!
Plug for Alex's site
-
http://sacredsoftware.net/tutorials/Anim...tion.xhtml
Not sure if that is going to be above your head or not, hopefully not. Give it a read or three.
-http://sacredsoftware.net/tutorials/Anim...tion.xhtml
Not sure if that is going to be above your head or not, hopefully not. Give it a read or three.
The machine does not run without the coin.
Possibly Related Threads...
| Thread: | Author | Replies: | Views: | Last Post | |
| My pongball won't move!! | clapton541 | 1 | 1,697 |
Jan 13, 2007 10:27 AM Last Post: clapton541 |
|
| Why won't my dot move? | nmartin | 12 | 4,186 |
Nov 30, 2003 04:00 PM Last Post: Fenris |
|

