0

It might be that I am missing something fundamental. I have made a class that inherits CCSprite, called Dude. In my layer I add the object dude, which works correctly: it shows on the screen. Everything goes well, until I touch the screen. Somehow the method "Jump" in my class Dude can't be reached.

The error I get is:

-[CCSprite Jump::]: unrecognized selector sent to instance 0xf4611a0 2012-05-18 10:20:40.870 bitman[1732:10a03] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[CCSprite Jump::]: unrecognized selector sent to instance 0xf4611a0'

Can anyone point me in the right direction? Why does the error say [CCSprite Jump::] instead of [Dude Jump::]? What is it that I am missing?

I have a Layer setup as followed (only relevant code):

#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

I setup the class Dude as followed: Dude.h:

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

@interface Dude : CCSprite

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

@end

Dude.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
Buleria
  • 3
  • 1

1 Answers1

0

Your main error is within the implementation of your Dude.

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

First, you correctly assign self = [super init] wich leaves self as a Dude but by overwriting self with [CCSprite spriteWithFile:...] you change that to a default CCSprite.

You could instead use self = [super initWithFile:@"something.png"]. Like this:

-(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;
}

Another piece of advice: In objc methods are usually lowercase and tend to give you more information about the parameters. So you might consider renaming your Jump:: to

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

This is due to the fact that you read code more often than you write it ;)

Tharabas
  • 3,402
  • 1
  • 30
  • 29
  • Thanks! This looks logical...however, it causes an infinite loop.... When I put a NSLog just above the self=[super initWi...], it repeats for ever... – Buleria May 18 '12 at 09:55
  • Umm, that's odd. The implementation of CCSprite looks like there are only `[super initXYZ]` methods called, so no recursion should occurr. You're sure you didn't accidently call `self = [self initWithFile:...]`? – Tharabas May 18 '12 at 10:16
  • Yep, I'm sure. I brought my code to a minimum, I'll post it... there must be some recursion, but it seems not to make sense... – Buleria May 18 '12 at 10:38
  • I found a similar problem and solution: [link]http://stackoverflow.com/a/7298465/1402869[/link] For the part of replacing CCSprite with super, Tharabas' answer is the right one. Thanks! – Buleria May 18 '12 at 11:45