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

4

1 回答 1

3

方法声明,虽然我认为在技术上是有效的,但至少对于 ObjC 来说是不寻常的。当您在冒号上包装和对齐(对于长方法调用/声明的习惯)时,效果最佳:

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

通常,一个方法对所有参数都有一个名称:

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

调用肯定是无效的,ObjC 方法不采用逗号分隔的参数列表(除非特别声明这样做,这种情况很少见)。所以这是非法的:

[self evaluateTileAttack: tileTouchedCCNode, tileTouchedCCNode2];

相反,它应该是(虽然不确定这种未命名的格式):

[self evaluateTileAttack:tileTouchedCCNode 
                        :tileTouchedCCNode2];

这绝对有效,并且是预期/推荐的方法:

[self evaluateTileAttack:tileTouchedCCNode 
               otherNode:tileTouchedCCNode2];
于 2014-06-14T07:45:40.153 回答