J C
J C

Reputation: 63

Multiple parameters in method issue

My first shot at creating a method with multiple parameters. Still trying to wrap my head around how Objective C does things. Been banging my head for a couple days on this now. Finally ready to ask for help. Searched and tried many posts here on stack overflow. Below is various code chunks I'm working with ... this is a cocos2d v3 project FYI.

// MainPlayScene.h
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#include <sys/sysctl.h>

@interface MainPlayScene : CCScene <CCPhysicsCollisionDelegate>
 + (MainPlayScene *)scene;
 - (id)init;
 - (void)evaluateTileAttack:(CCNode*)tileTouchedCCNode : (CCNode*)tileTouchedCCNode2;
@end


// MainPlayScene.m
#import "cocos2d.h"
#import "MainPlayScene.h"

@implementation MainPlayScene
{
 CCNode *tileTouchedCCNode;
 CCNode *tileTouchedCCNode2;
}

+ (instancetype)scene
{
 return [[self alloc] init];
}

- (id)init
{
 return self;
}

- (void)evaluateTileAttack: (CCNode*)ccnode1 : (CCNode*)ccnode2
{
 NSLog(@"ccnode1: %@", ccnode1.physicsBody.collisionType);
 NSLog(@"ccnode2: %@", ccnode2.physicsBody.collisionType);
}

- (void)actionMenuAttackHandler: (id)sender
{
 [self evaluateTileAttack: tileTouchedCCNode, tileTouchedCCNode2];
  ^^^^^^^^^^^^^^^^^^^^^
  error at this line
}

@end

ERROR: No visible @interface for 'MainPlayScene' declares the selector 'evaluateTileAttack:'

Not sure why I am getting this error because I think I am declaring in MainPlayScene.h properly ...

Upvotes: 2

Views: 51

Answers (1)

CodeSmile
CodeSmile

Reputation: 64477

The method declaration, though technically valid I think, is at least unusual for ObjC. Best seen when you wrap and align (as is customary for long method calls/declarations) on the colon:

- (void)evaluateTileAttack:(CCNode*)tileTouchedCCNode 
                          :(CCNode*)tileTouchedCCNode2;

Normally a method has a name for all parameters:

- (void)evaluateTileAttack:(CCNode*)tileTouchedCCNode 
                 otherNode:(CCNode*)tileTouchedCCNode2;

The call is definitely invalid, ObjC methods do not take a comma-separated list of parameters (unless specifically declared to do so, which is rare). So this is illegal:

[self evaluateTileAttack: tileTouchedCCNode, tileTouchedCCNode2];

Instead it should be (not sure about this unnamed format though):

[self evaluateTileAttack:tileTouchedCCNode 
                        :tileTouchedCCNode2];

This definitely works and is the expected/recommended approach:

[self evaluateTileAttack:tileTouchedCCNode 
               otherNode:tileTouchedCCNode2];

Upvotes: 3

Related Questions