iDevGames Forums
simple function call not working - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Programming Languages & Scripting (/forum-8.html)
+--- Thread: simple function call not working (/thread-199.html)

simple function call not working - burrows111 - Mar 30, 2010 02:49 PM

HI guys,
such a newbie at objective c in general, i am trying to implement a simple function

in my .h file i have declared the function as so:
Quote:- (void) throwBall;

have described what i wish to do when the function is called as so:
Quote:-(void) throwBall

//do this etc

and when i wish this to happen:

is done like so:
Quote:- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:self.view];
CGFloat deltaX = fabsf(gestureStartPoint.x - location.x);
CGFloat deltaY = fabsf(gestureStartPoint.y - location.y);

if (deltaY >= kMinimumGestureLength && deltaX <= kMaximumVariance) {
player1.text = @"Vertical swipe"; //debug to check for vertical swipe of any kind based on the boundaries at top

//implement ball move on y!

throwBall(); // HERE IS THE FUNCTION CALL!!!

and i get compile errors such as

implicit decleration of throwBall;

property throwBall requires method -ballThrown to be defined - use @synthesise etc

im sorry for posing what i believe to be such a simple thing to do ( is in other languages that im quite competent at) but until i get my head around function decleration i can press on with my final year project - yes due in 3 weeks!

have read reems of google stuff but to no avail.

any help please - much appreciated

simple function call not working - sealfin - Mar 30, 2010 03:14 PM

Because what you have declared is not a function - it is a method - and should be called using:
[ [i]<object>[/i] throwBall ];
The function equivalent to that method would be declared as:
void throwBall( void );

simple function call not working - michaeln1978 - Mar 30, 2010 04:37 PM

As sealfin said; to call class method in objective-c you use the syntex


so if you are calling your throwBall method in the same class that you defined it you could call it like so:
[self throwBall];

@interface ClassName : NSObject {

-(void) throwBall;

@implementation ClassName

    if(!(self = [super init])) return nil;

    // Lets throw the ball.
    [self throwBall];

-(void) throwBall
  // Do stuff here


Now lets say you create an object of ClassName in some other class you create. In that case you would call throw ball like this:
ClassName *myClassNameObject = [[ClassName alloc] init];
[myClassNameObject throwBall];

I hope that helps.