Reputation: 27
I have an attack method I am trying to implement in class 'Enemy', in which it will call upon the subclasses of the class 'Paragon'.
The subclasses are created via @interface in the .h file, and implemented in .m.
This is my current attack method in stated in the Enemy class:
-(void)chosenParagonForAttack:(Paragon*)paragon{
_paragonLink = nil; //_paragonLink, is an object of class Paragon.
_paragonLink = paragon;
[self attackParagon];
[paragon underAttackByEnemy :self];
}
The problem I am getting is that since the actual paragon is created by a subclass, the attack is not occuring. The subclasses define a different "paragon", with differing images and data etc.
Is there a way to call the subclasses just stating the baseclass (Paragon)?
I can work around this by creating an if statement containing every subclass of the Paragon class, however I am interested to find out if there is a better way to go about this.
I was thinking that 'isSubclassOfClass' or 'isKindOfClass' would sort it out but I haven't been successful using them.
Thanks.
Upvotes: 0
Views: 71
Reputation: 116111
This sounds like a general object-oriented design problem, rather than something that's specific to Objective-C.
You shouldn't use a series of if statements that are aware of every Paragon class. Doing so would mean that your Enemy class is tightly-coupled to every Paragon subclass: if you ever wanted to add a new Paragon subclass, you'd also have to update Enemy.
Instead, each of your Paragon subclasses should override the underAttackByEnemy:
method and probably any methods on Paragon that are called from attackParagon
. That way, your sub-classes will know that the attack is occurring and can perform any logic that's specific to them while allowing Enemy to only know about Paragon, rather than all of Paragon's sub-classes. Any attack logic that's common among all the Paragon sub-classes should be implemented in Paragon and then called from the sub-classes: for example, [super underAttackByEnemy:enemy]
Upvotes: 4