2

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

dandan78
  • 13,328
  • 13
  • 64
  • 78
J C
  • 63
  • 1
  • 7

1 Answers1

3

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];
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • 1
    Thank you for the clarification! It works great now. I still can't vote this. My rep too low. Why is my rep not going up? – J C Jun 14 '14 at 19:25
  • you have 6 rep now ;) – CodeSmile Jun 14 '14 at 19:46
  • Thanks for the rep bump! However, now I have been banned from asking questions on here. I really didn't think the questions were unreasonable. If they are can someone explain why? I have read the instructions but there are times when I simply can't post the code I have tried because what I try doesn't work. One of my posts was simply asking (because it had been a while since anyone had asked) if it was possible to get details on another games leaderboard. – J C Jun 14 '14 at 19:52
  • i don't think your account is banned but individual questions may have been put on hold or deleted by a moderator, depending on offtopic, quality, etc – CodeSmile Jun 14 '14 at 22:08