0

可能是我遗漏了一些基本的东西。我创建了一个继承 CCSprite 的类,叫做 Dude。在我的图层中,我添加了对象 dude,它可以正常工作:它显示在屏幕上。一切顺利,直到我触摸屏幕。不知何故,无法达到我班 Dude 中的方法“Jump”。

我得到的错误是:

-[CCSprite Jump::]: unrecognized selector sent to instance 0xf4611a0 2012-05-18 10:20:40.870 bitman[1732:10a03] *由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:'-[CCSprite Jump:: ]: 无法识别的选择器发送到实例 0xf4611a0'

谁能指出我正确的方向?为什么错误显示 [CCSprite Jump::] 而不是 [Dude Jump::]?我错过了什么?

我有一个图层设置如下(仅相关代码):

#import "GameplayLayer.h"
#import "Dude.h"

@implementation GameplayLayer

+(CCScene *) scene
{
    // 'scene' is an autorelease object.
    CCScene *scene = [CCScene node];

    // 'layer' is an autorelease object.
    GameplayLayer *layer = [GameplayLayer node];

    // add layer as a child to scene
    [scene addChild: layer];

    // return the scene
    return scene;
}

-(id)init{
    self=[super init];
    if(self!=nil){
        dude=[[Dude alloc]init] ;
        dude.position=ccp(screenSize.width/2,screenSize.height/2);
        [self addChild:dude];

        fJumpHight=screenSize.height/3;
        fJumpTime=.2f;
    }
    return self;
}

-(void) registerWithTouchDispatcher
{ 
    [[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority: swallowsTouches:YES];
}

-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event{
    [dude Jump:40:3];
    return YES;
}

@end

我设置类 Dude 如下:Dude.h:

#import "CCSprite.h"
#import <Foundation/Foundation.h>
#import "cocos2d.h"

@interface Dude : CCSprite

    -(void) Jump:(float)fHight:(float)fTime;

@end

老兄.m:

#import "Dude.h"
#import <Foundation/Foundation.h>
#import "cocos2d.h"

@implementation Dude
-(id)init{
    self=[super init];
    if(self!=nil){

        CGSize screenSize =[CCDirector sharedDirector].winSize;
        self=[[CCSprite spriteWithFile:@"something.png"]retain];
        self.position=ccp(screenSize.width/2,screenSize.height*0.333f);

    }
    return self;
}

-(void) Jump:(float)fHight:(float)fTime{
    NSLog(@"JUMP!");
    //Jump actions    
}
@end
4

1 回答 1

0

您的主要错误在您的Dude.

self=[[CCSprite spriteWithFile:@"something.png"]retain];

首先,您正确地将self = [super init]wich 叶子分配self为 aDude但通过覆盖将self[CCSprite spriteWithFile:...]更改为 default CCSprite

您可以改为使用self = [super initWithFile:@"something.png"]. 像这样:

-(id)init {
  self = [super initWithFile:@"something.png"];
  if (self == nil) return nil;

  CGSize screenSize =[CCDirector sharedDirector].winSize;
  self.position = ccp(screenSize.width / 2.0, screenSize.height / 3.0);

  return self;
}

另一条建议:在 objc 中,方法通常是小写的,并且倾向于为您提供有关参数的更多信息。所以你可以考虑将你的重命名Jump::

-(void)jumpWithHeight:(float)height duration:(ccTime)duration {
  ...
}

这是因为您阅读代码的频率高于编写代码的频率;)

于 2012-05-18T09:33:14.270 回答