У меня есть пара узлов спрайтов на моем экране: child1, child2, которые наследуются от родителя. У родителя есть метод customAction, который перезаписывает дочерние1 и дочерние2. И все они наследуются от skspritenode.
Теперь, в моем touchesBegin я использую линию
SKSpriteNode * touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
Но я хочу вызвать дочернюю версию customAction из дочерней1, если затронутый узел имеет тип дочерняя1 и дочерняя2 версия, если дочерняя2.
Есть ли «красивый» или «лучший» способ сделать это, кроме как пометить спрайты, а затем использовать оператор switch для приведения узла к соответствующему дочернему типу?
Спасибо!
редактировать: код
// parent class
@interface animal : SKSpriteNode
-(int)getID;
-(void)yell;
@end
@implementation Animal
{
int animalID;
bool clickable;
}
-(id)initAnimalWithIDWithImage:(int)ID :(NSString *)animalType
{
NSDictionary * returnedAnimal = [animalParser getAnimalByType:animalType];
NSString *Icon = [returnedAnimal objectForKey:@"Icon"];
self = [super initWithImageNamed:Icon];
animalID = ID;
return self;
}
-(int)getID
{
return animalID;
}
-(void)yell
{
// **todo**
}
@end
// child1
@interface bear : animal
-(void)yell;
@end
@implementation bear
-(id)initBearWithID:(int)ID
{
return [super initAnimalWithIDWithImage:ID :@"Bear"];
}
-(void)yell
{
// **todo**
}
@end
// child2
@interface boar: animal
-(void)yell;
@end
// identical to bear
// in the scene
-(id)initGameScene:(CGSize)size :(int)mode
{
[self initialize:size];
if(self = [super initWithSize:size])
{
bear testBear = [[Bear alloc] initBearWithID:idCount++];
testBear.position = CGPointMake(300,300);
[self addChild:testBear];
}
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch * touch = [touches anyObject];
CGPoint positionInScene = [touch locationInNode:self];
// problem solved!
// this is wrong
// SKSpriteNode * touchedNode = (SKSpriteNode *)[self nodeAtPoint:positionInScene];
// this is right
animal *touchedNode = (animal *)[self nodeAtPoint:positionInScene];
[touchedNode yell];
// need to something like if touchedNode is a bear do bear yell.. if boar do boar yell
}