I appreciate the responses everyone - I checked out that tutorial but it doesn’t seem to address my core issue since I didn’t see anything in there that handled collision detection. Where I stand right now is that I can spawn enemies with custom attributes without issue - specifying sprite, health, speed, etc. It’s the collision detection that’s causing an issue, and here’s why:
levelScene.m
- (void)didBeginContact:(SKPhysicsContact *)contact{
if(firstBody.categoryBitMask == primaryProjectileCategory && secondBody.categoryBitMask == enemyCategory){
[self primaryWeaponProjectile:firstBody.node didCollideWithEnemy:(SKSpriteNode *)secondBody.node];
}
}
Above is the code for detecting any collision, and then routing to the appropriate function (below)
- (void)primaryWeaponProjectile:(SKNode *)weaponSprite didCollideWithEnemy:(SKSpriteNode *)body2{
spawnEnemy *enemyClass = (spawnEnemy *)body2;
[enemyClass doDamageWithAmount:l1_primaryWeaponDamage];
[weaponSprite removeFromParent];
}
spawnEnemy.m
-(void)doDamageWithAmount:(float)damageAmount{
_currentHealth -= damageAmount;
int scoreAddAmount = 0;
SKAction* pulseRed = [SKAction sequence:@[[SKAction colorizeWithColor:[SKColor redColor] colorBlendFactor:1.0 duration:0.1],
[SKAction colorizeWithColorBlendFactor:0.0 duration:0.1]]];
[enemySprite runAction:pulseRed];
if(_currentHealth <= 0){
[self makeExplosion:self.position];
[self removeFromParent];
}
}
For the collision, I establish the enemy class, then call a function therein that will handle damage called doDamageWithAmount. The file called to do this is spawnEnemy.m . This is fine for something small scale, but as I said I won’t have enemies all behaving the same way so I might need to “do damage” to enemyScout enemyBoss, enemyShooter, etc.
I just don’t see a way to accommodate different enemy classes with the way my files are set up right now, which is why I was hoping I could have some sort of “enemy handler” which would call the appropriate class to do damage to the enemy. I think I’ll look into the class hierarchy suggestion randall put forward - it sounds closest to what I might need but I’ll have to research how to execute it. Having read the above do you agree that’s the route I should be taking?
Quick Update: Subclassing is looking promising, having read parts of this tutorial. I’m going to give it a go tomorrow - I still welcome suggestions though, my methods are certain to be out of date at this point, having started the project almost 2 years ago now.
New Update: Alright, I’ve tried subclassing and that seems to do the trick, so thank you all for your suggestions. I’m actually feeling pretty giddy at the moment at the prospect of having overcome this hurdle, will probably be working all weekend on the app now .
@randall: I’m not sure why you deleted your post, but it was precisely what I needed - I pictured in my mind that subclassing was what I had to do, I just didn’t know how to do it or what to call it, funny how knowing the right term can help so much.
@aeberbach: I appreciate you pointing me toward GameplayKit - while it wasn’t what I was looking for it is good to know about so I can keep an eye on it for my next project, should I ever finish this one.
To jgnovak: Reading what you posted again I think it may have worked, it’s a little hard to tell since I’m not entirely clear on how Enums work or even what they really are. That said I’m looking forward to reading the tutorial - I’ve been hoping to find for more of those on here that deal 2D game development.