simple function to move an object not doing as wanted

burrows111
Unregistered
 
Post: #1
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 :
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 Smile

thanks in advance - your legends!
Quote this message in a reply
burrows111
Unregistered
 
Post: #2
apologies i used quote to put in my code not the correct one!
Quote this message in a reply
Moderator
Posts: 700
Joined: 2002.04
Post: #3
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 Annoyed 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? Cry

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
burrows111
Unregistered
 
Post: #4
mark - thanks for the update . i shall have a look and get back with my results - thanks
Quote this message in a reply
burrows111
Unregistered
 
Post: #5
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 Sad
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!
Quote this message in a reply
Moderator
Posts: 700
Joined: 2002.04
Post: #6
If you send me the most recent version of your project prior to those changes, I'll try to take a look.

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
burrows111
Unregistered
 
Post: #7
Hi sealfin, have dropped in your inbox - any help and pointers as to where my understanding isnt right is much appreciated.. thanks alot!!!
Quote this message in a reply
Moderator
Posts: 700
Joined: 2002.04
Post: #8
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 Rasp Anyway, I'll fix that and take a longer look...

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
burrows111
Unregistered
 
Post: #9
cheers buddy - more of a saint than a god!
Quote this message in a reply
Moderator
Posts: 700
Joined: 2002.04
Post: #10
I've sent you an email...

Mark Bishop
--
Student and freelance OS X & iOS developer
Quote this message in a reply
burrows111
Unregistered
 
Post: #11
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!
Quote this message in a reply
Member
Posts: 67
Joined: 2006.06
Post: #12
Plug for Alex's site Rasp -
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.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  My pongball won't move!! clapton541 1 1,848 Jan 13, 2007 10:27 AM
Last Post: clapton541
  Why won't my dot move? nmartin 12 4,431 Nov 30, 2003 04:00 PM
Last Post: Fenris