简体   繁体   English

从LevelHelper扩展LHSprite

[英]Extending a LHSprite from LevelHelper

I am trying to extend a LHSprite from LevelHelper so I can add different behavior to different elements. 我正在尝试从LevelHelper扩展LHSprite ,以便可以向不同的元素添加不同的行为。

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. 我希望能够定义一个扩展LHSprite的类Character并定义一个move方法。 All characters should extend this Character class and define their own movement. 所有角色都应扩展此Character类并定义自己的移动。 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). 到目前为止,我已经跟踪了有关自定义LHSprites的链接,但是我遇到了一个问题:与我的案例的第一个区别是,我没有使用LevelHelper添加我的元素(至少是我尝试扩展的元素)。 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 ). 因此,我使用了此初始化方法来创建cop(扩展了CharacterCharacter扩展了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. 但是,当我尝试调用move方法(或任何其他方法)时,它使我无法识别选择器崩溃。

I believe this error happens because of the cast to (Cop *) but I don't know how to surpass this. 我认为发生此错误是由于强制转换为(Cop *)但我不知道该如何超越。

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 . 但是,此标记未在源文件中更新,因此我不得不在enum LevelHelper_TAG的LevelHelperLoader.h中手动添加它。

Then I had to register that tag when I initialize the LevelHelper 然后我必须在初始化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. 初始化LevelHelper之前,请不要忘记向LevelHelper讲述新类。 You can do that with this line: 您可以在此行中执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM