Search code examples
ioscocos2d-iphoneextendlevelhelper

Extending a LHSprite from LevelHelper


I am trying to extend a LHSprite from LevelHelper so I can add different behavior to different elements.

Ok, so far so good, but what do you want?

Basically imagine a set of characters that have different movements. I want to be able to define a class Character that extends LHSprite and defines a move method. All characters should extend this Character class and define their own movement. This way I can add elements to the map and I can treat them (in terms of movement) in the same way.

Ok, I understood that, but what have you done?

So far I have followed this link about custom LHSprites but I am facing a problem: The first difference from my case to that one is that I don't add my elements using the LevelHelper (at least the ones I am trying to extend). I add my elements in code because I want a random number of those elements in a random position.

So I have made this init method that creates a cop (that extends Character and Character extends LHSprite). This method actually looks more to "add to loader" method but whatever:

- (id) initInLoader:(LevelHelperLoader *) loader andNumber: (int) i 
                                                     atPos: (CGPoint) pos{
    self = (Cop *) [ loader createBatchSpriteWithName:@"cop_01" 
                            fromSheet:@"copSheet" fromSHFile:@"enemies" ];

    _uniqueName = [ NSString stringWithFormat:@"Cop_%d", i + 1 ];
    [ self setUniqueName: _uniqueName];

    [ self prepareAnimationNamed:@"cop" fromSHScene:@"enemies" ];
    [ self setAnimationDelayPerUnit: 1.0f/70.0f ];

    self.position = pos;

    [ self playAnimation ];
}

So far so good. I can see my cop standing and animated. However, when I try to call the move method (or any other method) it gives me an unrecognized selector crash.

I believe this error happens because of the cast to (Cop *) but I don't know how to surpass this.


Solution

  • I found out that I needed to add a tag to the level helper. However, this tag wasn't updating in the source files so I had to add it manually in the LevelHelperLoader.h, at the enum LevelHelper_TAG.

    Then I had to register that tag when I initialize the LevelHelper

     [[LHCustomSpriteMgr sharedInstance] registerCustomSpriteClass:[Cop class] 
                                                            forTag:COP_TAG];
    

    And I had to pass that tag when I get that element from the loader:

    self = (GlowWorm *) [ loader createBatchSpriteWithName:@"cop_01" 
                                                 fromSheet:@"copSheet" 
                                                fromSHFile:@"enemies" 
                                                       tag: COP_TAG];
    

    EDIT

    To answer the user that made a new answer with a problem

    Don't forget to tell about the new class to LevelHelper before you initialize LevelHelper. You can do that with this line:

    [[LHCustomSpriteMgr sharedInstance] registerCustomSpriteClass:[Cop class] 
                                                           forTag: COP_TAG ];